21

I think this is a really easy thing to code, but I'm having trouble with the syntax in C, I've 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'm wondering how I can get the communication between the function and the main working, and the syntax for printing the address pointed to and of iptr itself? Since the function is void, how can I send all three values to main?

I think the address is something like:

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

I know it's a simple question, but all the examples I found online just got the value, but it was defined in main as something like

int iptr = 0;

Would I need to create some arbitrary value?

Thanks!

0

6 Answers 6

43

Read the 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", (void*)iptr );

  /*Print the address of iptr itself*/
  printf("Value:  %p\n", (void*)&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
Sign up to request clarification or add additional context in comments.

4 Comments

@chux "%x" is for hexadecimal, which can be used for displaying the pointers. The only difference will be a 0x in the beginning.
The C spec does not support ""%x" ... can be used for displaying the pointers". "o,u,x,X The unsigned int argument is converted ..." and "If a conversion specification is invalid, the behavior is undefined."
Pointer size might not be the same as int size.
"%p" is for void *. Although common that int * and void * have the same size and other attributes, that is not specified by C. For a portable answer, any pointer that can be converted to void *, is cast to void * first as in stackoverflow.com/a/32914514/2410359.
9

To access the value that a pointer points to, you have to use the indirection operator *.

To print the pointer itself, just access the pointer variable with no operator.

And to get the address of the 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 it's necessary to cast the pointers to this type.

2 Comments

"%p" matches void *. Most systems behave as hoped with "%p" and int *, yet better to cast to void *. "p The argument shall be a pointer to void." C11dr §7.21.6.1 8
@chux ... and (void**) in the third case??
3

Address are some memory values which are written in hexadecimal notation starting with 0x

/Value pointed to by the pointer iptr/

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

Address pointed to by the pointer will be the value of the iptr pointer itself

/print the address pointed to by the iptr/

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

/print the address of iptr itself/

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

Comments

1

int* iptr is already a pointer, so you don't need the & in front of it when you write

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

This is how to print a pointer value.

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

Also you have the function prototype for pointerFuncA() in the wrong place, being inside main(). It should be outside of any function, before it is called.

4 Comments

That's the address that iptr points to, not the address of iptr itself.
@Barmar agreed, reading more carefully, OP asks "the syntax for printing the address pointed to and of iptr itself"
Look at the comments in the code. He wants to print 3 different things.
Yeah, printing three different values/addresses. Thanks for all your help guys!
1
#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 re-define the function again in the main....

2 Comments

Okay, so that worked for the value. Is there a way to print the address pointed to, and the address of iptr? Would I still just pass in iptr, so no change to main?
In this code you are printing the address of iptr, what do you mean by address pointed to?
0

I had noticed that the address of the pointer was the same for each call of the function given in the examples. It then dawned on me the reason was the address displayed was for the function parameter pointer, not the pointer being passed to the function.

There is nothing wrong with the examples and they work as intended. When I first viewed the code, I thought the intent was to display the address of the pointer that was passed, so this caused some confusion for a few minutes. The results were not matching what I expected. Only when I looked more closely did I realize my mistake. Hopefully this will help someone else.

I modified the code in the examples to provide address of the pointer being passed rather than the address of the parameter. This code was written in an Arduino Sketch.

void pointerFuncA_Original(Print* p, const char* str, int *iptr) {
  p->printf("Value of %s: %d\n", str, *iptr);
  p->printf("Address of %s: %p\n", str, iptr);
  p->printf("Address of pointer to %s: %p\n", str, &iptr);
}

void pointerFuncA(Print* p, const char* str, int **iptr) {
  p->printf("Value of %s: %d\n", str, *(*iptr));
  p->printf("Address of %s: %p\n", str, *iptr);
  p->printf("Address of pointer to %s: %p\n", str, &(*iptr));
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  // Wait for the serial port to connect
  while (!Serial) ;

  int i = 1234;
  int *fooi = &i;
  int j = 5678;
  int *fooj = &j;
  Serial.println("--- Original Function outputs the same address for the pointer passed to the function. ---");
  pointerFuncA_Original(&Serial, "i", fooi);
  Serial.println();
  pointerFuncA_Original(&Serial, "j", fooj);
  Serial.println();

  Serial.println("--- Modified Function Calls showing correct address of the pointer passed to the function. ---");
  pointerFuncA(&Serial, "i", &fooi);
  Serial.println();
  pointerFuncA(&Serial, "j", &fooj);
  Serial.println();

  Serial.println("--- Verify ---");
  Serial.printf("Value of i: %d\n", i);
  Serial.printf("Address of i: %p\n", &i);
  Serial.printf("Address of fooi: %p\n", &fooi);
  Serial.println();
  Serial.printf("Value of j: %d\n", j);
  Serial.printf("Address of j: %p\n", &j);
  Serial.printf("Address of fooj: %p\n", &fooj);
}

void loop() {
  // put your main code here, to run repeatedly:

}

OUTPUT:

--- Original Function Calls same address the pointer sent to the function. ---
Value of i: 1234
Address of i: 0x20041fb8
Address of pointer to i: 0x20041fa4

Value of j: 5678
Address of j: 0x20041fc0
Address of pointer to j: 0x20041fa4

--- Modified Function Calls showing correct address of the pointer sent to the function. ---
Value of i: 1234
Address of i: 0x20041fb8
Address of pointer to i: 0x20041fbc

Value of j: 5678
Address of j: 0x20041fc0
Address of pointer to j: 0x20041fc4

--- Verify ---
Value of i: 1234
Address of i: 0x20041fb8
Address of fooi: 0x20041fbc

Value of j: 5678
Address of j: 0x20041fc0
Address of fooj: 0x20041fc4

The original version displays the address of the pointers to i & j as 0x20041fa4. The modified version and the verification check show the correct addresses of the pointers to i & j that were passed to the function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.