views:

78

answers:

5

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 the 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);
}
+8  A: 

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.

Mark E
+1 That's right.
karlphillip
Also, a reference to `data` needs to be passed to `add_fields`, and not `data` itself. And the `struct` block needs to end with a semicolon.
strager
I am pleasantly surprised at how quick I got a helpful answer. Thank you very much. I understand now.
Scott S.
+1  A: 

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.

James McNellis
A: 

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?

Kizaru
A: 

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

GWW
+3  A: 

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.

Starkey
You are immensely helpful. Thank you.
Scott S.