Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a pointer to a character point to in C?

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.

like image 250
Manish Giri Avatar asked Jun 09 '14 02:06

Manish Giri


People also ask

What is pointer to a character in C?

A pointer may be a special memory location that's capable of holding the address of another memory cell. So a personality pointer may be a pointer that will point to any location holding character only. Character array is employed to store characters in Contiguous Memory Location.

What does pointer to character mean?

char *p = "abc"; defines p with type "pointer to char" and initializes it to point to an object with type "array of char" with length 4 whose elements are initialized with a character string literal. If an attempt is made to use p to modify the contents of the array, the behavior is undefined.

What does a pointer points to?

As just seen, a variable which stores the address of another variable is called a pointer. Pointers are said to "point to" the variable whose address they store.

Can a pointer point to a type?

The pointers can point to any type if it is a void pointer. but the void pointer cannot be dereferenced. only if the complier knows the data type of the pointer variable, it can dereference the pointer and perform the operations.


2 Answers

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.

like image 184
M.M Avatar answered Sep 20 '22 05:09

M.M


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

like image 27
Orelsanpls Avatar answered Sep 21 '22 05:09

Orelsanpls