Independent Researcher
Question
Asked 28 January 2011
Negative indices in C
Hello Friends,
Anyone please explain to me what happens when i supply negative index in C array. I got garbage value when i tried it.
Most recent answer
Hi need a sample open source for document reconstructions.... with DP and prims algo in java.. Actually this real time proj based on forensic science... Can any one help plz come on chat in google mail
All Answers (11)
Jiwaji University
Array indices start from 0 in C++ , I guess C too..
try to convert negative indices into positive..
Array[(i* -1)] , where i :negative index..
University of Oklahoma
An array in most programming languages is just a contiguous area of addressable storage and the language, say C, Rexx, NetRexx/JAVA keeps track of each element's 'address' starting from zero. When you tell C to address a negative amount it obliges with whatever garbage was in storage preceding the array's storage area. Rexx and NetRexx are not so brutal. I'm not sure what JAVA does; In general, it follows C.
1 Recommendation
University of Oklahoma
No matter what the language, best practice is to check the array index before it is used to make sure it is within logical bounds.
Robert L Hamilton, Engineer
Richardson, Texas USA
if y'u are trying to get negative value the program itself is vague, or absurd, because negative statements are considered as junk statements in programming languages, only positive statements are considered in p languages, so y'ur question is vague.
Jawaharlal Nehru Technological University, Hyderabad
index of an array, actually points to a position relative to the base address of the array(.i.e address of a[0]). Therefore a[3] is a location 3 positions after a[0] or is equivalent to:
*p=&a[0];
*p++; *p++; *p++;
hence a[-2] implies
*p--; *p--; *p--;
which is still a memory location(provided it exists) nevertheless (and can therefore show the value at the location) , but is not a part of your logical array,a
Similar questions and discussions
Related Publications
BASIC is a simple, easy-to-use computer programming language with English-like statements and mathematical notations.
Representing design decisions for complex software systems, tracing them to code, and enforcing them throughout the lifecycle are pressing concerns for software architects and developers. To be of practical use, specification and modeling languages for ...
Im ersten Teil des Buches haben wir von einfachen Programmieraufgaben ausgehend eine einfache Programmiersprache entwickelt, die es erlaubt, Algorithmen zur Lösung dieser Aufgaben zu programmieren. Ohne uns dann eine ernsthafte Rechenschaft darüber abzuverlangen, ob diese Programmiersprache zur Formulierung aller Algorithmen taugt, sind wir in Kapi...