简体   繁体   English

使用指针输入结构

[英]Using pointers to input in a struct

I am trying to get some input from the user to a struct in this function: 我试图在此功能中从用户获取一些输入到结构:

void adding(){
    Item *x = malloc(sizeof(Item));
    printf("Enter an integer.");
    scanf("%d", (x->any));
    printf("Enter a string.");
    scanf("%s", (x->text));
    printf("Which set would you like to add the Item to A/B?");
    while((scanf("%c", inp)) != ("A"||"B")){
        printf("Set does not exist\n");
    }
        if(A == inp)
            add(A, *x);
        else
            add(B, *x);

}

The Item struct is as follows: Item结构如下:

typedef struct anything{
    char text[MAXI];
    int any;
}Item;

The add function which is being called in the end is this one: 最后被调用的add函数就是这个:

void add(Array *S,Item *x){
    bool rep = false;
    int i = 0;
    for(i = 0; i<(S->size); i++){
        if(compStructs(x,S->arr+i))
            rep = true;
    }
    if(rep == false){
            Item *p2 = realloc(S->arr, (S->size+1)*sizeof(Item));
            *p2 = *x;
            (S->size)++;
    }
    else
        printf("The item is already in the set.");

}

Since a runtime error is occuring when the first scanf() is encountered, I think that I'm doing something wrong in the way I'm handling pointers. 由于遇到第一个scanf()时发生运行时错误,因此我认为我在处理指针的方式上做错了。

Compile with all warnings and debugging info (eg gcc -Wall -g ). 编译所有警告和调试信息(例如gcc -Wall -g )。 Learn to use a debugger (eg gdb ). 学习使用调试器(例如gdb )。

Then, you should use the result of scanf(3) . 然后,您应该使用scanf(3)的结果。 It gives the number of really read items. 它给出了真正阅读项目的数量。

And while((scanf("%c", inp)) != ("A"||"B")) is very wrong. while((scanf("%c", inp)) != ("A"||"B"))是非常错误的。 I'm sure the compiler would have warned you! 我确定编译器会警告您!

Should be 应该

char inp = 0;
while (((inp=0),scanf(" %c", inp) == 1) && inp !=  'A' && inp != 'B')

first scanf should give a runtime error. 首先scanf应该给出运行时错误。 scanf expects a location of a variable in which the value read from user would be put. scanf期望一个变量的位置,将从用户读取的值放在该变量中。

instead of scanf("%d", (x->any)); 而不是scanf("%d", (x->any)); you are supposed to write scanf("%d", &(x->any)); 您应该写scanf("%d", &(x->any));

because when you allocate memory via malloc all the bytes would be assigned value = 0. 因为当您通过malloc分配内存时,所有字节都将被赋值为0。

so basically by writing scanf("%d", (x->any)); 所以基本上是通过写scanf("%d", (x->any)); you are saying put value read from user to memory location 0. which is error. 您是说从用户读取到存储位置0的put值。这是错误的。

there is one more error which i have found in your code is when you are calling add() function. 当您调用add()函数时,我在您的代码中发现了另一个错误。

it expects Item * as a second argument. 它希望将Item *作为第二个参数。 while when you are calling it by add(A, *x); 而当您通过add(A, *x);调用它时add(A, *x); and add(B, *x); add(B, *x); you are passing Item and not Item * . 您传递的是Item而不是Item * bcz x itself is a Item * type variable. bcz x本身是Item *类型变量。 so you are supposed to call it like this add(A, x); 所以应该这样称呼它add(A, x); .

Cheers:) 干杯:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM