2015-05-04 101 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); 
相關問題