victory
victory

Reputation: 185

Change int variable pointed by pointer

I am a little bit newbie, and pointers still do troubles to me. I would like to change value of int, which I get from parameter (as a pointer) in function.

#include <stdio.h>

bool function(int* a){

    a++;

    printf("%d",*a); //getting some big number, maybe address. If i did not use a++; I get just normal a, unchanged.

    return false;
}

Upvotes: 2

Views: 11493

Answers (4)

Eduardo Pascual Aseff
Eduardo Pascual Aseff

Reputation: 1166

The problem is that you're incrementing the pointer (not the value pointed with it) in the statement a++. If you want to increment the value of the parameter, you should dereference it first:

(*a)++; //instead of a++;

Printf is not printing exactly an address, is just printing the value of the integer (may be an integer, can be something else) stored next to your parameter a.

Upvotes: 6

sg7
sg7

Reputation: 6298

A pointer is a variable which contains the address in memory of another variable. We can have a pointer to any variable type.

The indirection or dereference operator * gives the contents of an object pointed to by a pointer.

The unary or monadic operator & gives the address of a variable.

In your program you increment the pointer a:

 a++;

That operation does NOT affect the object to which pointer points.

If you want to increment the value of the parameter, you should dereference pointer with the dereference operator * and then increase the value.

(*a)++;

This is a small example how in practice you can change the value of the variable by having a pointer to it.

#include <stdio.h>

void function(int* a){

    (*a)++;          // dereference the pointer `a` and increase the content of an object pointed to by the pointer by 1.
    (*a) = (*a) + 2; // dereference the pointer `a` and increase the content of an object pointed to by the pointer by 2.

    printf("\nprint from function (*a) = %d\n",(*a) ); 
}

int main(void)
{
    int x = 7;
    int *p = &x;             // pointer to variable x 

    printf("x = %d\n", x );  //     x = 7

    function(p); // calling with pointer p as argument

    printf("x = %d\n", x );  //    x = 10

    function(&x); // calling with pointer &x as argument  

    printf("x = %d\n", x );  //    x = 13       

    return 0;
}

Output:

x = 7                                                                                                               

print from function (*a) = 10                                                                                       
x = 10                                                                                                              

print from function (*a) = 13                                                                                       
x = 13                                                                                                              

Upvotes: 3

zainosaurus
zainosaurus

Reputation: 148

You need to dereference the pointer to get the actual value of the integer (not address) by using *a. Then you can increment that value. So your code should be (*a)++ instead of a++ (In your case you are just incrementing the address and trying to access the next location in memory).

Upvotes: 3

HubballiHuli
HubballiHuli

Reputation: 777

do (*a)++ instead. Your function receives a pointer as an argument. If ++ is used with a pointer variable then, for 64 bit int variable, it will increment to 4 byte and will hold that memory address, if there is no value in that location then a garbage value will be printed out which why you are getting that large number. Follow this link to have a basic idea about pointer arithmetic.

Upvotes: 1

Related Questions