What does a pointer to a character point to in C?
Asked Answered
S

3

5

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-

  1. 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?

  2. 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.

Slink answered 9/6, 2014 at 2:12 Comment(6)
%c is for chars. %s is for null-terminated strings. You have a char.Archaeopteryx
"What does a pointer to a character point to in C?" A character.Shults
Why are you casting sizeof(ptr1) to void *?Archaeopteryx
A pointer-to-character points to a single character, much as a pointer-to-int points to a single int, a pointer-to-double points to one double, etc. Any pointer may point to an array of whatever type the pointer is, but that's not something you can determine from the pointer - that's a program logic issue.Jadwigajae
A pointer to a character points to the reference of your character.Mozzarella
@KissaSy - "...points to the reference of your character"? Please explain what you mean. Thanks.Jadwigajae
M
6

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 variable ptr3 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.

Masjid answered 9/6, 2014 at 2:20 Comment(3)
Thank You for this information, it was really helpful. I am really surprised none of the materials I came across so far advised the usage of %zu for printing the result of sizeof operator. This corrected my solution, along with the %c. Just one thing- I never understood why casting to void * was needed for pointers, could you please advise on this too?Slink
The %p specifier expects a (void *), if you give it something else then it tries to read memory as if you had given a void *. If the thing you give it actually walks and talks like a void * then it works out, otherwise it doesn't.Masjid
%zu was added in 1999, some reference materials haven't caught up that far yet (and some compilers coughMSVCcough took a long time to catch up)Masjid
N
1
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

  • Bold is your character
  • aa aa aa is another program memory

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

Noheminoil answered 9/6, 2014 at 2:29 Comment(0)
B
0

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.

Besmear answered 9/6, 2014 at 2:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.