Print value and address of pointer defined in function? - c

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!

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

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.

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 )

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.

#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....

Related

Difference in ptr,&ptr,&ptr[0]

Below is the test code:
#include <stdio.h>
int funtion(int *ptr)
{
int temp;
temp = *ptr;
printf("ptr = %p %p %p %d\n", ptr, &ptr, &ptr[0], *ptr);
printf("*ptr = %d\n", *ptr);
return temp;
}
int main()
{
int i = 10;
int *tp = &i;
printf("tp = %p %p %p %d\n", tp, &tp, &tp[0], *tp);
(void)funtion(tp);
return 0;
}
answer:
tp = 0x7ffc1117392c 0x7ffc11173930 0x7ffc1117392c 10
ptr= 0x7ffc1117392c 0x7ffc111738f8 0x7ffc1117392c 10
*ptr =10
**question:
what are the difference between tp &tp &tp[0]
Which passing parameter we should use.
is this difference only for passing parameter?**
You are passing an int pointer to function. This is passed by value. Of course, the value they point to is the same.
int funtion(int *ptr)
{
int temp = *ptr;
printf("ptr = %p %p %p %d\n", ptr, &ptr, &ptr[0], *ptr);
printf("*ptr = %d\n", *ptr);
return temp;
}
temp is initialized with the value pointed to by ptr.
The first thing you print is ptr: the memory address you passed to function by value.
The second thing you print is &ptr. This is the address of that function argument. While this may be the same between function calls due to implementation-specific handling of the stack, it may also be different.
The third thing you print is &ptr[0]. This is the address is ptr[0], which is equivalent to writing *(ptr + 0) or just *ptr. Getting the address of this in turn is equivalent to just writing ptr.
The fourth thing you print is *ptr. Again, as with initializing temp this is just the int value that ptr points to.
1. what are the difference between tp &tp &tp[0]
Its their type.
The type of tp is int *.
The type of &tp is int **.
The type of &tp[0] is int * because &tp[0] is equivalent to tp1) :
&tp[0] -> &(tp[0]) -> &(*(tp) + (0)) -> &(*tp) -> tp
Note : You do not need to use [0] with pointer to access the value of a scalar type, which the pointer is pointing at.
2. Which passing parameter we should use.
Its not clear, what exactly you want to ask.
You are passing value of tp to function() function which is nothing but &i. The ptr parameter of function() function will hold the &i when it is called. If you want to make changes to the value of i within the function() function then you can do it because ptr hold address of i and *ptr will give value at that address which is nothing but the value of variable i. If you just want to access the value of i, you can simply pass the value of variable i.
3. is this difference only for passing parameter?
Which difference? I believe, you mean - why there is difference in &tp and &ptr?
ptr is local variable of function() function and tp is local variable of main() function. Though, they both hold the address of variable i their own addresses are different because they are different variables.
1). C Standards#6.5.2.1
The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2)))..

Can anyone explain why the address of a pointer change when called by another function? How to make nPtr=Ptr?

