hamza
hamza

Reputation: 2824

Is this C code wrong?

I know that pointers contain the addresses of variables, for example:

int c = 5;
int *p;  
p = &c;
printf("%d",*p); // Outputs 5.

But what if I want to send an address to a function:

void function (int *p)
{
  p++;
}
int c;
function (&c);

When function is called, the value of &c is assigned to int *p. I guess that the exact instruction is: *p = &c;, but I don't understand what this means.

Upvotes: 2

Views: 765

Answers (6)

t0mm13b
t0mm13b

Reputation: 34592

*p = &c; means:

Get the address of c in memory which is determined at compile-time link-time (Thanks Mikeage) e.g, 0x1234 as an example. The pointer to type int, i.e. *p is assigned to 0x1234, which does not sound right to me, I think you should be doing it this way, p = &c then *p would have the value pointed to by the address of c. And therefore *p would have the value of what c was assigned to (that is called dereferencing - by putting a * before the pointer variable then you can obtain the value).

In your function you are trying to increment the value pointed to by p.

void function (int *p)
{
  *p++;
}

// It would be called like this
int c = 5;
function(&c); // Now c would have 6!

The key point is you want to pass by reference which is what the parameter &c does. If you omit the passing by reference like this:

void function (int p)
{
  p++;
}
// And call it like this
int c = 5;
function(c); // c is still 5

Now c would still be 5, but within the function itself c is 6, but no copy is passed back out as it is incremented within the local stack for the function itself.

Upvotes: 4

Alok Singhal
Alok Singhal

Reputation: 96131

Others have answered your question. I will just add that a helpful way of reading pointer declarations is this. Let's say I have the following declaration:

int i;

This is really easy. Here, i is an int. Now, let's say I have this:

int *pi;

Here, you can either say that pi is a pointer to an int (which is correct). Another way to look at it would be that *pi is an int, i.e., when you have *pi in your code, that *pi will be of type int. Having understood that, I think it's easy to see that ++pi or pi++ doesn't increment the int, because pi is the wrong type. You want (*pi)++. The parentheses are needed because in C, the operator ++ has higher precedence than the unary operator *. Since you don't use any of the side effects of the increment, you can as well do: ++*p.

Of course, as with all the pointers, pi has to point to something useful for the above to make sense.

Upvotes: 1

Steve Jessop
Steve Jessop

Reputation: 279245

If you want the function to increment c when called with &c, then write this:

void function(int *p) { ++(*p); }

function(int *p) means that p is the function parameter. So whatever value the caller gives, that will be assigned to p (not to *p).

The type of p is int *. That is, p is a pointer-to-int.

If p is a pointer-to-int, then *p is the int it points to.

c is an int. Therefore &c is a pointer-to-int, and the int it points to is c. Therefore, if p is &c, then *p is c.

With my version of the function, this code:

int c = 5;
function(&c);

Does the same as this code:

int c = 5; // same as before
int *p;    // parameter of the function
p = &c;    // assign the caller's value to the parameter
++(*p);    // body of the function, increments *p, which is the same as c.

Which does the same as this code:

int c = 5;
++c;

Upvotes: 3

Skilldrick
Skilldrick

Reputation: 70819

I prefer to write int* p = &c;, that is, a pointer to type int is being assigned the address of c.

Pointer notation can be confusing, as you've noticed, because these two are equivalent:

int *p = &c;

and

int *p;
p = &c;

Hope this helps!

EDIT: The confusion comes because * is used both as the dereference operator, and for declaring a pointer. So when p contains the address of c, *p dereferences the pointer and returns the value of c. But when you say int *p = &c;, the * is saying "p is a pointer to an int", but is not doing any dereferencing.

Upvotes: 4

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143081

I can't really make out your question, but the function looks like bug to me and what you meant is, perhaps,

++(*p)

Otherwise it's a no-op.

Upvotes: 3

Amber
Amber

Reputation: 526583

Are you sure p++; is what you mean to do in your function? That's incrementing the location pointed to by p, it's not incrementing the value at the current location. If you're wanting to increment the value (i.e. make c become 6), then what you want is (*p)++;.

Upvotes: 2

Related Questions