Chris Barry
Chris Barry

Reputation: 4594

How to pass a pointer to a function

I am trying to pass an array pointer to a method of a class, so the method can operate on the array.

However when I am passing it, the address seems to change.

The pointers are member variables.

Basically I am doing this:

unsigned char array[1000];
unsigned char * pointer = array;
printf("p%", &pointer);

setup(pointer);

void setup(unsigned char* pointer){
  unsigned char * p = pointer;
  printf("p%", &p);
}

Upvotes: 1

Views: 296

Answers (9)

cenk
cenk

Reputation: 41

setup(&pointer);//pass with its address

void setup(unsigned char** pointer){// make one for address, one for array
    ...
}

Upvotes: 0

user269597
user269597

Reputation:

pointer and p are separate variables and therefore will have different addresses, despite the fact that they point to the same place. Perhaps you meant to use the dereference operator * rather than the address operator &.

If you want to check that they are both pointing to the same memory address then you should be outputting the pointers themselves, rather than the addresses of those pointers.

E.g.

printf("%p", pointer);

Upvotes: 0

Jim Lewis
Jim Lewis

Reputation: 45135

unsigned char * pointer = array;
printf("%p", &pointer);

You're not printing the address of array here; you're printing the address of pointer. The contents of pointer represent the address of array, so I think you need printf("%p", pointer); here.

Upvotes: 1

Tom
Tom

Reputation: 19302

There are a lot of good answers here, but none that are really illustrating the crucial concept, in my opinion, which is that variables are generally passed by value in C++ unless otherwise specified.

In your function "setup", you're actually asking for the address of the pointer, not the address of the array (which is what the pointer contains.) Since this pointer was passed to your function by value, it is a copy of your original pointer. This is why it has a different address.

Upvotes: 0

Edward Strange
Edward Strange

Reputation: 40895

unsigned char array[1000];
Unsigned char * pointer = array;
printf("p%", &pointer);

You're printing the address of the variable 'pointer'.

Setup(pointer);

You're passing the variable 'pointer' by value to 'Setup'.

Void setup(unsigned char* pointer){

You've accepted a variable by value called 'pointer' so it's been created for you and initialized with the value the function was called with.

Unsigned char * p = pointer;

You've created yet another variable called 'p' that is initialized with the same value held in 'pointer'.

printf("p%", &p);

You're printing out the address of this new variable. It's located in a different place in memory of course because it's not the same variable you first printed.

}

Please review this article: http://crazyeddiecpp.blogspot.com/2010/12/pet-peeve.html

Upvotes: 1

alex
alex

Reputation: 490637

You are making a new variable, and then getting its address.

You want the address of the original pointer in the arguments.

Upvotes: 0

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272802

&pointer is finding the address of the pointer, not the address of the array. You just want printf("%p", pointer);.

Upvotes: 3

Alexander Rafferty
Alexander Rafferty

Reputation: 6233

You are printing the address of the pointer variable. In the first case, it is the address of pointer as a local variable, and in the second it is the address of pointer as a function argument. Logically, they have different addresses, though their value is the same, which is the address of array. You need to remove the & from your printfs.

Upvotes: 3

TIMO
TIMO

Reputation: 11

You're printing the address of the newly-allocated char * p, aren't you?

Upvotes: 1

Related Questions