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!
You can print a pointer value using printf with the %p format specifier.
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 .
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.
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.
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);.
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.
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With