SPradhan
SPradhan

Reputation: 67

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);

Upvotes: 1

Views: 5099

Answers (3)

Sourav Ghosh
Sourav Ghosh

Reputation: 134336

As per the operator precedence, -> is having higher precedence over +. You need to change your code to

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

Upvotes: 1

Arun A S
Arun A S

Reputation: 7006

You need

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

Your original code was the same as

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

because the -> has a higher precedence than +, so you get that error.

Upvotes: 1

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

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.

Upvotes: 6

Related Questions