fork download
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.  
  6. char name[] = "Tim"; // array initialized to string literal,
  7. // array name variable stored in the stack frame
  8.  
  9. char *namePtr = "Tim"; // pointer variable (stored in stack frame)
  10. // initialized to a string literal (static data area)
  11.  
  12. // array name = address of the first element
  13. printf ("Addresses:\n\tStarting address of the Array (Stack Frame): %p \n", name);
  14. printf ("\tAddress of pointer variable (Stack Frame): %p \n", &namePtr);
  15. printf ("\tAddress (contents) in the pointer (String Literal ");
  16. printf ("location in the Static Data Area): %p\n", namePtr);
  17.  
  18. printf ("First Characters in each: %c and %c\n", name[0], *namePtr);
  19. printf ("Second Characters in each: %c and %c\n", name[1], *(namePtr+1));
  20. printf ("Third Characters in each: %c and %c\n", name[2], *(namePtr+2));
  21.  
  22. printf ("Strings in each: %s and %s\n", name, namePtr);
  23.  
  24. printf ("Can only change the contents of the array, not the string literal via a pointer\n");
  25.  
  26. name[0] = 'K'; // change Tim to Kim
  27.  
  28. // can't change the string literal ... such as *namePtr = "Bob";
  29.  
  30. printf ("Strings now in each: %s and %s\n", name, namePtr);
  31.  
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0s 5300KB
stdin
Standard input is empty
stdout
Addresses:
	Starting address of the Array (Stack Frame): 0x7fff96b7fb34 
	Address of pointer variable (Stack Frame): 0x7fff96b7fb28 
	Address (contents) in the pointer (String Literal location in the Static Data Area): 0x55a4d839e004
First Characters in each: T and T
Second Characters in each: i and i
Third Characters in each: m and m
Strings in each:  Tim and Tim
Can only change the contents of the array, not the string literal via a pointer
Strings now in each:  Kim and Tim