C/explanation
Expert: Narendra - 6/16/2004
Question***can you explian the meaning for the follwoing program for me, I don't know what the meaning of the code.****
char *c1, *c2, *c3, *c4, *c5 ;
char analysis[8] = {'a', 'n', 'a', 'l', 'y', 's', 'i' ,'s'};
int main()
{
c5 = c4 = analysis;
++c4;
c3 = &analysis[6];
c2 = c5 + 2 ;
c1 = &analysis[7] - 3 ;
printf ("%c\t%c\t%c\t%c\t%c", *c1,*c2,*c3,*c4,*c5);
return 0;
}
---------------------------------------------
y a i n a
Answerc1, c2, c3, c4 and c5 are pointers (which can hold addresses).
analysis is an array variable which is holding the string "analysis".
>c5 = c4 = analysis;
The starting address of the array is given to c5 and c4. Hence they point to first character 'a' in the array.
>++c4;
c4 is incremented by 1. Hence points to 'n' in the array.
>c3 = &analysis[6];
c3 is given the address of 7th character (count from 0) of the array. Hence c3 points to character 'i'.
>c2 = c5 + 2 ;
c5 points to first character. plus 2 means, c2 points to 3rd character 'a' (second 'a' in the string).
>c1 = &analysis[7] - 3 ;
c1 points to 3rd character from the end (not counting the last character).
c1 holds the address. *c1 means the data strored at c1. Since c1 points to 3rd character from the end (that is 5th character - count starts from 0), *c holds character 'y'.
Hence *c1 will print 'y' on the screen.
Similarly for other pointer variables *c2, *c3, *c4 and *c5.
Hope it is clear now.
-ssnkumar