Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print value and address of pointer defined in function?

Tags:

c

pointers

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!

like image 778
austinscode Avatar asked Oct 02 '15 19:10

austinscode


People also ask

How will you print the value and address of a pointer variable in C?

You can print a pointer value using printf with the %p format specifier.

How do I print a function address?

Without using the separate pointer for print the address of the function, You can use the name of the function in the printf . printf("The address of the function is =%p\n",test); For printing the address in the hexa-decimal format you can use the %p .

What is pointer explain with example to print the address of variable using pointer?

A pointer is a variable that stores the address of another variable. Unlike other variables that hold values of a certain type, pointer holds the address of a variable. For example, an integer variable holds (or you can say stores) an integer value, however an integer pointer holds the address of a integer variable.

How to print the value of a pointer in C++?

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.

How do you use pointers in a function definition?

To accept these addresses in the function definition, we can use pointers. It's because pointers are used to store addresses. Let's take an example: Example: Call by reference. When you run the program, the output will be: The address of num1 and num2 are passed to the swap() function using swap(&num1, &num2);.

How to assign a variable to a pointer in C++?

1 Define a pointer variable 2 Assigning the address of a variable to a pointer using unary operator (&) which returns the address of that variable. 3 Accessing the value stored in the address using unary operator (*) which returns the value of the variable located at the address specified by its operand.

How to pass an address as an argument to a function?

In C programming, it is also possible to pass addresses as arguments to functions. To accept these addresses in the function definition, we can use pointers. It's because pointers are used to store addresses. Let's take an example: When you run the program, the output will be:


1 Answers

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", 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
like image 184
Atahan Acar Avatar answered Sep 21 '22 02:09

Atahan Acar