Search code examples
cpointersdereferenceoperator-precedenceinvalid-argument

invalid type argument of '->' (have 'int')


I get the error reported below while I am compiling my code. Could you please correct me where I mistaken?

invalid type argument of -> (have int)

My code is as follows:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

typedef struct bundles
    {
    char str[12];
    struct bundles *right;
}bundle;

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    unsigned long N;
    scanf("%lu", &N);
    bundle *arr_nodes;
    arr_nodes = malloc(sizeof(bundle)*100);
    int i=5;
    for(i=0;i<100;i++)
    {
    scanf("%s", &arr_nodes+i->str);
    printf("%s", arr_nodes+i->str);
    }
    return 0;
}

I am facing issues at these lines:

scanf("%s", &arr_nodes+i->str);
printf("%s", arr_nodes+i->str);

Solution

  • You mean

    scanf("%s", (arr_nodes+i)->str);
    

    without parentheses the -> operator was being applied to i instead of the increased pointer, that notation is often confusing, specially because this

    scanf("%s", arr_nodes[i].str);
    

    would do exactly the same.

    You should also, check that malloc() didn't return NULL and verify that scanf() did scan succesfully.