We help IT Professionals succeed at work.

Array Question

jerntat
jerntat asked
on
If for example i have :

char temp[256] = "abcde";
cout<<temp[-1];

Does anybody know what does temp[-1] do? Which part of the value in the array temp does temp[-1] hold?
Comment
Watch Question

In general, we can not know what is
the value for temp[-1].

Author

Commented:
I saw this being use in a sample code, but just couldnt figure out what it is doing. The code look something like that:

while( (nbytes = read(fd, bufptr,buffer + sizeof(buffer) - bufptr -1 )) > 0)
{

  bufptr += nbytes;
  if( bufptr[-1] == '\n' || bufptr[-1] == '\r')
  {
    break;
  }
}


                      // assume we have
                     char *save_ptr = bufptr;

                      bufptr += nbytes;
                      if( bufptr[-1] == '\n' || bufptr[-1] == '\r')
                      {
                        break;
                      }

Now, bufptr[-1] is same as save_ptrr[nbytes-1]


Assume , we have

      char buf[200] = "sample code";
      char *bufptr   = buf;

      bufptr  = bufptr + 3;


      now *bufptr is p . If you use     cout << bufptr;  it  will display

                ple code

     and  bufptr[-1] is m




Explore More ContentExplore courses, solutions, and other research materials related to this topic.