2015-05-04 155 views
0

我在编译我的代码时遇到下面报告的错误。你能纠正我在我错误的地方吗?的->无效类型参数' - >'(有'int')

无效的类型参数(有int

我的代码如下:

#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; 
} 

我在这行面临的问题:

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

回答

6

你意思是

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

不带括号的->运营商正在应用到i,而不是增加的指针,该符号往往是混乱的,特别是因为这

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

会做完全一样的。

您还应该检查malloc()未返回NULL并验证scanf()确实扫描成功。

+0

感谢iharob。有效。我不知道括号。现在有道理。 – SPradhan

1

你需要

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

你原来的代码是一样的

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

因为->+更高的优先级,让你得到这个错误。

1

根据operator precedence,->优先于+。您需要将您的代码更改为

scanf("%s", (arr_nodes+i)->str); 
相关问题