1

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);
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
SPradhan
  • 67
  • 1
  • 8

3 Answers3

6

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.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
1

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.

Arun A S
  • 6,421
  • 4
  • 29
  • 43
1

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

scanf("%s", (arr_nodes+i)->str);
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261