RSS Forums RSS

Arrays and Pointers

Please support our C advertiser: Programming Forums
Reply
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

  #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 2:41 pm. Reason: fixed code tags
AddThis Social Bookmark Button
Reply With Quote  
Posts: 77
Reputation: mahlerfive is an unknown quantity at this point 
Solved Threads: 16
mahlerfive mahlerfive is offline Offline
Junior Poster in Training

Re: Arrays and Pointers

  #2  
Dec 1st, 2008
You are right that the array name is the address of the first element of that array. This works exactly the same for arrays of char as well.

In C, there is no "string" type, so we are forced to use arrays of char. There is one special difference between a string (array of char) and other arrays, and that is that the last element of a string MUST be the null terminating character '\0'. This signifies the end of the string.

The puts() function will take a char*, assume it is a string ( an array of char), and output all the chars in the array up to the null terminating character.

By using printf in the way you are using it, you are telling it to output one character (%c) and giving it the first character of the string (since you dereference the array, giving you the first element). If you want to print the whole string, use %s and don't dereference the array. You could also print any single character within a string by just giving the index of the character. For example:
  1. printf( "%c\n", message2[3] ); // this will print 't'
Reply With Quote  
Posts: 2,000
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 331
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Arrays and Pointers

  #3  
Dec 1st, 2008
Some additions:
1. To print pointer value use %p (not %d) format specifier. As usually, it's a senseless operation .
2. True type of a string literal is const char* - a constant pointer to the 1st element of a char array contained a string with terminated null character. You can't change string literal via pointer to it. Constructs like
  1. char* message1 = "C";
are valid (for backward compatibility with older C programs) but modern compilers place literals in read-only memory.
3. An array name is not converted to a pointer to the 1st element of an array (&array[0]) in sizeof array_name (operator sizeof argument position).
Last edited by ArkM : Dec 1st, 2008 at 5:04 am.
Reply With Quote  
Posts: 271
Reputation: Luckychap is an unknown quantity at this point 
Solved Threads: 35
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz in Training

Re: Arrays and Pointers

  #4  
Dec 1st, 2008
if u want to print the address of first character in the string it is same as you did with other array. For Ex.

  1. int intArray[3] = {1,2,4};
  2. char *str = "C for Cat";
  3.  
  4. //This will print the address of 1 in inArray array
  5. printf("The address of intArray[1] is %d", intArray);
  6.  
  7. //And this will print the address of 'C' in str
  8. printf("The address of 'C' is %d", str);

the job of puts(str) function is to print all the character starting at address str till it finds '\0'.
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 515 | Replies: 3 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:00 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC