开发者

New to C and C Structures -- How to take a struct as a parameter in a function

开发者 https://www.devze.com 2023-01-15 23:45 出处:网络
New to StackOverflow and new to C. I\'m trying to take a struct as a parameter in a function \'add_fields\', which adds the first two int fields \'a\' and \'b\' and puts the result in int field \'c\'.

New to StackOverflow and new to C. I'm trying to take a struct as a parameter in a function 'add_fields', which adds the first two int fields 'a' and 'b' and puts the result in int field 'c'. Not getting anything from th开发者_开发问答e compiler, so obviously I'm doing something wrong. I just don't know what. Any help would be appreciated.

    #include <stdio.h>

    struct add{
    int a;
    int b;
    int c;
    }

    void add_fields(struct add *d){
    d->c = a + b;

    }

    main(){

    struct add data;
    data.a = 1;
    data.b = 2;
    data.c = 0;
    add_fields(data);
    printf("%d + %d = %d", data.a, data.b, data.c);
}


You're very close, but variables a and b don't exist in that context, rather you need to access the fields via the pointer to struct d:

d->c = d->a + d->b;

Second, you need to pass a reference to the struct (since add_fields expects a pointer) when you call add_fields:

add_fields(&data);

The & indicates that you're going to pass the address of the variable data rather than the value.


Here is your corrected code:

#include <stdio.h>

struct add{
    int a;
    int b;
    int c;
};

void add_fields(struct add *d){
    d->c = d->a + d->b;
}

int main(){

    struct add data;
    data.a = 1;
    data.b = 2;
    data.c = 0;
    add_fields(&data);
    printf("%d + %d = %d", data.a, data.b, data.c);

    return 0;
}
  • You forgot a semicolon after the struct definition.

  • I had to fix your add_fields function. You didn't use your struct for a and b.

  • You needed to pass a pointer into add_fields (hence the &) in the function call. By passing a pointer in, your changes in add_fields were reflected on the outside calling struct.

  • main always returns an int.


There are several issues:

In main, data is of type struct add. When you call add_field, you need to pass it a struct add* (that is, a pointer to a struct add). To get a pointer to data in main, you need to use the & operator:

add_fields(&data);

In add_fields, you need to use the "member access syntax" (the ->) to access a and b.

Two other minor issues: main should have a return type of int:

int main() { ...

and you need to place a semicolon after the closing } of a struct definition.


C is call by value language. When you pass data, you are passing a copy of the object of type struct add. In your add_fields field, you are accepting a pointer to the struct. Since you want to change the fields of the struct, you should pass the address of the data struct (and accept a pointer to it in add_fields). To do this,

add_fields(&data);

Also, in add_fields, you aren't have undefined variables (a and b). I assume they should be from the struct, as well?


You should be passing the memory address of the struct to the add_fields function.

add_fields(&data)

** Wow I was really slow on this one :P

0

精彩评论

暂无评论...
验证码 换一张
取 消