user5771881
user5771881

Reputation: 97

Passing a struct member into functions in C

I'm trying to use one of the members in the struct to pass it through the calculation function(). This function will calculate a new value for my variable. Can you please show me what I have to do to pass my variable into my main function. I also what to keep my three functions. Thanks

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

My prototype:

void calculations_using_struct_values(int i);

My struct:

struct test
{
    int x,y,z;
};

My main:

int main()
{
    calculations_using_struct_values();
    return 0;
}

Initializing the values for my variables:

void struct_values()
{
    test variable;

    variable.x=50;
    variable.y=100;
    variable.z=150;

    calculations_using_struct_values(variable.x);
    return;
}

I stored my variable.x into i for this function to plus it by 5:

void calculations_using_struct_values(int i)
{
    int a=5;
    i += a;
    printf("%d\n",i);
    return;
}

Upvotes: 2

Views: 2214

Answers (4)

Peter K
Peter K

Reputation: 1807

If you want to modify value in place, you need to use pointers:

void calculations_using_struct_values(int *i)
{
    int a=5;
    *i += a;
    printf("%d\n", *i);
    return;
}

...
    calculations_using_struct_values(&variable.x);

Upvotes: 0

weston
weston

Reputation: 54781

Your function can take a pointer to an int.

void calculations_using_struct_values(int *i)
{
    int a=5;
    *i += a;
}

And pass the address of your struct member to the function (&):

void struct_values()
{
    //as before

    calculations_using_struct_values(&variable.x);
}

See: Passing by reference in C

Or you could if needed pass the whole struct:

void calculations_using_struct_values(struct test *s)
{
    int a=5;
    s->x += a;
}

And pass the address of your struct to the function (&):

void struct_values()
{
    //as before

    calculations_using_struct_values(&variable);
}

Upvotes: 2

Johannes
Johannes

Reputation: 6707

Here is a collection of calls that prints the different consequences of calling. Note how address and value behave after passing them to functions and when the functions return. Depending on your compiler references may not be supported since they are not ansi-c.

#include <stdio.h>

void AddByValue(int i)
{
    printf("%8s: address: 0x%X value: %i\n", "Val A", &i, i);
    i = i + 1;
    printf("%8s: address: 0x%X value: %i\n", "Val B", &i, i);
}

void AddByPointer(int *iptr)
{
    //note that you copy the pointer itself by value
    //the value of the pointer is the address of a place in memory
    //using the asterix the compiler can navigate to that memory address
    //using the typeinfo the compiler knows what is supposed to be at that place in memory
    printf("%8s: address: 0x%X value: 0x%X\n", "Point A", &iptr, iptr);
    printf("%8s: address: 0x%X value: %i\n", "Point B", iptr, *iptr);
    (*iptr) = (*iptr) + 1;
    printf("%8s: address: 0x%X value: %i\n", "Point C", iptr, *iptr);
}

void AddByReference(int &i)
{
    printf("%8s: address: 0x%X value: %i\n", "Ref A", &i, i);
    i = i + 1;
    printf("%8s: address: 0x%X value: %i\n", "Ref B", &i, i);
}

struct test
{
    int x, y, z;
};

void PrintStrruct(test t)
{
    printf("%8s: value: %i\n", "test x", t.x);
    printf("%8s: value: %i\n", "test y", t.y);
    printf("%8s: value: %i\n", "test z", t.z);
}

void PassingAStruct(test *testptr)
{
    (*testptr).x = 0;
    testptr->y = 0;
    test & ref = *testptr;
    ref.z = 0;
}

int main()
{
    int i = 1;
    printf("%8s: address: 0x%X value: %i\n", "main A", &i, i);
    AddByValue(i);
    printf("%8s: address: 0x%X value: %i\n", "main B", &i, i);
    AddByPointer(&i);
    printf("%8s: address: 0x%X value: %i\n", "main C", &i, i);
    AddByReference(i);
    printf("%8s: address: 0x%X value: %i\n", "main D", &i, i);

    printf("--- structs ---\n");
    test mytest;
    PrintStrruct(mytest);
    PassingAStruct(&mytest);
    PrintStrruct(mytest);
    AddByValue(mytest.x);
    PrintStrruct(mytest);
    AddByPointer(&mytest.y);
    PrintStrruct(mytest);
    AddByReference(mytest.z);
    PrintStrruct(mytest);
    return 0;
}

Upvotes: 0

user3121023
user3121023

Reputation: 8286

You could have the functions return values rather than void.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct test
{
    int x,y,z;
};

int calculations_using_struct_values(int i)
{
    int a=5;
    i += a;
    printf("%d\n",i);
    return i;
}

struct test struct_values()
{
    struct test variable;

    variable.x=50;
    variable.y=100;
    variable.z=150;

    variable.x = calculations_using_struct_values(variable.x);
    return variable;
}

int main( int argc, char *argv[])
{
    struct test values;

    values = struct_values();
    printf("%d\n",values.x);

    return 0;
}

Upvotes: 1

Related Questions