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.
%c
is for chars.%s
is for null-terminated strings. You have a char. – Archaeopteryxsizeof(ptr1)
tovoid *
? – Archaeopteryx