将2d数组转换为指针?

时间:2016-04-13 20:29:06

标签: c arrays pointers

我正在尝试自学C,所以我写了一个程序来保存成绩簿的记录!在我学习指针的努力中,我试图将我开始的数组项目之一转换为指针。我想将我的2d数组转换为指针。下面是我使用2d数组的原始程序,下面是我尝试将其转换为2d指针数组。

原创计划

int numberPeople, choice, i, j;
char people[15][3][100];

printf("Please indicate number of records you want to enter (min %d, max %d): ", 5, 15);

scanf("%d", &numberPeople);

while ((numberPeople < 5) || (numberPeople > 15)) {
    printf("\nNumber not in specified range, try again.\n");
    printf("Please indicate number of records you want to enter (min %d, max %d): ", 5, 15);
    scanf("%d", &numberPeople);
}

printf("\n");

while ((getchar()) != '\n'); // flush the return (and anything else) after the number input above

printf("Enter the first name, last name, and grade (put a space in between each): \n");

for (i = 0; i < numberPeople; i++) {
    char tempArr[MAXIMUM_LINE_LENGTH];

    fgets(tempArr, MAXIMUM_LINE_LENGTH, stdin);

    char *token = strtok(tempArr, " ");

    for (j = 0; j < DATA_FIELDS && token != NULL; j++) {
        strncpy(people[i][j], token, MAXIMUM_DATA_LENGTH);
        token = strtok(NULL, " \r\n");
    }

}

二维阵列的尝试 - &gt; POINTER

int numberPeople, choice, i, j;
char* people;


printf("Please indicate number of records you want to enter (min %d, max %d): ", 5, 15);

scanf("%d", &numberPeople);

people = (char*)(malloc(numberPeople*DATA_FIELDS*sizeof(char)));

while ((numberPeople < 5) || (numberPeople > 15)) {
    printf("\nNumber not in specified range, try again.\n");
    printf("Please indicate number of records you want to enter (min %d, max %d): ", 5, 15);
    scanf("%d", &numberPeople);
}

printf("\n");

while ((getchar()) != '\n'); // flush the return (and anything else) after the number input above

printf("Enter the first name, last name, and grade (put a space in between each): \n");

for (i = 0; i < numberPeople; i++) {
    char* tempArr;

    fgets(tempArr, 100, stdin); // Thread 1: EXC_BAD_ACCESS code=1 address=0x0

    char *token = strtok(tempArr, " ");

    for (j = 0; j < 3 && token != NULL; j++) {
        strncpy(people, token, 50);
        token = strtok(NULL, " \r\n");
    }

}

在人员输入步骤中,它是在它打破时。它适用于第一个条目,但它会遇到一个断点(我使用Xcode),它读取&#34; EXC_BAD_ACCESS&#34;,我不太清楚这意味着什么,任何提示会有所帮助,谢谢!

2 个答案:

答案 0 :(得分:0)

是的,你可以转换为所有指针,但我不推荐它。这是一个例子:

char ***people = malloc(sizeof(*people) * 15);
for (int i = 0; i < 15; i++) {
    people[i] = malloc(sizeof(*people[i]) * 3);
    for (int j = 0; j < 3; j++) {
        people[i][j] = malloc(sizeof(*people[i][j]) * 100);
    }
}

这会重新创建数组char people[15][3][100];。如你所见,它很乱。另外,完成后你必须释放所有的内存。

我更清洁的方法是使用结构:

struct Student {
    char firstName[100];
    char lastName[100];
    char grade[100];
};
struct Student people[15];

这将留出与数组一样多的内存。您还可以根据需要动态分配尽可能多的学生:

struct Student *people = malloc(sizeof(*people) * 20);

这将有20名学生。您可以使用数组表示法访问它们:

printf("%s %s %s", people[0].firstName, people[0].lastName, people[0].grade);

您只需拨打free一次。

答案 1 :(得分:0)

这一行不正确:

char* tempArr; 
fgets(tempArr, 100, stdin); 

指针的声明:tempArr没有分配任何实际内存来包含调用fgets()所读取的行建议:

#define MAX_INPUT_LEN (100)
 ... 
char tempArr[MAX_INPUT_LEN]; 
fgets( tempArr, MAX_INPUT_LEN, stdin );

此外,应检查调用fgets()的返回值(!= NULL)以确保操作成功。