-4

This is just part of a bigger code, but it's full of errors so I try to fix them one by one. When I try to use malloc on my pointer vector the line returns this error

main.c|14|error: expected '{' before '*' token

Any resolutions?

   struct students {
       int group;
       char name[20];
       int grade;
   };

   int main()
   {
         struct students *ptr[100];
         int num, i, max=0;
         scanf("%d", &num);
         ptr = (struct*) malloc(num * sizeof(struct));
         if(ptr == NULL)
             {
              printf("error");
              exit(0);
             }
    }
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
virtualghost
  • 33
  • 1
  • 6
  • 1
    You put `struct` where you should have put `struct students`. – user2357112 Dec 20 '16 at 05:05
  • 1
    `struct *` is a sytanx error. Maybe you meant `struct students *`. BTW you can avoid this error by not casting malloc, which is redundant anyway, and not repeating the type in the `sizeof`. – M.M Dec 20 '16 at 05:05
  • `sizeof(struct students)`. Don't cast the return of malloc. – Robert Prévost Dec 20 '16 at 05:05
  • I get this now and I need to use heap vector in this exercise.. main.c|14|error: assignment to expression with array type – virtualghost Dec 20 '16 at 05:08
  • Note that `ptr` is the name of an array (of 100 pointers to `struct student`). You can't assign directly to an array like that. That's quite apart from the problem already identified with `sizeof(struct student)` missing the word `student`. However, you need to rethink what you're trying to do more than just fixing that syntax error. – Jonathan Leffler Dec 20 '16 at 05:29

3 Answers3

0
  1. While using malloc for a 1D array, you should allocate a pointer, not an array of pointers as you have done.

  2. While allocating you are using sizeof(struct). The type here is struct students and you need sizeof(struct students)

  3. Do not cast the result of malloc. See Do I cast the result of malloc?

The final code is

struct students *ptr;
ptr = malloc (num * sizeof(struct students));
Community
  • 1
  • 1
Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
0

You have an array of pointers to structure. You should allocate memory for them separately.

for(int i=0; i<100 && i<num; i++)
{
    ptr[i] = malloc(sizeof(struct students));
    if(0 == ptr[i])
    {
        /* Handle this case. */
    }
}
/* Your code. */

/* At the end free the memory. */
for(int i=0; i<100; i++)
{
    if(0 != ptr[i])
    {
        free(ptr[i]);
        ptr[i] = 0;
    }
}

But I think you just wanted to allocate an array of struct students. In that case you just need one pointer.

struct students *ptr = 0;

/* You allocate memory and store it in that pointer. */
ptr = malloc(num * sizeof(struct students)); 
if(0 == ptr)
{
    /* Handle this case. */
}

You can access ith element of the array like ptr[i]. But add necessary checks and make sure i < num.

You need to free the allocated memory whenever you are done using the array.

if(0 != ptr)
{
    free(ptr);
    ptr = 0;
}
MayurK
  • 1,925
  • 14
  • 27
0

Struct is reserved keyword for declaring/defining structures in C, it isn't variable, nor something you cant get size of it. You have declared struct students (according to your code, i think it should be student instead of students), now you have to define a variable and allocate space for 100 structs via a double pointer, the code should be something like this

   struct student {
       int group;
       char name[20];
       int grade;
   };

   int main()
   {
         struct student ** ptr;
         int num, i, max=0;
         scanf("%d", &num);
         ptr = malloc(num * sizeof(struct student));
         if(ptr == NULL)
             {
              printf("error");
              exit(0);
             }
    }

Now you can access individual students with array subscript

ptr[0]->grade = 20; // putting 20 in grade of first student

Also, there is no need for casting malloc result in C

e.jahandar
  • 1,715
  • 12
  • 30