Strings

My doubt is while accessing the values using arrays like (names[2][0]) we can access values stored in array but while we are writing &names[2][0] it means we are printing the address of that value in the following code.Please clarify


main( )
{
char names[ ][10] = {

"akshay",
"parag",
"raman",
"srinivas",
"gopal",
"rajesh"
} ;
int i ;
char t ;
printf ( "\nOriginal: %s %s", &names[2][0], &names[3][0] ) ;
you are correct the & means address of.

and because you specify both indices, you are getting the address of a character.

so in your printf() you can either
1) replace the %s's with %x to see the hex addresses of the two chars you reference.
2) replace the %s's with %c's and remove the &'s to see the characters you reference.
3) remove the &'s and remove the [0]'s to see the strings you are really after.

its worth noting that names[][] is actually an array of arrays.
with that in mind printf("%s",names[3]) is actually passing the fourth array of chars ("srinivas") which we refer to as a c-string or c style string.
Last edited on
Topic archived. No new replies allowed.