2

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;
}
user5771881
  • 97
  • 1
  • 9
  • Please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve), don't split your program up like you do, don't retype in the question and introduce other errors or calling other functions. Copy-paste an actua[MCVE](http://stackoverflow.com/help/mcve) that actually compiles directly into the question body. – Some programmer dude Mar 09 '16 at 13:01
  • the function: `struct_values` is never called, and is missing a prototype. – user3629249 Mar 09 '16 at 20:20
  • the parameter passed to `calculations_using_struct_values()` needs to be a pointer, not the content. In `main()` the call to that function is missing the parameter. This line: `i += a;` should be: `*i += a;` this line: `printf("%d\n",i);` should be: `printf("%d\n", *i);` – user3629249 Mar 09 '16 at 20:24

4 Answers4

2

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);
}
Community
  • 1
  • 1
weston
  • 54,145
  • 21
  • 145
  • 203
  • In my int main() there is an error occurring in the brackets of calculations_using_struct_values(); – user5771881 Mar 09 '16 at 16:24
  • Well if it's like main in your question, you need to pass an argument to it. – weston Mar 09 '16 at 18:29
  • int main(); //my main prototype /////////////////////////////////////////////////////////////////////////////////// void calculations_using_struct_values(struct test *v) { int a=5; v->x +=a; main(); } ///////////////////////////////////////////////////////////////////////////////////////// int main() { printf("%d\n",); return 0; }/////////////////////////////////////////////////////////////////////////////////////////////////////////////////what do I have to write to pass the pointer into my main function – user5771881 Mar 10 '16 at 06:29
  • I used ////// to separate my functions – user5771881 Mar 10 '16 at 06:34
  • calculations_using_struct_values(&variable); – weston Mar 10 '16 at 18:15
1

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;
}
user3121023
  • 8,181
  • 5
  • 18
  • 16
0

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);
Johannes
  • 6,490
  • 10
  • 59
  • 108
Peter K
  • 1,787
  • 13
  • 15
0

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;
}
Johannes
  • 6,490
  • 10
  • 59
  • 108