0

I can't seem to pass pointer to memory address. I thought the following code would pass the memory address that ptr points at, to my func(), then when I print out the address within func() it would show the same address as ptr did from within main(), however that's not what I'm seeing. p's address is different from ptr's address.

How do I correctly pass a pointer to functions?

#include <iostream>
using namespace std;

void func(int* p)
{
    printf("p address: %p\r\n", &p);
}

int main()
{
    int* ptr;
    memset(&ptr, 0, sizeof(int));
    printf("ptr address: %p\r\n", &ptr);
    func(ptr);
}
cigien
  • 57,834
  • 11
  • 73
  • 112
haosmark
  • 1,097
  • 2
  • 13
  • 27
  • 1
    First, `ptr` doesn't point to anything. It is not initialised, and `memset(&ptr, 0, sizeof(int));` doesn't make it point to anything in particular. It is an invalid pointer. Note that even copying an invalid pointer is undefined behaviour. Second, you never examine the value of `ptr`. You print the address of `ptr`, and then the address of `p`. They are different objects. Different objects in general have different addresses. Third, `%p` is only good for printing `void*`, and `&ptr` is not `void*`, so this is undefined behaviour again. Fourth, we don't use pointers or `printf` much in C++. – n. m. could be an AI Sep 25 '20 at 17:51
  • Not the only problem with the code but you print the pointer like this `printf("p address: %p\r\n", p);`. You should already have a pointer then `&` will give you a pointer to a pointer which is not what you're looking for here. – john Sep 25 '20 at 17:52
  • Related: https://stackoverflow.com/questions/10240161/reason-to-pass-a-pointer-by-reference-in-c – πάντα ῥεῖ Sep 25 '20 at 17:53
  • Thanks everyone, it helped me debug my code. I use malloc now to init a pointer, seems to be working, and I fixed pointer at a pointer issue. I think I have to deal with them, since I'm trying to pack up a complex struct to send over a network, not sure what else can be done. – haosmark Sep 25 '20 at 18:03

2 Answers2

2

In this declaration:

void func(int* p)

the variable p is indeed a copy of ptr, and so they have different memory addresses. The same rules apply here as they do for an int parameter. There's nothing special about pointers in this context. Don't be confused by the fact that the pointed at values are the same, i.e *p and *ptr are the same. The objects p and ptr are still copies of each other, and have different addresses.

If you want the argument p to have the same address as ptr, accept it by reference:

void func(int* &p)

Of course, they both still point at the same address as well.

cigien
  • 57,834
  • 11
  • 73
  • 112
  • Note: The copying of pointers becomes very important later when you need to pass a pointer into a function to change where it points. This mistake trips up a lot of people writing their first linked list. – user4581301 Sep 25 '20 at 18:02
2

Here's the code I think you are looking for

void func(int* p)
{
    printf("%p\n", p);
}

int main()
{
    int something = 0;
    int* ptr = &something;
    printf("%p\n", ptr);
    func(ptr);
}

Note there is only one & in the whole program when I initialise the pointer in main. It's using & when it isn't needed that was your main problem I believe.

john
  • 85,011
  • 4
  • 57
  • 81