简体   繁体   English

Struct指针包含指向char(字符串)的指针,如何为其分配内存?

[英]Struct pointer contains pointer to char (string) , how to allocate memory for it?

Excuse my english , my program crashes as it is , but when i change the struct member *name (string) to name[50] and remove the command (students+i)->name = (char*)malloc(50 * sizeof(char)); 请问我的英语,我的程序按原样崩溃,但是当我将结构成员*name (string)更改为name[50]并删除命令(students+i)->name = (char*)malloc(50 * sizeof(char)); the program works fine , so i guess the problem is that i do something wrong when dynamically allocating memory for each name. 该程序运行良好,所以我想问题是为每个名称动态分配内存时我做错了什么。

#include <stdio.h>
#include <stdlib.h>

struct student
{
    char *name;
    int grade;
};

int main()
{
    int student_number = 0,i;
    struct student *students = NULL;

    printf("Give the number of students: ");
    scanf("%d",&student_number);

    students = (struct student*)malloc(student_number * sizeof(struct student));

    for(i=0;i<student_number;i++)
    {
        (students+i)->name = (char*)malloc(50 * sizeof(char));

        system("CLS");

        printf("Give the name of student no. %d: ",i+1);
        scanf(" %s",&(students+i)->name);
        printf("Give the grade of student no. %d: ",i+1);
        scanf("%d",&(students+i)->grade);
    }

    system("CLS");

    for(i=0;i<student_number;i++)
    {
        printf("%d. %s %d\n",i+1,(students+i)->name,(students+i)->grade);
    }

    system("PAUSE");
    return 0;
}

Some of the correct ways to call C scanf function are: 调用C scanf函数的一些正确方法是:

scanf("%d", int*)
scanf("%s", char*)

This is incorrect: 这是不正确的:

scanf("%s", char**)

Consider the working code: 考虑工作代码:

int grade;
char name [50];
scanf("%d", &grade);
scanf("%s", name);

Here grade is an int variable, so &grade is the pointer to a int variable, having type int* . 这里grade是一个int变量,因此&grade是指向int变量的指针,类型为int* So this is correct. 所以这是正确的。

name is an array of char , when passed to scanf it decays to a char pointer (type char* ) point to the first element of the array. name是一个char数组,当传递给scanf它会衰减为指向数组第一个元素的char指针(类型char* )。 So this is also correct. 所以这也是正确的。

char name [50];
scanf("%s", &name);

&name is a pointer to an array of 50 char s, having type char(*)[50] , so technically this is incorrect, but it happens to work because the pointer have the same value as &name[0] or (char*) name . &name是指向50 char数组的指针,类型为char(*)[50] ,因此从技术上讲这是不正确的,但由于指针与&name[0](char*) name具有相同的值,因此它确实起作用(char*) name

struct a { int x; int y; };
a b;
scanf("%d", &b);

This code is also wrong, but happens to work because &b have the same value (most of the time) as &b.x , so the code will have identical functionality to 该代码也是错误的,但由于&b (在大多数情况下)与&b.x具有相同的值(大多数情况下),因此它&b.x ,因此该代码将具有与

scanf("%d", &b.x);

although the pointer types are different. 尽管指针类型不同。 Don't rely on the behavior, anyway. 无论如何,不​​要依赖行为。

int grade;
scanf("%d", grade);

This crashes because grade is not a pointer to a int . 由于grade不是指向int的指针,因此会崩溃。

Now consider dynamically allocated array (of course, remember to call free(name) when you've done using it). 现在考虑动态分配的数组(当然,请记住在使用完数组后调用free(name) )。

char* name;
name = (char*) malloc(50 * sizeof(char));

Consider using calloc instead. 考虑改用calloc But anyway, in this case, name is a char pointer that point to the first byte of the allocated memory. 但是无论如何,在这种情况下, name是一个char指针,指向已分配内存的第一个字节。 So 所以

scanf("%s", name)

is correct, because name is a char* pointing to the allocated memory, as I said above. 是正确的,因为name是指向分配的内存的char* ,如上所述。

scanf("%s", &name)

&name is the pointer to the char pointer name , having type char** . &name是指向char指针name的指针,类型为char** Incorrect. 不正确。


In conclusion, use & wisely. 总之,使用&明智的。 Don't stick to the pattern scanf("format", &variables) . 不要坚持使用scanf("format", &variables)


Unrelated note: Don't use (students+i)->name , use students[i].name instead. 无关的注释:请勿使用(students+i)->name ,而应使用students[i].name It's much clearer. 更清楚了。

Firstly, remove the '&' in the scanf(" %s" &(students+i)->name) This is because it is already a pointer, and the '&' makes it a pointer to a pointer when it asks for just a pointer. 首先,在scanf(“%s”&(students + i)-> name)中删除'&',这是因为它已经是一个指针,并且'&'使其成为指向指针的指针只是一个指针。 After adding this change it works fine for me. 添加此更改后,对我来说效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM