Is this the address of a string pointer?

Debojit Acharjee 455 Reputation points
2023-08-03T10:19:48.38+00:00

In the following program, two character pointers are assigned with character and string but in case of pointer 'a' the address is printed but for 'b' is that the address getting printed or something else?

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
   char x, *a, *b = "Hello";

   x = 'A';
   a = &x;
   
   printf("The value of 'a' is %c\n", *a);
   printf("The address of 'a' is %p\n", a);

   printf("The value of 'b' is %s\n", b);
   printf("The address of 'a' is %p\n", b); // Is this going to print address?

   return 0;
}

Output:

The value of 'a' is A

The address of 'a' is 0061FF17

The value of 'b' is Hello

The address of 'a' is 00405064

Developer technologies | C++
{count} votes

2 answers

Sort by: Most helpful
  1. RLWA32 49,551 Reputation points
    2023-08-03T10:37:44.83+00:00

    Maybe this will help you understand how things work.

    #include <stdio.h>
    
    int main(void)
    {
    	char x, *a, *b = "Hello";
    
    	x = 'A';
    	a = &x;
    
    	printf("The character value stored in the address pointed to by 'a' is %c\n", *a);
    	printf("The address contained in the 'a' pointer variable is %p\n", a);
    	printf("The address of the 'a' pointer variable is %p\n", &a);
    
    	printf("The string literal pointed to by 'b' is %s\n", b);
    	printf("The address contained in the 'b' pointer variable is %p\n", b);
    	printf("The address of the 'b' pointer variable is %p\n", &b);
    
    	return 0;
    }
    

  2. RLWA32 49,551 Reputation points
    2023-08-11T08:16:14.2833333+00:00
       printf("The address of 'a' is %p\n", b); // Is this going to print address?
    

    is the address of 'b' that is being printed is the address of 'H'?

    The above statement does NOT print the address of the 'b' variable. It prints the address contained in the pointer variable 'b'.

    A char* pointer to a string literal contains the address of the first character of the string literal. So the variable 'b' contains the address of 'H", the first character in "Hello".

    Why the printed address of 'b' is different from rest? It shows only numeric - 00405064.

    What was printed was the address contained in the 'b' variable as specified by the printf statement. It did exactly what it was supposed to do.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.