This question already has an answer here:
Converting unsigned char array to an integer
(1 answer)
Closed 8 months ago.
#include <stdio.h>
int main(){
char p[5] = "ABCD";
int* ip = (int *)p;
printf("%d \n", *(ip + 0)); // 1145258561
printf("%d \n", ip); // 6422016
}
Could anybody please explain to me the output of this program?
Here you cast the char[4], p, into an int* and initialize ip with the result:
int* ip = (int *)p;
Here you dereference ip, which, since it's an int* means that it will read sizeof(int) bytes to form an int from the address ip points at:
printf("%d \n", *(ip + 0)); // 1145258561
Since ints are often 4 bytes, it will often seem to work, but it violates the strict aliasing rule and results in undefined behavior. Also, if an int is 8 bytes, the program would have undefined behavior since it would then read outside the char[4].
Here you print the value of ip as an int, but it is a pointer, so again, the program will have undefined behavior. Also, a pointer is often 8 bytes so it will likely cause undefined behavior for that reason too.
printf("%d \n", ip); // 6422016
To properly print pointers, use %p and cast the pointer to void*:
printf("%p\n", (void*) ip);
Related
#include <stdio.h>
int main () {
char c = 'A';
int *int_ptr;
double *double_ptr;
*int_ptr = *(int *)&c;
*double_ptr = *(double *)&c;
printf("Original char = %c \n", c);
printf("Integer pointer = %d \n", *int_ptr);
printf("Double pointer = %f\n", *double_ptr);
return 0;
}
The questing is – Why can't I assign the double_ptr using this code, because it causes segmentation fault, but works fine for integer?
As I understand char is 1-byte long and int is 4-bytes long, so double is 8 bytes-long.
By using expression *(double *)&c I expect the following:
& – Get the memory address of c.
(double *) – pretend that this is a pointer to double.
*() – get the actual value and assign it to double var.
Your code has Undefined Behaviour. Therefore anything could happen.
The UB is because you are casting a char which is one byte to types that are 4 and 8 bytes, which means you are (potentially) accessing memory out of bounds, or with the wrong alignment.
Whether any of this will "work" or "not work" on any particular system is not very relevant, because the code is erroneous.
In your program, typecast of char to int* or double* and then a dereference would get some number of extra bytes from memory, which is undefined behavior.
This is the source code
#include<stdio.h>
void main()
{
int *p,a=5;
p=&a;
scanf("%d",p);
printf("%d\t",*p);
printf("%d",a);
}
How can we accept an address of a pointer?.Cuz it has the address of variable 'a' already.There are no errors shown by the compiler.
Also,i'm not able to understand the output.
output:(if my input is 45)
45 45
Your pointer is also a variable like a and has it's own address. You can access it by saying &p. But you say scanf("%d", p); so it is accessing pointer's pointing address.
EDIT: if you want to print pointer's address you can use printf("%p\n",(void *) &p);
You can access pointer itself by its address &p
int pa;
int *p;
scanf("%d", &pa);
p = (int*) pa;
printf("%p\t", p); // printing pointer stored address (got from input)
printf("%d\n", (void *) *p); // printing value stored in address stored in p, of course would be segfault in case of invalid address
Also use it with caution, its unportable and may resulting undefined behavior. it may work on 32bit machines because of int and int* have same length of bits but for other CPU's may not work.
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).
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 */
#include <stdio.h>
struct audiocd {
char title[256];
int trackNo;
char type;
char publisher[256];
};
int main() {
struct audiocd* cdptr;
struct audiocd cdarray[4];
cdptr = cdarray;
printf("%d\n", &(cdarray[2]));
printf("%d\n", cdptr);
}
What is cdarray[2] & cdptr?
EDIT: Thanks, but if printf("%d\n", &cdarray) is 4291520 , is it possible to trace printf("%d\n", &(cdarray[2])) & printf("%d\n", cdptr)?
The overall effect of the program is simply undefined behavior. It's passing addresses to printf, but using the %d conversion, which expects an int. The mismatch causes undefined behavior.
In a typical case that int and a pointer happen to be the same size, it'll print out the address in cdptr and the address of cdarray[2].
If you want to print out those addresses, the obvious way is something like:
printf("%p", (void *)&cdarray[2]); // (void *)cdarray+2
printf("%p", (void *)cdptr);
As for what those expressions "are", they're addresses -- the addresses of the beginning of the array and the third element of the array, respectively.
thanks, but if printf("%d\n",
&cdarray) is 4291520 , is it possible
to trace printf("%d\n", &(cdarray[2]))
& printf("%d\n", cdptr).
If I got your question correctly, I think you want to infer the values of &(cdarray[2]) and &(cdptr) from value of cdarray.
As you assigned cdarray to cdptr, cdptr will store the starting address of the array. Now &(cdarray[2]) is just
cdarray + 2*sizeof(struct audiocd)
Also, as discussed above, %p should be used to print memory addresses instead of %d.