Differences in using array in main program or in functions sub-routines
I'v got some question about using array pointers in program. When I use
some array name (which is a const pointer to first array element)
char charTab[] = "ABCDEFGHIJKLMNOPRSTUWXYZ"; /* Basic data buffer */
char *charPtr = charTab; /* Assign */
charPtr += 3; /* It's ok, now we point
4th element */
charTab += 3; /* No, the lvalue required
for '+' operand */
But when I create let's say the following function:
void CharTabMove(char *tabToMove, int noToMove);
With definition
void CharTabMove(char *tabToMove, int noToMove)
{
printf("-------IN FUNCTION---------\n");
printf("It's pointing to %c\n", *tabToMove);
tabToMove += noToMove;
printf("Now it's pointing to %c\n", *tabToMove);
printf("-------LEAVING FUNCTION---------\n");
fflush(stdout);
}
The function is allow to move this pointer along the array with no
problem. Sure, after leaving the function the pointer will be still
pointing to first element of charTab, but why the function is allowed to
move the constant pointer? Thanks in advice for response, I'm trying to
explain that to my 11 yo nephew :)
No comments:
Post a Comment