Home > OS >  Pointer pointing to char type array can be used to show the content of the array without a dereferen
Pointer pointing to char type array can be used to show the content of the array without a dereferen

Time:03-17

int main(int argc, char *argv[])
{
  
  char b[]="Among Us";
  char* string2 = &b[0];
  
  printf("Print address of b\t\t=  %p\n", &b[0]);
  
  printf("Print content of string2\t=  %p\n", string2);
  printf("Print content of string2\t=  %s\n", string2); // why ???
  
  system("PAUSE");  
  return 0;
}

Why does the last printf show us the content of b? Isn't it supposed to show us the address of b but in %s format?

I thought printf("Print content of string2\t= %s\n", *string2); was the correct method of getting the content of b printed out through string2 but apparently it was the wrong way.

CodePudding user response:

Both specifiers %p and %s expect a pointer.

%p will print the value of the pointer.

But %s will use the pointer to print a C string, that is an array of char delimited by '\0'.

If you give *string2, it is not a pointer. It is the value of the object the pointer points to, in your case a char.

Raise the warning level of your compiler to the maximum, and hope that it is smart enough to recognize format strings and to check the arguments.

CodePudding user response:

printf("Print content of string2\t= %s\n", string2);

Will print the entire string even when there is no deference operator before string2, because %s, as defined by the standard, expects a char *, i.e. an address. Refer here

CodePudding user response:

Why does the last printf show us the content of b?

address of first element in an array is an address of the array itself. => you can use string2 as b as well

Variable names don't exist anymore after the compiler runs , program care address of variable

refer this : How are variable names stored in memory in C?

CodePudding user response:

You'd better revisit the memory addresses & pointers chapter of your C book.

Suppose you're running a 32 bits system, which addresses memory using 4 bytes & the memory state of your code is:

// char array "b"
address   value
=======   =====
aaaa:0001 A // hex value 41
aaaa:0002 m // hex value 6d
aaaa:0003 o // hex value 6f
aaaa:0004 n // hex value 6e
aaaa:0005 g
aaaa:0006   // that's a "space" char, hex value 20
aaaa:0007 U
aaaa:0008 s
aaaa:0009 0 // that's a zero, hex value 00

// pointer "string2"
// pointing to "aaaa:0001"
// in reverse order
// depending on endianness of your system
address   value
=======   =====
ffff:0001 01
ffff:0002 00
ffff:0003 aa
ffff:0004 aa
  1. Your first printf will print aaaa:0001 as hex; i.e. the memory address of array b.
  2. Your second printf will print aaaa:0001 as hex; i.e. the values stored in address ffff:0001 through ffff:0004. From the format specifier %p, the compiler will know that it's going to print a memory address and read the 4 bytes & revert them before printing.
  3. Your third printf will print Among Us; From the format specifier %s, the compiler will know that it's going to print a char array & print the chars at the address it was pointed to, until printf reaches to a string terminator which is a 0.
  • Related