简体   繁体   English

动态分配内存以指向结构数组的指针

[英]dynamically allocate memory for pointer to array of structure

hi i have this structure 嗨,我有这个结构

typedef struct STUDENT
{
    char studName[20];
    int timeEnter;
    int timeUpdate;
}STUDENT;

and the local pointer to array of structure 和指向结构数组的本地指针

STUDENT *studArr[100];

I'm trying to allocate memory for the structure by doing reading in the first line of the file then use it to allocate memory for the structure. 我正在尝试通过在文件的第一行中读取内容来为结构分配内存,然后使用它为结构分配内存。

fscanf(fp, "%s", &first);
**studArr = (STUDENT**) malloc(sizeof(STUDENT*)*first);

I got an error saying that no operator "=" matches these operands on the allocation line 我收到一个错误消息,说没有运算符“ =”与分配行上的这些操作数匹配

why am I gettting the error, what did i do wrong here? 为什么我得到错误,我在这里做错了什么?

thank in advance 预先感谢

I think you're confusing things, it looks like you're declaring an array of pointers, when all you need is a single pointer. 我认为您感到困惑,看起来您在声明一个指针数组,而您所需的只是一个指针。 Note that as long as you're indexing properly, a pointer to "one" struct is the same as a pointer to a hundred. 请注意,只要索引正确,指向“一个” struct的指针就等于指向100的指针。

You should probably have: 您可能应该具有:

STUDENT *studArr;

then, once you know how many you need (I'm assuming first is the number of students to allocate room for): 然后,一旦您知道您需要多少(我first假设是要分配空间的学生数量):

studArr = malloc(first * sizeof *studArr);

Also note that no casting is needed . 另请注意, 不需要强制转换

If you want to allocate an array of 100 students, you have two choices: 如果要分配一个由100个学生组成的数组,则有两种选择:

struct STUDENT
{
    char studName[20];
    int timeEnter;
    int timeUpdate;
};


struct STUDENT studArr1[100];

... OR ...

struct STUDENT *studArr2 = (struct STUDENT *)malloc (sizeof (struct STUDENT) * 100);

If you just want a pointer, you can say: 如果只需要一个指针,可以说:

  struct STUDENT *p = studArr1;

PS: PS:

I deliberately left the "typedef" stuff out so as not to confuse the basic issue (struct vs. array of struct vs pointer to array of struct). 为了避免混淆基本问题(结构vs.数组vs指向struct的指针),我特意省略了“ typedef”内容。

You have defined an array of pointers to struct, not a pointer to an array of struct. 您已经定义了一个指向struct的指针数组,而不是指向struct数组的指针。 The memory for the array is already allocated, as a global array. 该阵列的内存已作为全局阵列分配。

The type of **studArr is STUDENT, and hence is not compatible with the expression to the right of the assignment operator, which is casted to (STUDENT**) . **studArr的类型为STUDENT,因此与赋值运算符右侧的表达式不兼容,该表达式**studArr(STUDENT**)

You probably meant: 您可能的意思是:

STUDENT **studArr;

[...] [...]

fscanf(fp, "%s", &first);
studArr = (STUDENT**) malloc(sizeof(STUDENT*)*first);

As you are using STUDENT *studArr[100]; 当您使用STUDENT * studArr [100]时; then it means you have allocated the memory for 100 pointers for STUDENT structure type. 则表示您已为STUDENT结构类型分配了100个指针的内存。

so for allocating the memory you should try. 因此,您应该尝试分配内存。

studArr =malloc(sizeof(struct STUDENT)*first);

then you can access all the allocated members as studArr[i] ....so on . 那么您可以将所有已分配的成员作为studArr[i] ....so on

You're not exactly clear on what your overall goal is. 您不清楚总体目标是什么。

First of all, you want to use %d if you want to read in an integer: 首先,如果要读取整数,则要使用%d

int count;
if (fscanf(fp, "%d", &count) != 1) { // error... }

Next, don't use STUDENT as both the struct name and the typdef. 接下来,不要同时使用STUDENT作为结构名称和typdef。 Just leave the struct name out: 只需省略结构名称即可:

typedef struct
{
    char studName[20];
    int timeEnter;
    int timeUpdate;
} student_t;

Now, you're going to want just a pointer to an array of student_t s: 现在,您只需要一个指向student_t s数组的指针:

student_t* student_array = NULL;

Finally, allocate that array: 最后,分配该数组:

student_array = malloc(count * sizeof(student_t));

Now you can use it, like: 现在您可以使用它,例如:

strncpy(student_array[0].studName, "Bobby", 20);

Or get a pointer to an individual student: 或获得指向单个学生的指导:

student_t* bob = &student_array[1];
bob->timeEnter = 42;

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

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