473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Negative array index in C

4
Hi all,

char *a;
char b[10]={"12345789"};
a=b;

Can any one tell what actually a[-1] would refer to.
What is the use of it /its common usage.

-
Harry
Sep 16 '08 #1
3 2987
donbock
2,426 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. char *a;
  2. char b[10]={"12345789"};
  3. a=b;
Can any one tell what actually a[-1] would refer to.
What is the use of it /its common usage.
Your example is will yield either an intermittent bug or a segment fault; but there is a similar construction that does work. The following code excerpt comes from "The Standard C Library" by P.J. Plauger, copyright 1992.
Expand|Select|Wrap|Line Numbers
  1. #include <limits.h>
  2. #if EOF != -1 || UCHAR_MAX != 255
  3. #error WRONG TOLOWER TABLE
  4. #endif
  5. static const short tolow_tab[257] = {EOF,
  6.   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,   /* 0x00 - 0x07 */
  7.   ...
  8.   0x40,  'a',  'b',  'c',  'd',  'e',  'f',  'g',  /* 0x40 - 0x47 */
  9.   ...
  10.   };
  11. const short *_Tolower = &tolow_tab[1];
  12.  
  13. #define tolower(c) _Tolower[(int)(c)]
  14.  
  15. int (tolower)(int c) {
  16.   return (_Tolower[c]);
  17. }
The difference between this example and yours is that the pointer is set to the address of the second element of the array. Thus there is something at offset -1 from the pointer.

For an array variable A with n elements, the index absolutely must not be outside the range from zero to n. Note that you can refer to the address of A[n], but you can't actually try to access that element. The compiler knows if you break these rules.

For a pointer variable, you can use pointer arithmetic to access locations on either side of the location pointed to. Pointer arithmetic can be indicated either by a literal arithmetic expression or through array index format. The compiler doesn't know if your pointer arithmetic expression is meaningful.

Cheers,
donbock
Sep 16 '08 #2
donbock
2,426 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. const short *_Tolower = &tolow_tab[1];
This line would be better as
Expand|Select|Wrap|Line Numbers
  1. const short * const _Tolower = &tolow_tab[1];
Cheers,
donbock
Sep 16 '08 #3
vharry
4
This line would be better as
Expand|Select|Wrap|Line Numbers
  1. const short * const _Tolower = &tolow_tab[1];
Cheers,
donbock
Hi donbock,

Thanks a lot .
got it .

Above code would be
char *a ;
char b[10]={"123456789"};

a=&b[strlen(b)-4];

a[0] = would be -> 6
a[-1] = 5
a[-2]= 4
a[-3]= 3
etc

It should accessed as above way .
Thanks
harry
Sep 17 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: dan | last post by:
I was recently surprised, and quite shocked in fact, to find that Python treats negative indices into sequence types as if they were mod(length-of-sequence), at least up to -len(seq). This fact...
2
by: Mantorok Redgormor | last post by:
Are negative values used as an index for an array valid or is this illegal and if it is illegal where does it state it in the standard? The only way I can see where a negative value can be used...
6
by: David Osborn | last post by:
I have a pointer "a" that points to an element in an array that is not the first element. I want to use a negative index to derefence the pointer, like so: a. Is this valid C99? If not, I can...
8
by: Joakim Hove | last post by:
Hello, I have the following code: #define N 99 double *ptr; double *storage; int index; storage = calloc(N , sizeof(double));
1
by: illegal.prime | last post by:
So I see from the documentation here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemCollectionsArrayListClassBinarySearchTopic.asp That the code uses the...
1
by: Ryan Liu | last post by:
Hi, When my code run to DataRow.Delete() It throws: System.ArgumentOutOfRangeException: Non-negative number required. Parameter name: length at System.Array.Copy(Array sourceArray, Int32...
35
by: Martin Wells | last post by:
Plain char may be signed or unsigned. Typical ranges could be: CHAR_MIN == -128, CHAR_MAX == 127 CHAR_MIN == 0, CHAR_MAX == 255 The Standard says that the behaviour is undefined if we...
7
by: klays | last post by:
Hi all, I have created char array, when I use tab compiler shouts out: error: size of array 'tab' is negative I tried to cheat compiler and a little modify this code. Look at the code: ...
1
by: santoshsri | last post by:
Hi All, My C# web application calls a webservice to process a report. It sends XMLs as parameter and in response gets an XML node which stores Binay datatype bin.base64. It makes an instance of...
6
by: shashi shekhar singh | last post by:
Respected Sir, I am facing problem when i try to deploy my website on iis 7.0 on test page. i have to display some .mht files on iframe in gridview and error looks like below, Server Error in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.