1

I'm coming from a dominantly object-oriented programming language and trying to understand the functioning of C a little more so decided to make a small program to do this.

The problem I am having is what is usually addressed with the use of DI, how do I pass the reference of a value to another function so that it can perform arithmetic on it without using globals?

Consider the following program:

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

int doStuff(int v)
{
    v = v + 10; // main->value = 10
}

int otherStuff(int v)
{
    v = v - 10; // main->value = 0
}

int main()
{
    int value = 0;
    int stop = 0;

    do
    {
        printf(" %i should be 0 \n", value);

        doStuff(value); // "value" = 10
        printf(" %i should be 10 \n", value);

        otherStuff(value); // "value" = 0
        printf(" %i should be 0 \n", value);

        exit(0);

        if(value >= 10)
        {
            stop = 1;
        }

    } while(!stop);

}

Output:

0 should be 0
0 should be 10
0 should be 0
Jahee Kaje
  • 13
  • 2

4 Answers4

2

Have a look at this: Passing by reference in C

In your example you are passing the value of the variable, you want to pass the value of the pointer.

Thomas Gak-Deluen
  • 2,759
  • 2
  • 28
  • 38
  • I did look into this but was not exactly sure after reading so decided to post here to clear up my confusion. For example, I'm unsure of the * and & uses, I just know that the * denotes the pointer address. – Jahee Kaje Sep 02 '17 at 17:16
  • @JaheeKaje When you declare a pointer, yes, you can say that * denotes a pointer. But it is also a dereference operator. To put it in the simplest way possible, when you use * on a pointer outside of a declaration, it actually gets the value that the pointer points to. – Nothing Nothing Sep 02 '17 at 20:45
1

pass them as pointers. If you do not return the value - declare functions void, or return something

void doStuff(int *v)
{
    *v = *v + 10; // main->value = 10
}

void otherStuff(int *v)
{
    *v = *v - 10; // main->value = 0
}

and in the main

        doStuff(&value); // "value" = 10
/*....*/
        otherStuff(&value); // "value" = 0

int *v in the function means that v is the pointer to the int object. in the function call &value passes the pointer (address) to the value. Dereferencing the pointer v in the function - all poerations are actuqally done on the value.

0___________
  • 60,014
  • 4
  • 34
  • 74
  • This seemed to do the trick. Do you mind me asking what the & actually does differently? – Jahee Kaje Sep 02 '17 at 17:15
  • @JaheeKaje you basically pass the pointer to value by doing `&value`, the `&` gives the value of the pointer. And then in the function you dereference the pointer so you can modify the actual value – Thomas Gak-Deluen Sep 02 '17 at 17:47
  • Pointer and reference is the same thing. They both refer to the memory address, in which the variable is stored. So, the & operator returns the "memory address of a variable" and the * operator returns the "data that are stored in a memory address". That is why it is called "de-referencing". The use of type when de-referencing helps you ensure you read the correct amount of data, and understand them as the same type that was stored in memory. – Angelos Asonitis Mar 25 '19 at 13:13
0

You should pass pointers to the integers to the functions rather than the integers themselves.

For you function to accept an integer pointer(reference) rather than the integer itself it should be declared like so:

int doStuff(int *v)

The * here indicates that a pointer to an integer will be passed.

Then in the function itself we have to derefernece that pointer to the integer itself:

int doStuff(int *v)
{
    // The * here is dereferncing the pointer to the integer it is pointing to
    *v = *v + 10; // main->value = 10
}

And finally, the main routine should be modified to pass a pointer to value rather than value itself by using the & symbol:

dostuff(&value);
Brian Zammit
  • 179
  • 6
-1

You have to return a value from the function, in your case:

int doStuff(int v)
{
v = v + 10; // main->value = 10

return v;

}

int otherStuff(int v)
{
v = v - 10; // main->value = 0
return v;
}    

And in main you have to save the value returned from the function like that value=doStuff(value);