#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.
Related
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 am a first year student in C programming so my skill/knowledge is limited. I am trying to create my own implementation on printf, but I am having trouble with retrieving and printing the address of a variable. With printf, its possible to output the address of a variable with %p, i need to replicate %p somehow.
When storing the address of a variable, the data type is int*, and I cannot figure out how to write this address to the screen(stdout).
For Example:
int i = 123;
int *address = &i;
Now how would I output address(not the value at i)? I have tried using the original printf format specifiers for testing purposes. I tried using %x, %s, %d, %lu... it all gives me an error as I am trying to output an int*(integer pointer).
Can anyone assist me in outputting the address?
You can only inspect the bits of the pointer and print those, using the type unsigned char:
unsigned char* a = ( unsigned char* )address;
for( size_t i = 0 ; i < sizeof( address ) ; i++ )
printf( "%hhu" , a[i] );
Another option, if the pointer is a pointer to an object, is to cast the pointer to types: intptr_t or uintptr_t, but the availability of those types is implementation defined.
If it's for the address of an object you could do the following:
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
int main(void)
{
int a;
uintptr_t uip = (uintptr_t) ((void*) &a);
printf("address of a = 0x%"PRIXPTR, uip);
}
To print the address of a function you need to go as proposed here.
#include <stdio.h>
int main()
{
int i;
int buf[10];
char *p ;
p = 4;
printf("%d",p);
return 0;
}
Output:
4
How come it is 4? I was expecting some address value. Can you please help me understand it?
This is undefined behavior, because %d expects an integer.
The reason why you see this output is that pointers have enough capacity to store small integer numbers, such as 4. If by coincidence the pointer size on your system matches the size of an integer, printf would find a representation that it expects at the location where it expects it, so it would print the numeric value of your pointer.
The proper way to print your pointer would be with the %p format specifier, and a cast:
printf("%p", (void*)p);
I was expecting some address value.
You would get an address value if you had assigned p some address. For example, if you did this
char buf[10];
char *p = &buf[3];
printf("%p", (void*)p);
you would see the address of buf's element at index 3.
Demo.
In this code to print the value of int and char pointer variables, why do I access them differently? For the char pointer I write sampleclient but for the int I write *intid. Why does one use * but not the other?
int main()
{
char client[] = "ABCD";
int intid = 10;
samplepass(&client, &intid);
return 0;
}
void samplepass(char *sampleclient, int *intid)
{
printf("%s %d\n", sampleclient, *intid);
}
This is because %s format specifier expects a char pointer, while %d expects an integer argument. If you want to see the value of the pointers themselves(i.e. the address they point to) use %p.
In C, you can't pass a string (character array) to a function like printf, so you do the next best thing: pass it its address in memory. The function can then treat the memory address like an array using pointer arithmetic. So the way printf reads strings is by taking in a pointer to the string. Then %s displays the dereferenced string (ABCD here), not the pointer address (which you could get by using %p instead of %s).
The integer problem is more straightforward: the * in *intid means 'get the value stored at this address'. That's why it prints out 10, not the memory address.
The 'correct' format specifier to get a memory address is %p. But you could write:
int main()
{
char client[] = "ABCD";
int intid = 10;
samplepass(&client, &intid);
return 0;
}
void samplepass(char *sampleclient, int *intid)
{
printf("%d %d\n", sampleclient, intid);
}
On my machine, the output was
-2140958000 -2140958004
(and those are the addresses in memory of client and intid). Substituting %p for %d gives nicely formatted pointers with a 0x in front and conforms to the standard, so I'd use that.
For an output that looks like:
ABCD 10
change your line
from samplepass(&client,&intid);
to samplepass(client,&intid);
I was trying this code:
int main()
{
char *ch="hello";
printf("%u",&ch);
return 0;
}
From the above printf() statement I have address of ch i.e 65524
My question is Can i find value if any address is given like *(65524) rather than *(&ch)?
Yes, you can:
printf("%d\n", *(unsigned char *) 65524);
If the address does not point to a valid object, you are invoking undefined behavior.