| | |
Arrays and Pointers
![]() |
•
•
Join Date: Jul 2008
Posts: 6
Reputation:
Solved Threads: 0
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:
Thanks for the help,
java_girl
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:
c Syntax (Toggle Plain Text)
#include <stdio.h> int arr[5] = { 2, 4, 6, 8, 10 }; int main( void ) { printf( "The address of the '2' is %d\n", arr ); return 0; } char* message1 = "C"; char* message2 = "is the"; char* message3 = "best"; char* message4 = "programming"; char* message5 = "language!"; puts( message1 ); puts( message2 ); puts( message3 ); puts( message4 ); puts( message5 ); /* message1 points to the 'C' and using puts (message1) will */ /* display the letter as opposed to the address. Why is this? */ /* I know the puts() function receives a char pointer as an */ /* argument. Is this why one need not dereference it by */ /* using *message1 unless one is using */ /* printf( "%c\n", *message1 ); */ /* Essentially, I'm slightly confused by when one pointer is */ /* pointing to the address of the first element in an array and when */ /* it is pointing to the first value */ return 0;
java_girl
Last edited by Narue; Dec 1st, 2008 at 3:41 pm. Reason: fixed code tags
•
•
Join Date: Aug 2008
Posts: 77
Reputation:
Solved Threads: 16
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:
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:
C Syntax (Toggle Plain Text)
printf( "%c\n", message2[3] ); // this will print 't'
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
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
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 c Syntax (Toggle Plain Text)
char* message1 = "C";
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 6:04 am.
if u want to print the address of first character in the string it is same as you did with other array. For Ex.
the job of puts(str) function is to print all the character starting at address str till it finds '\0'.
c Syntax (Toggle Plain Text)
int intArray[3] = {1,2,4}; char *str = "C for Cat"; //This will print the address of 1 in inArray array printf("The address of intArray[1] is %d", intArray); //And this will print the address of 'C' in str 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.
![]() |
Similar Threads
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- Sorting arrays of pointers with function? (C)
- Comparing arrays (C++)
Other Threads in the C Forum
- Previous Thread: Good webpage that explains how to use rand() and srand() correctly?
- Next Thread: Random Choice
| Thread Tools | Search this Thread |
* ansi array asterisks binarysearch calculate changingto char character cm convert copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createprocess() database feet fflush fgets file floatingpointvalidation fork forloop function givemetehcodez global grade gtkwinlinux hacking histogram homework i/o inches infiniteloop input interest intmain() iso kernel keyboard kilometer km linked linkedlist linux locate looping loopinsideloop. lowest match meter microsoft mqqueue number oddnumber odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc process program programming pyramidusingturboccodes radix read recv recvblocked research reversing scanf segmentationfault sequential single socket socketprograming socketprogramming standard string suggestions systemcall threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault win32api windowsapi






