Print the value and address of a pointer defined in a function?

I think this is a very simple thing for the code, but I am having problems with the syntax in C, I just programmed in C ++.

#include <stdio.h> #include <stdlib.h> void pointerFuncA(int* iptr){ /*Print the value pointed to by iptr*/ printf("Value: %x\n", &iptr ); /*Print the address pointed to by iptr*/ /*Print the address of iptr itself*/ } int main(){ void pointerFuncA(int* iptr); return 0; } 

Obviously, this code is just a skeleton, but I wonder how I can get the connection between the function and the main work, and the syntax for printing the address pointed to by iptr itself? Since the function is not valid, how can I send all three values โ€‹โ€‹to the main?

I think the address looks something like this:

 printf("Address of iptr variable: %x\n", &iptr ); 

I know this is a simple question, but all the examples that I found on the Internet just got the meaning, but it was defined as something like

 int iptr = 0; 

Do I need to create some arbitrary value?

Thanks!

+5
source share
5 answers

Read comments

 #include <stdio.h> #include <stdlib.h> void pointerFuncA(int* iptr){ /*Print the value pointed to by iptr*/ printf("Value: %d\n", *iptr ); /*Print the address pointed to by iptr*/ printf("Value: %p\n", iptr ); /*Print the address of iptr itself*/ printf("Value: %p\n", &iptr ); } int main(){ int i = 1234; //Create a variable to get the address of int* foo = &i; //Get the address of the variable named i and pass it to the integer pointer named foo pointerFuncA(foo); //Pass foo to the function. See I removed void here because we are not declaring a function, but calling it. return 0; } 

Output:

 Value: 1234 Value: 0xffe2ac6c Value: 0xffe2ac44 
+7
source

To access the value that the pointer points to, you must use the indirectness operator * .

To print the pointer itself, simply enter the pointer variable without an operator.

And to get the address of a pointer variable, use the & operator.

 void pointerFuncA(int* iptr){ /*Print the value pointed to by iptr*/ printf("Value: %x\n", *iptr ); /*Print the address pointed to by iptr*/ printf("Address of value: %p\n", (void*)iptr); /*Print the address of iptr itself*/ printf("Address of iptr: %p\n", (void*)&iptr); } 

The %p format operator requires the corresponding argument to be void* , so pointers must be made to this type.

+5
source

int* iptr already a pointer, so before writing & you donโ€™t need to

 printf("Address of iptr variable: %x\n", &iptr ); 

Here's how to print the value of a pointer.

 printf("Address of iptr variable: %p\n", (void*)iptr); 

You also have a function prototype for pointerFuncA() in the wrong place, inside main() . It must be outside of any function before it is called.

+1
source
 #include <stdio.h> #include <stdlib.h> void pointerFuncA(int* iptr){ /*Print the value pointed to by iptr*/ printf("Value: %p\n", (void*) iptr ); /*Print the address pointed to by iptr*/ /*Print the address of iptr itself*/ } int main(){ int iptr = 0; pointerFuncA( &iptr); return 0; } 

I think you are looking at something like this, there is no need to redefine the function again basically ....

+1
source

An address is some memory value that is written in hexadecimal notation starting at 0x

/ The value pointed to by iptr /

 printf("Value is: %i", *iptr); 

The address pointed to by the pointer will be the value of the iptr pointer itself

/ type the address iptr points to /

  printf("Address is: %p", iprt); 

/ type the iptr address itself /

  printf("Address of iptr: %p", &iptr ) 
+1
source

Source: https://habr.com/ru/post/1232831/


All Articles