如何在C中初始化结构
由于2-3天我有一个小问题,与一个项目,事实上,我的项目是做一个汽车环岛。我是C的初学者,所以我试着自己初始化一个结构 我的结构是:如何在C中初始化结构,c,C,由于2-3天我有一个小问题,与一个项目,事实上,我的项目是做一个汽车环岛。我是C的初学者,所以我试着自己初始化一个结构 我的结构是: typedef struct Car Car; struct Car { char vehicule; /* N, S, E, W */ int speed; /* 1 => slow ou 2 => fast */ int pos_i; /* Position courante 'i' du véhicule sur la route */ int
typedef struct Car Car;
struct Car
{
char vehicule; /* N, S, E, W */
int speed; /* 1 => slow ou 2 => fast */
int pos_i; /* Position courante 'i' du véhicule sur la route */
int pos_j; /* Position courante 'j' du véhicule sur la route */
char alig; /* 'l' for left and 'r' for right */
};
然后我创建了一个名为initCar的函数,以便将两个随机汽车放置在红色圆圈的23,1处,橙色圆圈的21,1处,如下所示:
我的汽车功能:
Car initCar () {
Car car [43][90] = {{0}};
char direction [] = {'N', 'S', 'E', 'W'};
int position [2][2] = {{0}};
position [0][0] = 23;
position [0][1] = 1;
position [1][0] = 21;
position [1][1] = 1;
int i = 0, j = 0;
srand (time (NULL));
for (i = 0; i < 43; i++) {
for (j = 0; j < 90; j++) {
car [i][j].vehicule = direction [rand () % 4];
car [i][j].speed = 1;
car [i][j].pos_i = position [i][j];
car [i][j].pos_j = position [i][j];
car [i][j].alignement = 'r';
}
printf("\n");
}
roadMap(car);
}
和职能c:
void myDelay (float i) { // Fais une pause de l'app durant i secondes
clock_t start, end;
start = clock ();
while (((end = clock ()) - start) <= i * CLOCKS_PER_SEC);
}
void roadMap (Car matrice [NB_LIN][NB_COL]) {
int i = 0, j = 0;
//NB_LIN de NB_COL
for (i = 0; i < NB_LIN; i++) {
for (j = 0; j < NB_COL; j++) {
switch (i) {
case 20: {
if (j < 19) {
do {
printf("■");
j++;
} while (j < 19);
}
else if (j < 33) {
do {
printf("%c", matrice[i][j].vehicule);
j++;
} while (j < 33);
}
else if (j < 53) {
do {
printf("╬");
j++;
} while (j < 53);
}
else if (j < 67) {
do {
printf("%c", matrice[i][j].vehicule);
j++;
} while (j < 67);
}
else if (j < 87) {
do {
printf("■");
j++;
} while (j < 87);
}
}
break;
case 18:
case 22: {
if (j < 10) {
do {
printf("─ ");
j++;
} while (j < 10);
}
else if (j < 23) {
do {
printf("%c", matrice[i][j].vehicule);
j++;
} while (j < 23);
}
else if (j < 43) {
do {
printf("╬");
j++;
} while (j < 43);
}
else if (j < 57) {
do {
printf("%c", matrice[i][j].vehicule);
j++;
} while (j < 57);
}
else if (j < 68) {
do {
printf("─ ");
j++;
} while (j < 68);
}
}
break;
} printf("\n");
}
}
Car initCarFromWest () {
Car car [43][90] = {{0}};
char direction [] = {'N', 'S', 'E', 'W'};
int position [2][2] = {{0}};
position [0][0] = 23;
position [0][1] = 1;
position [1][0] = 21;
position [1][1] = 1;
int i = 0, j = 0;
srand (time (NULL));
for (i = 0; i < 43; i++) {
for (j = 0; j < 90; j++) {
car [i][j].vehicule = direction [rand () % 4];
car [i][j].speed = 1;
car [i][j].pos_i = position [i][j];
car [i][j].pos_j = position [i][j];
car [i][j].alignement = 'r';
}
printf("\n");
}
roadMap(car);
}
Voiture initCarFromEst () {
}
Voiture initCarFromSouth () {
}
Voiture initCarFromNorth () {
}
我的功能路线图是近400行,这就是为什么我给你一个示例的原因。首先,你不是在创建2辆车,而是在创建43*90=3870辆车。您正在申报一个43x90系列的汽车,总共有3870辆汽车。然后迭代数组中的所有汽车,初始化所有3870辆汽车。但是索引超出了位置数组的边界。位置是2x2数组,因此两个索引都必须在[0,1]范围内。但是您使用循环索引对其进行索引,因此您将远远超过数组的边界,并且您将获得调用堆栈上的任何值
听起来你想要做的就是拥有两辆车。这应该会更好。你有43*90辆车,正如你刚刚宣布的汽车[43][90]。 即使您将它们声明为包含0的“空” 我建议您修改代码如下:Car*Car[43][90]={{0};基本上,将每个指针都设置为NULL,而非空指针指向动态分配的移动车辆,除了明显减少内存消耗外,这也会更有效。当然,您需要相应地修改地图绘制功能
您的位置[i][j]是错误的,因为i和j都在数组限制之外移动位置仅为[2][2]。我想你的地图是43x90,这就是你困惑的根源。如果是这种情况,你甚至不应该有一个位置变量pos_i和pos_j,因为数组索引已经标记了位置,比如arr[i][j]是汽车,那么汽车就在i,j中。如果你只想放置两辆汽车,为什么要创建3870辆汽车?请点击@guillome评论中的蓝色链接:它是Minimal,Complete,可验证的示例–这样我们可以重现您的问题案例,以测试可能的解决方案。事实上,我们需要围绕你的代码创建一个框架来运行它,这可能会隐藏你的错误。我把我的mcve放在我编辑的answersJongware中:在我的项目结束时,我必须制作一个可以像这样移动我的汽车的程序;[Moving Cars][1]这就是为什么我制作了一个包含43林和90列的2d数组[1]:我不明白你在说什么我英语这么差^^你有什么例子吗?
#ifndef FONCTIONS_H_INCLUDED
#define FONCTIONS_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define NB_LIN 43
#define NB_COL 90
#define clrscr() printf("\033[H\033[2J")
#define couleur(param) printf("\033[%sm",param)
typedef struct Car Car;
struct Car
{
char vehicule; /* N, S, E, W */
int speed; /* 1 => slow ou 2 => fast */
int pos_i; /* Position courante 'i' du véhicule sur la route */
int pos_j; /* Position courante 'j' du véhicule sur la route */
char alig; /* 'l' for left and 'r' for right */
};
void roadMap (char matrice [NB_LIN][NB_COL]);
void initCarFromEst ();
#include "function.h"
int main () {
initCarFromEst ();
return 0;
}
void myDelay (float i) { // Fais une pause de l'app durant i secondes
clock_t start, end;
start = clock ();
while (((end = clock ()) - start) <= i * CLOCKS_PER_SEC);
}
void roadMap (Car matrice [NB_LIN][NB_COL]) {
int i = 0, j = 0;
//NB_LIN de NB_COL
for (i = 0; i < NB_LIN; i++) {
for (j = 0; j < NB_COL; j++) {
switch (i) {
case 20: {
if (j < 19) {
do {
printf("■");
j++;
} while (j < 19);
}
else if (j < 33) {
do {
printf("%c", matrice[i][j].vehicule);
j++;
} while (j < 33);
}
else if (j < 53) {
do {
printf("╬");
j++;
} while (j < 53);
}
else if (j < 67) {
do {
printf("%c", matrice[i][j].vehicule);
j++;
} while (j < 67);
}
else if (j < 87) {
do {
printf("■");
j++;
} while (j < 87);
}
}
break;
case 18:
case 22: {
if (j < 10) {
do {
printf("─ ");
j++;
} while (j < 10);
}
else if (j < 23) {
do {
printf("%c", matrice[i][j].vehicule);
j++;
} while (j < 23);
}
else if (j < 43) {
do {
printf("╬");
j++;
} while (j < 43);
}
else if (j < 57) {
do {
printf("%c", matrice[i][j].vehicule);
j++;
} while (j < 57);
}
else if (j < 68) {
do {
printf("─ ");
j++;
} while (j < 68);
}
}
break;
} printf("\n");
}
}
Car initCarFromWest () {
Car car [43][90] = {{0}};
char direction [] = {'N', 'S', 'E', 'W'};
int position [2][2] = {{0}};
position [0][0] = 23;
position [0][1] = 1;
position [1][0] = 21;
position [1][1] = 1;
int i = 0, j = 0;
srand (time (NULL));
for (i = 0; i < 43; i++) {
for (j = 0; j < 90; j++) {
car [i][j].vehicule = direction [rand () % 4];
car [i][j].speed = 1;
car [i][j].pos_i = position [i][j];
car [i][j].pos_j = position [i][j];
car [i][j].alignement = 'r';
}
printf("\n");
}
roadMap(car);
}
Voiture initCarFromEst () {
}
Voiture initCarFromSouth () {
}
Voiture initCarFromNorth () {
}