Invalid type argument of -> C structs

后端 未结 2 1417
小鲜肉
小鲜肉 2020-12-05 13:07

I am trying to access items in an array of structs and print the structs fields as follows

printList(Album *a, int numOfStructs)
{
    int i;
    int j;

            


        
相关标签:
2条回答
  • 2020-12-05 13:49

    a is of type Album* which means that a[i] is of type Album (it is the ith element in the array of Album object pointed to by a).

    The left operand of -> must be a pointer; the . operator is used if it is not a pointer.

    0 讨论(0)
  • 2020-12-05 13:50

    You need to use the . operator. You see, when you apply a * to a pointer, you are dereferencing it. The same goes with the []. The difference between * and [] is that the brackets require an offset from the pointer, which is added to the address in the pointer, before it is dereferenced. Basically, these expressions are identical:

    *ptr == ptr[0]
    *(ptr + 1) == ptr[1]
    *(ptr + 2) == ptr[2]
    

    To connect to your question: Change a[i]->field2 and a[i]->field3 to a[i].field2 and a[i].field3.

    0 讨论(0)
提交回复
热议问题