I noticed that my pointer address is different when it is passed to a function. U am not sure to access the value that exists inside the function? Please don't suggest returning the pointer value from the function. I strictly want to use void function.
output:
&nPtr = 6422300
&Ptr = 6422272
&nPtr = 6422300
See the below code:
#include<stdio.h>
void myFunc(int *Ptr){
printf("&Ptr = %d\n", &Ptr);
};
void main(){
int *nPtr;
printf("&nPtr = %d\n", &nPtr);
myFunc(nPtr);
printf("&nPtr = %d\n", &nPtr);
}
Let me be more clear. Here is the code that I am trying to fix. All what I want is to make the two pointers point to the same address. I want to use the best coding practice.
#include<stdlib.h>
#include<stdio.h>
void myFunc(int *Ptr){
Ptr = malloc(10*sizeof(int));
printf("*Ptr = %d\n", *Ptr);
};
void main(){
int *nPtr;
printf("*nPtr = %d\n", *nPtr);
myFunc(nPtr);
printf("*nPtr = %d\n", *nPtr);
}
In main(), you're getting the address of nPtr in main()'s stack frame; in myFunc(), you're getting the address of the copy of nPtr that is being passed by value to myFunc() as Ptr (so you're actually getting the address of Ptr, which is distinct) and exists on myFunc()'s stack frame.
nPtr and Ptr are separate variables in separate functions, and as such each must necessarily have a different address, i.e. &nPtr == &Ptr will always be false.
But you asked about nPtr == Ptr, i.e. can they contain the same value, and as pointers they can both contain the same pointer value. For example:
#include<stdio.h>
void myFunc(int *Ptr){
printf("Ptr = %p\n", (void *)Ptr);
};
int main(){
int x;
int *nPtr = &x;
printf("nPtr = %p\n", (void *)nPtr);
myFunc(nPtr);
printf("nPtr = %p\n", (void *)nPtr);
}
Output:
nPtr = 0x7ffe9a615104
Ptr = 0x7ffe9a615104
nPtr = 0x7ffe9a615104
For this function
void myFunc(int *Ptr){
printf("&Ptr = %d\n", &Ptr);
};
when you call it, although you are passing a pointer (which is for pointing to an integer) to this function , the system is creating a copy of the pointer and of course they will be at different memory address.
Hence when you return back to the main and retrieve your original pointer address, it will be displaying back the original address

C memory stack and heap

#include <stdio.h>
#include <stdlib.h>
int main (void) {
int a = 5, *ptr;
ptr = &a;
printf("0x%X\n", ptr);
printf("%p\n", &ptr); //Why are these the same?
printf("%d\n", *ptr);
ptr++;
printf("0x%X\n", ptr);
printf("%p\n", &ptr); //Why are these the same?
printf("%d\n", *ptr);
return 0;
}
Why are those two values the same? Since I incremented the pointer shouldn't the address change as well?
you incremented ptr's value. It still resides in the same place in memory. &ptr tells you its address. This is no different than doing something like a++. If you print out a's value, it will now be 6, but if you print out the address of a with printf("%p\n", (void*)&a);, it will be the same before and after the increment.
5 is the value of a and &a is the address of a.
ptr = &a means address of a equals the value of ptr.
&ptr is the address of ptr.
You can think of ptr as a box that can hold an address as it's value.
When you increment ptr you change the address held inside the box i.e. ptr; not the address of the box itself.

C - Pass By Value and Pass By Reference Inconsistency

When using pass by reference in C, sometimes the address passed to the called function changes:
void mainFunction()
{
BYTE aAddress[10];
readFunction(aAddress); // Address of aAddress: 0x111111
}
void readFunction(BYTE *pAddress) // Address of pAddress: 0x222222
{
...
}
Another issue I encountered, when using pass by value, the value received by the called function differs from what is passed to it.
void mainFunction()
{
readFunction(0x20); // Passed value: 0x20
}
void readFunction(BYTE uValue) // Received value: 0x00
{
...
}
It all depends: How did you print the addresses? If you get the address of the variable, then of course they will be different. The value the pointers point to, however, will be the same::
int main ( void )
{
char some_array[100];
printf("array holds: %p\n", (void*) some_array);//same
printf("Address in main: %p\n", (void*) &some_array[0]);//same
pass_arr(some_array);
return 0;
}
void pass_arr(char *arr)
{
printf("Address of pointer VAR: %p\n", (void *) &arr);//PRINTS DIFFERENT ADDRESS
printf("Pointer address: %p\n", (void *) arr);//same
printf("points to: %p\n", (void *) &(*arr));//same
}
check this codepad
You probably failed to print the address of a variable correctly, the only right way is to use the %p placeholder, and to cast the address to void *. This is down to the fact that the way addresses are printed out is implementation dependent.
That said, you mention "pass by reference". C doesn't pass by reference, it doesn't have references in the C++ sense. But in order for a function to alter the value of something else, you ought to pass a pointer to a pointer:
struct some_str
{
int mem1;
size_t mem2;
};
int main ( void )
{
struct some_str *foo = malloc(sizeof *foo);
set_value(&foo);
free(foo);
return 0;
}
void set_value(struct some_str **change)
{//pointer to pointer
static int change_count = 0;
(*change)->mem1 = 123;
(*change)->mem2 = ++change_count;//for example
}
C always passes by value, in case of a pointer, that value is a memory address, in case of a primitive type (int, char, long...), it's not the variable but the value you're passing, if you pass a pointer to the variable (&my_int), you're not passing the variable, but its address in memory. Those are the only 2 options you have to choose from.
For more on the subject (arrays vs pointers, and why arrays decay into pointers in most cases you may find this link useful)
When using pass by reference in C, sometimes the address passed to the called function changes
--> NO. Look at this code
#include <stdio.h>
void readFunction (int *pAddress)
{
printf("Address of pAddress: %p\n", &pAddress);
printf("Content of pAddress: %p\n", pAddress);
}
void main()
{
int a[10];
printf("Address of a: %p\n", &a);
readFunction(a);
}
when execute it, you will get something as:
Address of a: 0xbfb94948
Address of pAddress: 0xbfb94930
Content of pAddress: 0xbfb94948
The pAddress is located in another place but it still points to the address of a. And this is the primitive of pass by reference

