开发者

How to pass a pointer to a function

I am trying to pass an array pointer to a method 开发者_JAVA百科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);
}


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


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.


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


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


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.


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

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


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.


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);


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