简体   繁体   English

为指向结构数组的指针分配内存

[英]allocate memory for pointer to array of structure

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "queue.h"
#include "stack.h"

#define RECORDS_SIZE 100
#define NAME_SIZE    20

typedef struct Student
{
    char nameStudent[NAME_SIZE];
    int  TimeIn;
    int  TimeUpdate;
}STUDENT;

typedef struct TUTOR
{
    char nameTutor[NAME_SIZE];
    int TutorTIme;
    STUDENT *ptr;
}TUTOR;


QUEUE *queue1;
STACK *stack1;

void getData(STUDENT *studentArr[RECORDS_SIZE]);

int main (void)
{

    STUDENT *studentArr[RECORDS_SIZE];
    FILE *fp = NULL;

    getData(studentArr);

    return 0;
} 

void getData(STUDENT *studentArr[RECORDS_SIZE])
{
    FILE *fp;
    char fileName[NAME_SIZE];
    char buffer[RECORDS_SIZE];
    int count = 0;

    printf("Enter file name: ");
    gets(fileName);
    fp = fopen(fileName, "r");
    if (fp == NULL)
    {
       printf("Error! The file does not exist!\n");
    }

    fgets(buffer, RECORDS_SIZE, fp);
    *studentArr = (STUDENT*) malloc(buffer[0]*sizeof(STUDENT));
    while (fgets(buffer, RECORDS_SIZE, fp) != NULL)
    {
        printf("%s", buffer[count]);
       *studentArr[count]->nameStudent = (char*) malloc(strlen(buffer)*sizeof(char));
        studentArr[count]->TimeIn = (int) malloc(strlen(buffer)*sizeof(int));
        sscanf(buffer,"%[,],%d", *studentArr[count]->nameStudent, &studentArr[count]->TimeIn);

        printf("%s%d\n", studentArr[count]->nameStudent, &studentArr[count]->TimeIn);
       count++;
    }

    return;
}

there is a warning that said assignment makes integer from pointer without a cast [enabled by default]| 有警告,该赋值是从指针中获取整数而没有强制转换[默认启用] | on the line where I allocate memory for *studentArr[count]->nameStudent, why am I getting this warning? 在为* studentArr [count]-> nameStudent分配内存的行上,为什么会收到此警告?

This is what my file look like 这是我的文件的样子

4
A,10
B,12
C,60
D,120
tutorY

I tried to read in the first line then use the number on the first line to allocate the pointer to array of structure and the continue reading and then allocate the rest of members of structure 我尝试在第一行中读取,然后使用第一行中的数字将指针分配给结构数组并继续读取,然后分配结构的其余成员

I think i did the while loop wrong, may be I'm not suppose to call fgets and then reuse it in while loop as it appears to have an error there. 我认为我在while循环中做错了,可能是因为我不应该调用fgets,然后在while循环中重用它,因为它在那里似乎有错误。 How do I fix this function? 如何修复此功能?

thank in advance 预先感谢

Revised Question 修订的问题

nameStudent is an array of 20 character, I think this may cause an issue when allocating. nameStudent是一个20个字符的数组,我认为这可能在分配时引起问题。

In that case, you don't need to dynamically allocate the nameStudent field at all. 在这种情况下,您根本不需要动态分配nameStudent字段。 When you create a student structure, all the space for the name is allocated as part of student structure. 创建学生结构时,名称的所有空间都将分配为学生结构的一部分。


Earlier observations 较早的观察

If you really have: 如果您确实有:

*studentArr[count]->nameStudent = (char*) malloc(strlen(buffer)*sizeof(char));

then for the plausible type of nameStudent 然后输入合理的nameStudent类型

 struct Xxxx
 {
     ...
     char *nameStudent;
     ...
 };

you have the * at the front dereferencing an undefined pointer to char. 您在前面有* ,它取消引用了指向char的未定义指针。 This is why you get the warning: 这就是为什么您收到警告:

 assignment makes integer from pointer without a cast [enabled by default]

You are likely to be better served by a structure with a fixed size student name: 具有固定大小的学生姓名的结构可能会更好地为您服务:

 struct Xxxx
 {
     ...
     char nameStudent[32];
     ...
 };

*studentArr[count]->nameStudent = (char*)...

Although you have not provided the definition of STUDENT , this is surely wrong ... if nameStudent is char* , you're trying to store into its first byte via that initial * . 尽管您尚未提供STUDENT的定义,但这肯定是错误的...如果nameStudent为char* ,则尝试通过该初始*将其存储到其第一个字节中。 Your code has additional bugs such as the (int) cast on the next line. 您的代码还有其他错误,例如下一行的(int)强制转换。 I suggest more study of the C language and more careful attention to whether and how your program meets its requirements. 我建议您更多地研究C语言,并更加注意程序是否以及如何满足其要求。 C demands great precision. C要求很高的精度。

Update: 更新:

Since nameStudent is an array, it cannot and should not have a pointer assigned to it. 由于nameStudent是一个数组,因此不能也不应为其分配指针。

Your code contains many other errors as well. 您的代码也包含许多其他错误。

fgets(buffer, RECORDS_SIZE, fp); 

This reads RECORDS_SIZE bytes, not one STUDENT record. 这将读取RECORDS_SIZE个字节,而不是一个STUDENT记录。

*studentArr = (STUDENT*) malloc(buffer[0]*sizeof(STUDENT));

buffer[0] makes no sense here. buffer[0]在这里没有意义。 Setting just the first element of studentArr makes no sense .. you don't set any of the other elements in the loop. 只设置studentArr的第一个元素没有任何意义..您无需在循环中设置任何其他元素。 There should not be an fgets call outside the loop ... a call that is never checked for error or EOF. 循环外不应存在fgets调用...永远不会检查错误或EOF的调用。

Your program contains too many errors and misunderstandings for your question to be answered without writing your code for you. 您的程序包含太多错误和误解,无法在不为您编写代码的情况下回答您的问题。

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

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