-1

What would be the correct syntax to allocate memory to scores for each array of type sections? Can I declare a variable that points to that structure and allocate it that way? Or do I have to go through the course structure first?

I've allocated: course* ptr = (course*)malloc(*num_courses * sizeof(course));

ptr[i].course_name = (char*)malloc(10);
ptr[i].sections = malloc(ptr[i].num_sections * sizeof(ptr[i].sections));
Cobra04
  • 9
  • 2
  • 3
    What does "sections of (arrays of?) students" represent? Why is it a double pointer? It's simpler to think, when you write small functions that do one small thing. Write a `student_init()` function that initializes one student only. Then write `course_init_sections()` that initializes sections only, and build up. – KamilCuk May 26 '20 at 07:14
  • 1
    Show more code, not only some out of context snippets. – Jabberwocky May 26 '20 at 07:15
  • 1
    And why have you made a student** if all you wanted was an array? – kesarling He-Him May 26 '20 at 07:19
  • It's actually an exercise that's made to be overly difficult. I've solved the problem with my own code, but I had to scrap that since it's required to use these structures. @d4rk4ng31 I've tried ptr[i].sections[j].scores = malloc("size I need"). – Cobra04 May 26 '20 at 07:25
  • 1
    Your code does not have any *array type* contained within it. Each pointer must hold as its value (e.g. point to) a valid block of memory. Take each pointer one-by-one and ask "*What does this pointer point to?*" If you need to allocate memory, do so. If you need to assign the address of an existing block, do so. Also note, in C, there is no need to cast the return of `malloc`, it is unnecessary. See: [Do I cast the result of malloc?](http://stackoverflow.com/q/605845/995714) – David C. Rankin May 26 '20 at 07:30
  • @DavidC.Rankin So is course* ptr = malloc(*num_courses * sizeof(course)); not create an array of course structures? – Cobra04 May 26 '20 at 07:34
  • Presuming `*num_courses` is a valid numeric value, then the allocation is find. Better `course *ptr = malloc (*num_courses * sizeof *ptr);` (using the dereferenced pointer to set the type-size). Both will allocate for `*num_courses` number of `struct course;` which you can then further work though and allocate for each member pointer. Don't forget to ***validate every allocation***, e.g. `if (!ptr) { perror ("malloc-ptr"); exit (EXIT_FAILURE); }` (or however you would like to handle it.) – David C. Rankin May 26 '20 at 07:37
  • @DavidC.Rankin And then `ptr[i].sections = malloc(ptr[i].num_sections * sizeof(ptr->sections));` Will create an array of member sections in the course structure? But then how would I allocate memory to `float *scores` in the array of `student** sections` I just allocated? That's what I'm confused with. Thanks for your help btw. – Cobra04 May 26 '20 at 07:42
  • Be very careful with `student** sections;` There are ***TWO*** allocations required (first you allocate pointers (as `sizeof(ptr->sections)` does, but with an additional level of indirection, `sections` is a *pointer-to-pointer*) You have also declared a block of `*num_courses` number of `struct courses`. So your first allocation would be `ptr[i].sections = malloc (ptr[i].num_sections * sizeof *ptr[i].sections);` to allocate `ptr[i].num_sections` **pointers**. The you must allocate for `ptr[i].sections[j] = malloc (how_many * sizeof *ptr[i].sections[j]);` `struct students` in the section. – David C. Rankin May 26 '20 at 07:52
  • Which is why using `student *sections;` would simplify things by removing one level of indirection. You would still have `ptr[i].num_section` number of `struct students`, but you would only need a single allocation `ptr[i].sections = malloc (ptr[i].num_sections * sizeof *ptr[i].sections);` and could then utilize the storage for each struct through `ptr[i].sections[j]` without having to further allocate for that as a pointer. – David C. Rankin May 26 '20 at 07:56
  • Two answers you will find helpful are [Difference between char *pp and (char*) p?](https://stackoverflow.com/a/60519053/3422102) and [Pointer to pointer of structs indexing out of bounds(?)...](https://stackoverflow.com/a/60639540/3422102). They have basic pointer handling and allocation discussions that may help the pieces fit together. Last note, the `'*'` goes with the variable, not the type. Why? `char* a, b, c;` does NOT declare 3-pointers to `char` -- it declares one pointer to `char` and two `char`. `char *a, b, c;` makes that clear. – David C. Rankin May 26 '20 at 07:57

1 Answers1

0

here you allocate array of courses: course* ptr = (course*)malloc(*num_courses * sizeof(course));

to allocate the array sections (array of pointers to student): ptr[i].sections = (student **)malloc(ptr[i].num_sections * sizeof(student *));

to allocate each student in the array sections you have to iterate the array and allocate each student:

for(int j=0; i<ptr[i].num_sections; i++)
{
   ptr[i].sections[j]=(student *)malloc(sizeof(student));
}

** you have to check each malloc if it fail or success.