0

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

bluebk
  • 169
  • 1
  • 6
  • 21

5 Answers5

3

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.

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):

studArr = malloc(first * sizeof *studArr);

Also note that no casting is needed.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
  • for some reason the compiler that I'm using asked to cast the malloc function to STUDENT*, It maybe because I'm using a c++ compiler – bluebk Apr 25 '13 at 07:33
1

If you want to allocate an array of 100 students, you have two choices:

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:

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

paulsm4
  • 114,292
  • 17
  • 138
  • 190
0

You have defined an array of pointers to struct, not a pointer to an array of 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**).

You probably meant:

STUDENT **studArr;

[...]

fscanf(fp, "%s", &first);
studArr = (STUDENT**) malloc(sizeof(STUDENT*)*first);
Elazar
  • 20,415
  • 4
  • 46
  • 67
0

As you are using STUDENT *studArr[100]; then it means you have allocated the memory for 100 pointers for STUDENT structure type.

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.

akp
  • 1,753
  • 3
  • 18
  • 26
  • thank I found another question that have the same issue http://stackoverflow.com/questions/15397728/c-pointer-to-array-of-pointers-to-structures-allocation-deallocation-issues – bluebk Apr 25 '13 at 07:09
0

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:

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

Next, don't use STUDENT as both the struct name and the 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_ts:

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;
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328