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

5
  • why **studArr and why sizeof(STUDENT*)? Commented Apr 25, 2013 at 6:59
  • because i want to allocate pointer to structure so i can point to structure member later on Commented Apr 25, 2013 at 7:02
  • do not cast malloc() return type... Commented Apr 25, 2013 at 7:09
  • @akp That's the least of his issues. And not even a real issue. Commented Apr 25, 2013 at 7:10
  • @JonathonReinhart but i have posted the answer too... Commented Apr 25, 2013 at 7:11

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

1 Comment

how can i be able to allocate if i have something like this STUDENT *studArr[100];?
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);

Comments

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.

1 Comment

thank I found another question that have the same issue stackoverflow.com/questions/15397728/…
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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.