1

我在编译代码时收到下面报告的错误。你能纠正我错在哪里吗?

->(have 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);
4

3 回答 3

6

你的意思是

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

没有括号,->运算符被应用于i而不是增加的指针,这种符号经常令人困惑,特别是因为这个

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

会做同样的事情。

您还应该检查malloc()没有返回NULL并验证scanf()扫描是否成功。

于 2015-05-04T11:35:42.220 回答
1

你需要

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

您的原始代码与

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

因为 的->优先级高于+,所以你会得到那个错误。

于 2015-05-04T11:36:50.327 回答
1

根据运算符的优先级->具有更高的优先级+。您需要将代码更改为

scanf("%s", (arr_nodes+i)->str);
于 2015-05-04T11:37:51.813 回答