This code prints different values for i and &i and both of them are not equal to 10. Please explain me what those two numbers indicate.
#include<stdio.h>
int main(){
int p=10;
int *i=&p;
printf("%d %d",i,&i);
}
This is what the output look like
i is an integer pointer that will be used to store address of a integer variable. In this case, i is stored in stack area of your main memory, when you print &i it means you print address of the place that storing i. When you print i it means you print value of i( value of i is address of p because you have assign &p to i by this line int *i=&p;). I hope it useful for you.
Here is a modified version of your code with comments in the printfs. Note that I added the third printf to reference your int that is in p
#include<stdio.h>
int main(){
int p=10;
int *i=&p;
printf("'i' = %p is the address of the int stored in variable p\n",(void *)i);
printf("'&i' = %p is the address of the pointer to an int called i\n",(void *)&i);
printf("'*i' = %d is the int that is stored at the location in i which points to p\n",*i);
}
'i' = 0x7ffee4e63abc is the address of the int stored in variable p
'&i' = 0x7ffee4e63ab0 is the address of the pointer to an int called i
'*i' = 10 is the int that is stored at the location in i which points to p
Related
Actually I was reading about pointers and wanted to try something ,so I wrote a small code
int main(){
int x = 10;
int *ptr;
ptr = &x;
printf("%d is stored at address %d\n",x,ptr );
int *c=*ptr;
c=&ptr;
printf("location is %d\n",c);
printf("value of c= %d",*c);
}
the result I expected was value of c would be 10 but instead the value came as the location of x.
output: 10 is stored at address 997523644 location is 997523648 value of c= 997523644
does this problem arise because I didnt pass any location as first?or is it something else or my question is quite silly I know:D,Can anyone help me?
The first thing that is missing in the code that you have provided, is the declaration of ptr. You haven't declared the ptr variable yet, so first you need to do
int *ptr;
, which tells that you need a pointer variable to point to a int variable.
Since you declare another pointer int *c and ptr is already a pointer, you only need to assign int *c = ptr (or c=ptr in case you want to declare int *c seperately first), which will store the value of ptr (location of x) to c. So, doing int *c=*ptr is wrong. Now you can access the value of x by doing derefencing (*c or *ptr), which you already did.
Doing c=&ptr is also wrong, because c is of type int * and &ptr is of type int **. In case you are trying to store the address of ptr, you have to first declare a variable of type int **, for example int **d. Now d=&ptr is valid and d holds a value which is the address of ptr. Dereferencing d (*d) should give you the value that the address of ptr is holding, which is nothing but the address of x (same as the value of c). If you dereference d twice (**d), you should get the back the value of x (10).
int* c = ptr should be enough, you can read it as: c is pointer to integer, c = ptr since ptr is already a pointer this will make both c and ptr point to the same value.
What you are currently doing: c=&ptr; is making c point to the address of ptr, so if you want to access x value you will need to dereference twice, once to get the address of ptr, and once again to get the content of x.
Changed it to this:
#include <stdio.h>
int main(void) {
int x = 10;
int *ptr = &x;
printf("%d is stored at address %p\n",x,ptr );
int *c=ptr;
int d = &ptr;
printf("location is %p\n", c);
printf("value of c= %d", *c);
return 0;
}
As ptr is an address already, you can just do it:
int *c=ptr;
Output is:
10 is stored at address 0x7ffd6127dd54
location is 0x7ffd6127dd54
value of c= 10
To print variable address, you can check this answer:
Stackoverflow Answer
You just need to remove the line
c=&ptr;
The above line means that you're setting C as the location of ptr, which is some random value. So C points to the location of ptr, and ptr points to the location of x. C is a double-pointer
Alternatively, if you keep that line in there, you can make it work by instead printing
printf("value of c= %d",**c);
either solution would get the output you expect
I initialized the pointer with a constant, and I knew its address will be delivered to the pointer. So when I tried to test whether the constant can be print or not, the program crashed. Is that illegal?
#include <stdio.h>
int main()
{
int *i = (int *)1;
printf("The value that i pointer points to is %d\n", *i);
return 0;
}
You seem to think (int *)1 produces the address where the value 1 is stored. It does not.
When a cast such as (int *) is used to convert an integer to a pointer, the result is generally that the value is made into an address.1
Thus int *i = (int *)1; sets i to point to the address 1. Then, when attempting to print *i, your program crashed because 1 was not a valid memory address. (Quite commonly, the first page of memory is kept unmapped so that incorrect uses of the null pointer will crash and reveal a problem rather than allowing the program to continue executing with incorrect data.)
To set i to point to an int with the value 1, you must set it to the address of an int object that has the value 1. One way to do this is:
int n = 1;
int *i = &n;
You can also create an unnamed int with the value 1 using a compound literal:
int *i = & (int) { 1 };
The (int) { 1 } creates a compound literal with the value 1, and the & takes its address, and then i is initialized to that address.
Footnote
1 According to the C standard, the result is implementation-defined. So a compiler could define any result it wants. However, every C compiler I have seen makes the integer value into an address, in one way or another. The resulting address is not necessarily valid to use for accessing an object.
(int *)1; is not a pointer to value 1. but rather the pointer to the memory cell # 1. What you probably want is
#include <stdio.h>
int main()
{
int n=1;
int *i = &n;
printf("The value that i pointer points to is %d\n", *i);
return 0;
}
printf("The value that i pointer points to is %d\n", *i);
You try to dereference the pointer i, but it's value (the value of the pointer) is some value, you set yourself. Can you guarantee that at address 0x0001 is an integer you own? All you can do with such a pointer is print it's pointer value (not the value it points to):
printf("The value of pointer i is %p\n", i);
Given program -
main()
{
int i=10;
int *p;
p=&i;
printf("%d,%d",i,*p);
printf("%u,%u",&i,p);
printf("%d",p);
printf("%p,%p",&i,p);
printf("%u",&p);
*p=50;
printf("%d,%d",i,*p);
}
Let address of p is 265.
Then I know output of first printf statement is 10. And of second printf is 265. But after that I don't know. Please help me.
Edit -
This program from c language notes. Main problem is I don't have computer or laptop to run this program. So I am looking help here.
First change a bit and add \n to formats:
int i = 10;
int *p;
p = &i;
printf ("%d,%d\n", i, *p);
printf ("%u,%u\n", &i, p);
declare i as integer that holds 10 as value
declare p as integer pointer that can points to the address of
an integer variable in Memory.
assign the address of i to p that lead to p points to i.
print i value : 10 and contents of Memory location that p points on it (i)
print address of i and address of p.
(Note that as p is a pointer variable, using p is equal in meaning to &i)
Code Output
10,10
3219586576,3219586576
If I run the following on OS X:
int main (void)
{
int* n; // initialise(declare) pointer
*n = 20; // the value in address pointed to by n is 20
printf("n: %i, n&: %i\n", n, &n);
return 0;
}
I get:
n: 1592302512, n&: 1592302480
Why the differing values?
Why do pointer and &pointer have different values?
The expression &n yields the address of n itself, while n evaluates to the value of the pointer, i.e. the address of the thing it points to.
But note that you have undefined behaviour First of all, because you are de-referencing an uninitialized pointer. You need to make n point somewhere you can write to.
For example,
int* n;
int i = 42;
n = &i;
// now you can de-reference n
*n = 20;
Second, you have the wrong printf specifier for &n. You need %p:
printf("n: %i, &n: %p\n", n, &n);
int* n declares a variable called n which is a pointer to an integer.
&n returns the address of the variable n, which would be a pointer to a pointer-to-integer.
Let's say we have the following code:
int a = 20; // declare an integer a whose value 20
int* n = &a; // declare a pointer n whose value is the address of a
int** p = &n; // declare a pointer p whose value is the address of n
In this case we would have the following:
variable name | value | address in memory
a | 20 | 1592302512
n | 1592302512 | 1592302480
p | 1592302480 | who knows?
In your code
int* n; //initialization is not done
*n = 20;
invokes undefined behavior. You're trying to de-reference (write into) uninitialized memory. You have to allocate memory to n before de-referencing.
Apart form that part,
n is of type int *
&n will be of type int **
So, they are different and supposed to have different values.
That said, you should use %p format specifier with printf() to print the pointers.
Just as an alternative, let me spell this out a different way.
char *ptr;
char c='A';
ptr = &c;
In this code, here's what's happening and what values are found when we qualify ptr in different ways.
ptr itself contains the address in memory where the char c variable is located.
*ptr dereferences the pointer, returning the actual value of the variable c. In this case, a capital A.
&ptr will give you the address of the memory location that ptr represents. In other words, if you needed to know where the pointer itself was located rather than what the address is of the thing that it points to, this is how you get it.
This question already has answers here:
How come an array's address is equal to its value in C?
(6 answers)
Closed 9 years ago.
I tried a code to see what is the difference between &i and i if i is an array. My assumption was that i represents the starting address of the array, and I had no idea what will happen if I print &i as well.
The result was surprising (to me), as both i and &i was the starting address of the array.
#include<stdio.h>
int main()
{
char str[25] = "IndiaBIX";
int i[] = {1,2,3,4};
printf("%i\n", &i);
printf("%i\n", i);
return 0;
}
The result will be:
2686692
2686692
This is the same address.
But if I use a pointer to int:
#include<stdio.h>
#include <stdlib.h>
int main()
{
int *j = (int *) malloc(sizeof(int));
*j = 12;
printf("%i %i %i\n", *j,j,&j);
return 0;
}
The result will be:
12
5582744
2686748
Here I assume the first is the value of the memory area pointed to by j,the second is the address of the memory area pointed to by j, the third is the memory address of the pointer itself. If I print i and &i, why does &i not mean the memory address of the pointer i?
int i[] = {1,2,3,4};
The difference is their type, i has a type of integer array, &i has a type of a pointer of an integer array.
Yeah, both i and &i leads to print the same answer but they are not exactly same,
-> i represents the address of the first element in an array named i.
-> &i represents the address of the whole array(though values of both are same, their types are different)
For more info, please refer this [link]http://publications.gbdirect.co.uk/c_book/chapter5/arrays_and_address_of.html
int ar[10];
ip = ar; /* address of first element */
ip = &ar[0]; /* address of first element */
ar10i = &ar; /* address of whole array */