View Single Post
Join Date: Jul 2008
Posts: 6
Reputation: java_girl is an unknown quantity at this point 
Solved Threads: 0
java_girl java_girl is offline Offline
Newbie Poster

Arrays and Pointers

 
0
  #1
Dec 1st, 2008
Hi.

I recently read that using the name of an array without brackets was one method for accessing the <address> of the array's first element. E.g:
  1. #include <stdio.h>
  2.  
  3. int arr[5] = { 2, 4, 6, 8, 10 };
  4.  
  5. int main( void ) {
  6.  
  7. printf( "The address of the '2' is %d\n", arr );
  8.  
  9. return 0;
  10. }
  11.  
  12. char* message1 = "C";
  13. char* message2 = "is the";
  14. char* message3 = "best";
  15. char* message4 = "programming";
  16. char* message5 = "language!";
  17.  
  18. puts( message1 );
  19. puts( message2 );
  20. puts( message3 );
  21. puts( message4 );
  22. puts( message5 );
  23.  
  24. /* message1 points to the 'C' and using puts (message1) will */
  25. /* display the letter as opposed to the address. Why is this? */
  26. /* I know the puts() function receives a char pointer as an */
  27. /* argument. Is this why one need not dereference it by */
  28. /* using *message1 unless one is using */
  29. /* printf( "%c\n", *message1 ); */
  30. /* Essentially, I'm slightly confused by when one pointer is */
  31. /* pointing to the address of the first element in an array and when */
  32. /* it is pointing to the first value */
  33.  
  34. return 0;
Thanks for the help,

java_girl
Last edited by Narue; Dec 1st, 2008 at 3:41 pm. Reason: fixed code tags
Reply With Quote