|
-
October 28th, 2001, 05:01 PM
#5
Re: Why negative subscript in array?
This code uses negative subscripts and is not an error, neither at compile time or run-time.
int main()
{
char name[] = "ABC123";
char *pName = name + 3;
printf("This is the character 'C' %c" ,pName[-1]);
}
pName points to the '1' in "ABC123", so pName[-1] points to one character before the '1', which is the 'C'. This should illustrate why negative subscripts are legal in C or C++. pName[-1] points to a valid location.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|