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
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
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
Why does the pointer p always point to its own memory address as an integer in the following example. I can't see where it is initialized and would guess that it would be a garbage value. Can someone show me why it is not a garbage value. By the way I am compiling this in gcc with -std set to c99.
#include <stdio.h>
int main() {
int *p; int a = 4;
p = &a;
*p++;
printf("%d %u\n", *p, p);
}
Your problem (as the other answers point out) is with *p++;. What that says to do is dereference p then increment the address in p.
From what you are seeing, we can assume p comes directly after a in memory
_________________________________________
|something | a | p | something else |
-----------------------------------------
So what ends up happening is p points to a, then is incremented so it points to itself (or more specifically: p stores the address that p is at).
First you need to print a pointer value with %p, and your code has undefined behavior. You move the pointer one place after a and dereference it.
Your code doesn't illustrate the point you (it seems) wanted, the following will:
#include <stdio.h>
int main() {
int *p; int a = 4;
p = &a;
printf("%d %p %p\n", *p, p, &p);
}
It produces something like:
4 0x7fff5c17da44 0x7fff5c17da48
p points to a then *p is the value of a. The value of p is 0x7fff5c17da44 which is the adresse of a and the address of p (&p) is 0x7fff5c17da48.
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.
#include<stdio.h>
int main()
{
int *p=0;
char *ch=0;
p++;
ch++;
printf ("%d and %d\n",p,ch);
return 0;
}
Output:
4 and 1
I know the char pointer increments as +1 in the address that it is pointing too.
I know the pointer to an int increments as +4 in the address in gcc that it is pointing too.
I know Derefrencing a pointer should be done by the use of * with the pointer.
Queries:
Why is this not giving any garbage value for p and ch as both are pointers and has not assigned any address;
Why is this giving me the address difference that the respective pointer has obtained while incrementing, or is this a undefined behavior.
3.Why is the output 4 and 1?
Pl. Explain.
I have compiled this code on gcc-4.3.4.
Its a C code.
I am sorry if this comes out to be a copy of some question as I was not able to find any such question on stackoverflow.
1.Why is this not giving any garbage value for p and ch as both are pointers and has not assigned any address;
err, you assigned address here > int *p = 0 and char *ch = 0. p contains address 0x00000000 and ch contains the address 0x00000000
2.Why is this giving me the address difference that the respective pointer has obtained while incrementing, or is this a undefined
behavior.
char *ch = 0; means that ch contains the address 0. Incrementing the address using ++ will increment the value by sizeof(char) viz 1. Similarly for integer. p contains the address 0. And using the ++ operator increments the value by sizeof(int) which seems to be 4 on your machine(note, this isn't always true, especially for 64 bit machine).
3.Why this output is 4 1 ? here
Because at first, p contained 0, then incremented by sizeof(type_of(p)) = sizeof(int) = 4 on your machine and ch incremented by sizeof(type_of(ch)) = sizeof(char) = 1.
First, your code is printing pointers as integers. While this is probably what you're trying to do, it is not defined behavior, as it is entirely unportable on platforms where the size of a pointer (in bytes) is not the same as the size of int. if you want to print pointer values, use %p instead.
To answer your questions. You are assigning values to both pointers: 0, which is synonymous with NULL.
Second. The reason you're getting 4 1 is due to the size of an int vs the size of a char on your platform. The char is going to be 1. On your platform, anint is 4 bytes wide. When incrementing a pointer the compiler will automatically move the address it references by the byte-count of the underlying type it represents.
#include<stdio.h>
int main()
{
int *p=0; // int is 4 bytes on your platform
char *ch=0; // char is 1 byte
p++; // increments the address in p by 4
ch++; // increments the address in ch by 1
printf ("%d and %d\n",p,ch);
return 0;
}
EDIT: you're going to get the similar results, but with a supported print statement, do this instead:
#include<stdio.h>
int main()
{
int *p=0;
char *ch=0;
p++;
ch++;
printf ("%p and %p\n",p,ch);
return 0;
}
Output (on my Mac) is:
0x4 and 0x1
As per my knowledge, I have added the answers to your questions inline:
#include<stdio.h>
int main()
{
int x=0,*p=0;
char c = 'A', *ch=0;
x++;
// You have initialized this to 0, so incrementing adds 4 (int pointer)
// Remember, the address '4' means nothing here
p++;
// You have initialized this to 0, so incrementing adds 1 (char pointer)
// Remember, the address '1' means nothing here
ch++;
// You are now printing the values of the pointers itself
// This does not make any sense. If you are using pointers, you would want to know what is being referenced
printf ("%d , %d and %d\n",x,p,ch);
// This will FAIL
// Because, you are now trying to print the data pointed by the pointers
// Note the use of '*'. This is called de-referencing
printf ("%d , %d and %d\n", x, *p, *ch);
// Let p point to x, de-referencing will now work
p = &x;
printf ("%d , %d\n", x, *p); // 1, 1
// Let ch point to c, de-referencing will now work
ch = &c;
printf ("%c , %c\n", c, *ch); // 'A', 'A'
return 0;
}
Hope this helps.