What is the meaning of printf("%p", int 1)?

I'm trying to understand the difference between int a and int *a, my first step was to see the value I could get by printi %p of an int a. Of course the compiler shows warnings, but does complete the job for the following code.
#include <stdio.h>
int main() {
int a;
printf("a - declared");
printf("int a = [%d]\n", a); // example - 1745899614
printf("int a pointer = [%p]\n", a); // example - 0x6810505e
a = 10;
printf("a - initialized to value of 10\n");
printf("int a = [%d]\n", a); // exmaple - 10
printf("int a pointer = [%p]\n", a); // example - 0xa
return 0;
}
And as I've mentioned in the source code, I do get a somewhat satisfactory result of 0xa which is equal to 10 in hexadecimal for the value of %p of an int a. But is it actually the case that int points to to that address, or is this just the compiler trying to make sense of %p in such a case?
Where is the memory allocated for ints? How do I test for that?
To print the address of an object named a, use:
printf("The address of a is %p.\n", (void *) &a);
Merely using %p does not tell printf to print the address of the object you use as the argument. You must use the “address of” operator, &, to take the address. Otherwise, you are passing the value of a to printf, not the address of a.
Additionally, it is proper to convert the address to void *, as shown above, because the %p specifier expects a pointer to void. Other types of pointers often work (or appear to work) in many C implementations, but the technical requirement is that a pointer to void be passed.
I imagine the formater ( in printf ), is just interpreting the memory as it is told to. So yeah, "%p" is for pointer, but you gave it an int. You wanted to give it the address of a:
printf( "%p", &a );
for the whole shabang:
int a = 10;
int *b = &a;
printf("value of a: %d\n", a );
printf("location of a: %p\n", &a );
printf("value of b: %p\n", b );
printf("location of b: %p\n", &b );
printf("dereference b: %d\n", *b );
But is it actually the case that int points to to that address, or is
this just the compiler trying to make sense of %p in such a case?
It's the latter. Compiler tries to interpret the integer as a pointer. When you print the value of a using %p compiler finds that the type of a is int and warns you that it's not a pointer.
To print the address of a use:
printf("int a pointer = [%p]\n", (void*)&a);
If a is a pointer (e..g int *a;) then you need to initialize it with a valid address and then you can print:
printf("int a pointer = [%p]\n", (void*)a);
%p is merely a way to tell printf to print your value as an address memory. You're passing the value of 10to it (the value of a) and you get printed this value in the hexadecimal notation 0xa. There is no special interpretation, it is just a formatting option.
If you want the value of the a's address memory printed you can simply do printf("%p", &a);. &a is the address of a.
Or if you want to use a pointer:
int* p;
p = &a;
printf("%p", p); //Prints the p value, that is the a address. Equivalent to printf("%p", &a).

Resources