简体   繁体   中英

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?

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. 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.

In that case, you don't need to dynamically allocate the nameStudent field at all. 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

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

you have the * at the front dereferencing an undefined pointer to 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 * . Your code has additional bugs such as the (int) cast on the next line. I suggest more study of the C language and more careful attention to whether and how your program meets its requirements. C demands great precision.

Update:

Since nameStudent is an array, it cannot and should not have a pointer assigned to it.

Your code contains many other errors as well.

fgets(buffer, RECORDS_SIZE, fp); 

This reads RECORDS_SIZE bytes, not one STUDENT record.

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

buffer[0] makes no sense here. Setting just the first element of studentArr makes no sense .. you don't set any of the other elements in the loop. There should not be an fgets call outside the loop ... a call that is never checked for error or EOF.

Your program contains too many errors and misunderstandings for your question to be answered without writing your code for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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