1

Adressing operator inside the printf changes the address of the function pointer from a regular adress to 0x1000. Why this happens and what does it mean ?

#include <stdio.h>
int main()
{
    int (*fp) (int);
    printf("%p\n", (fp));
    printf("%p", (fp));
}

When I run this I get 0x560cb44ce060 0x560cb44ce060 as expected but when ı run this

#include <stdio.h>
int main()
{
    int (*fp) (int);
    printf("%p\n", (fp));
    printf("%p", &(fp));
}

ı get 0x1000 0x7ffcc92c8990 I really can not figured out what does change when I add & operator at the last line.

  • 4
    You don't initialize the pointer, it doesn't point anywhere. Local (non-static) variables *are* uninitialized. They will have *indeterminate* values. – Some programmer dude May 10 '23 at 08:45
  • 1
    Also note that in C++ *using* indeterminate values *in any way*, even printing them, will lead to *undefined behavior*. The code you show are not valid C++ programs. Please remember that C and C++ are two separate and very different languages. – Some programmer dude May 10 '23 at 08:46
  • 5
    `fp` is the function pointer. `&fp` is its address – 463035818_is_not_an_ai May 10 '23 at 08:47
  • 1
    As for the unary `&` operator, think of it as a *pointer-to* operator. `&fp` is a pointer to the variable `fp`, and will have the type `int (**fp) (int)`. – Some programmer dude May 10 '23 at 08:48
  • Does this answer your question? [Pointers in C: when to use the ampersand and the asterisk?](https://stackoverflow.com/questions/2094666/pointers-in-c-when-to-use-the-ampersand-and-the-asterisk) – vvv444 May 10 '23 at 08:51
  • @vvv444 No I ask why does the output of the first printf changes when ı add & to second printf. – A_normal_guy May 10 '23 at 08:57
  • @A_normal_guy Your second program print ***two different values***. Nothing "changes". If you had two different variables, `a` and `b`, would you expect them to have the same value? It's really no difference here. – Some programmer dude May 10 '23 at 09:03
  • @A_normal_guy First, you shouldn't print uninitialized values. You can't expect to get anything reliable form that. And in your case `fp` is uninitialized – vvv444 May 10 '23 at 09:11
  • In memory address 0x7ffcc92c8990 I write the value 0x1000. What is the difference between 0x7ffcc92c8990 and 0x1000? One is the address, and the other is what is written there. – user253751 May 10 '23 at 09:16

2 Answers2

4

In the first case of using printf there is outputted the uninitialized pointer fp (its garbage value)

int (*fp) (int);
printf("%p\n", (fp));`

In the second case of using printf there is outputted the valid address of the pointer fp itself

printf("%p", &(fp)); 

So there is nothing changed. There are outputted two different entities: the value stored in a variable (pointer) and the address of the variable itself.

Pay attention to that the conversion specifier p expects a pointer of the type void * but function pointers may not be converted to the type void *.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
3

Maybe you confonded the address which pointer is pointing to and the pointer's address.

The first %p will output the address fp saved(though it is garbage value because you didn't initialized it), and the second one will output the fp's address.