Reputation: 3692
I am trying to learn pointers in C, and have gone through the concepts. I came across this lab question, and tried to write a solution for it.
/* p1.c
Write a short C program that declares and initializes (to any value you like) a
double, an int, and a char. Next declare and initialize a pointer to each of
the three variables. Your program should then print the address of, and value
stored in, and the memory size (in bytes) of each of the six variables.
Use the “0x%x” formatting specifier to print addresses in hexadecimal. You
should see addresses that look something like this: "0xbfe55918". The initial
characters "0x" tell you that hexadecimal notation is being used; the remainder
of the digits give the address itself.
Use the sizeof operator to determine the memory size allocated for each
variable.
*/
However, I have two major categories of errors when I compile the program-
My format placeholders for the printf statement seem to be all wrong. I was under the impression that memory addresses could be printed with %x, or %p. But in my program, both generated compiler warnings. I didn't understand- why in some of my previous programs, %p worked without any warning, and why both %x and %p did not work here. Can someone please help me out as to which placeholders work with which data types?
The other major problem is in assigning a pointer to a character variable. When I run this program, I get segmentation fault in the third printf statement of the program. I'm not sure why it is so-
If I am correct, a declaration like this-
char array[]="Hello World!"
and char *ptr=array
sets the character pointer variable ptr
to point to the first element of the array variable array
. So, ideally, *ptr
would indicate 'H'
, *(ptr+1)
would indicate 'e'
and so on.
Following the same logic, if I have a character variable var3
with the char 'A'
, then how should I make a pointer variable ptr3
point to it?
Here is the program I wrote-
#include<stdio.h>
int main()
{
int var1=10;
double var2=3.1;
char var3='A';
int *ptr1=&var1;
double *ptr2=&var2;
char *ptr3=&var3;
printf("\n Address of integer variable var1: %x\t, value stored in it is:%d\n", &var1, var1);
printf("\n Address of double variable var2: %x\t, value stored in it is:%f\n", &var2, var2);
printf("\n Address of character variable var3: %x\t, value stored in it is:%s\n", &var3, var3);
printf("\n Address of pointer variable ptr1: %x\t, value stored in it is:%d\n", ptr1, *ptr1);
printf("\n Address of pointer variable ptr2: %x\t, value stored in it is:%f\n", ptr2, *ptr2);
printf("\n Address of pointer variable ptr3: %x\t, value stored in it is:%s\n", ptr3, *ptr3);
printf("\n Memory allocated for variable var1 is: %i bytes\n", sizeof(var1));
printf("\n Memory allocated for variable var2 is: %i bytes\n", sizeof(var2));
printf("\n Memory allocated for variable var3 is: %i bytes\n", sizeof(var3));
printf("\n Memory allocated for pointer variable ptr1 is: %i bytes\n", (void *)(sizeof(ptr1)));
return 0;
}
Any help would be much much appreciated. Thank You.
Upvotes: 5
Views: 2506
Reputation: 23545
printf("\n Address of character variable var3: %x\t, value stored in it is:%s\n", &var3, var3);
printf("\n Address of pointer variable ptr3: %x\t, value stored in it is:%s\n", ptr3, *ptr3);
Here is your problem. Take a look at printf and it argument, precisly :
%s, %s is using to print string.
What is a string?
A string is defined by 0+ characters plus '\0' character.
What is the problem here?
Var3 is declared as char, so not a string.
What printf try to do? Take a look at your memory :
(aa125121)(memory random address) 65 aa aa aa
printf try to read at 65 and succeed (print it)
printf try to read some '\0' or other character value BAMMM segmentation fault
Your program access to a wrong memory address.
Soluce :
use %c
Upvotes: 1
Reputation: 1782
As your task required to use 0x%x
specifier\format then the first string of printing should be like, for example:
printf("\n Address of integer variable var1: 0x%x\t, value stored in it is:%d\n", (unsigned int)&var1, var1);
Next I believe you will understand the same way.
Upvotes: 0
Reputation: 141628
You have it all correct except that the printf specifier for char
is %c
(not %s
).
Following the same logic, if I have a character variable
var3
with the char'A'
, then how should I make a pointer variableptr3
point to it?
See your code for the answer!
Hoewver you ave some type mismatches in other printf statements. In the final one you use %i
for (void *)
, I'm not sure what you were thinking there.
To print the result of sizeof
use %zu
(and don't cast to void *
), this applies to your final four printf statements.
In the other statements you use %x
to print an address. This is wrong (your course is wrong to advise this also). You should use %p
to print an address.
Technically you should also cast the address to void *
, although on modern systems all pointers have the same size and representation so you get away with not doing it.
Upvotes: 6