0
struct Total
{
    char Name[40];
    int No[40];

};
struct Total *List = NULL;

I have this struct which I want to allocate memory for it dynamically.

List =(struct Total*)realloc(List,n*sizeof(struct Total));

But whenever I use the notation List.Name it gives error saying I should use -> is there a way to do this without using -> this ?

Carl
  • 29
  • 9
  • 3
    You can also use `(*List).Name` or `List[0].Name`. – Ian Abbott Nov 03 '20 at 14:51
  • 2
    `List` is a pointer to the first element of your allocated `Total` structs, hence you need to use `->` to access members. – MorningDewd Nov 03 '20 at 14:52
  • 1
    Yes, you need to use `->`. What's wrong with it? – Jabberwocky Nov 03 '20 at 15:08
  • 1
    OT: `List =(struct Total*)realloc(List,n*sizeof(struct Total));`: 1: the `(struct Total*)` cast is useless, just drop it. 2: Be aware that `realloc` is used to _change_ the size of an existing memory region. As you didn't show much of your code, I can't tell if it's correct in your case. – Jabberwocky Nov 03 '20 at 15:11
  • I feel I saw this code already. Note well: `char` is not a string but a single character. Is that what you need? – numzero Nov 03 '20 at 15:13
  • Here is the previous question, for context: https://stackoverflow.com/questions/64652352/reading-from-txt-file-into-a-struct/64652526 – numzero Nov 03 '20 at 15:14
  • 2
    You never *have* to use `->`, since `a->b` is just syntactic sugar for `(*a).b`. But `->` is easier to read. Similarly for the `[]` operator: `a[b]` is syntactic sugar for `*(a + b)`, but again, `[]` is easier to read (provided it's not abused by placing the pointer in the brackets and the offset outside of them). – Tom Karzes Nov 03 '20 at 15:47

0 Answers0