Linked Questions

5 votes
5 answers
7k views

Negative index in array [duplicate]

Possible Duplicate: Negative array indexes in C? Can I use negative indices in arrays? #include <stdio.h> int main(void) { char a[] = "pascual"; char *p = a; p += 3; ...
David Ranieri's user avatar
2 votes
3 answers
538 views

Why do arrays with negative indexes work? [duplicate]

Possible Duplicate: Negative array indexes in C? How come this compiles, and runs as intended? I'm confused. I was just curious what would happen and to my surprise. It worked. #include <...
jantristanmilan's user avatar
6 votes
2 answers
113 views

Pointer Array in C? [duplicate]

Pointers are a difficult topic for sure, but I have come across this snippet and I just can't figure out what the p[-1] is: #include <stdio.h> int main(void) { int t[10] = { 1, 2, 3, 4, 5,...
SplineO's user avatar
  • 63
0 votes
3 answers
160 views

array indexing with signed number in C [duplicate]

I just want to know is it a good way of programming style. I know what is happening in this piece of code. look for the first occurrence of href save it next_next and then look for the first ...
user avatar
-1 votes
2 answers
115 views

Why is array[i][-1] == 0? How does one prevent undefined variables from being used? [duplicate]

/** * fifteen.c * * Computer Science 50 * Problem Set 3 * * Implements Game of Fifteen (generalized to d x d). * * Usage: fifteen d * * whereby the board's dimensions are to be d x d, * ...
KevJpg's user avatar
  • 7
-1 votes
2 answers
80 views

Retrieval of values in reverse order using pointers [duplicate]

I understand working of *(p+i), but, what actually is happening at memory level when retrieving values with *(p-i) or p[-i] through printf() function ? #include <stdio.h> int main() { ...
ninja's user avatar
  • 3
-5 votes
2 answers
122 views

Why isn't this C loop counting from -5 to 19? [duplicate]

I don't know why my program can't count from -5 to 19. Does anyone have a tip for me? Thanks a lot! int printArray(int array[], int count){ for ( i = 0; i < count; i++) { printf(&...
xDMG126's user avatar
  • 13
0 votes
1 answer
85 views

Can someone explain the role of p[-i] printf("%d" , p[-i]) [duplicate]

What I guessed is that in p[-i] it takes the ith position from current pointing one towards the left-hand side.I am just a beginner so is there something more to it. #include<stdio.h&...
Yash's user avatar
  • 1
15 votes
8 answers
13k views

Why does C support negative array indices?

From this post in SO, it is clear that C supports negative indices. Why support such a potential memory violation in a program? Shouldn't the compiler throw a Negative Index warning at least? (am ...
boxed__l's user avatar
  • 1,336
24 votes
6 answers
4k views

Using negative number as array index

I came along a competitive question that asks the output of the following: #include <stdio.h> int main() { int a[] = {0,1,2,3,4}; int i, *ptr; for(ptr = a+4, i=0; i <=4; i++) ...
Jishan's user avatar
  • 1,674
5 votes
8 answers
1k views

Evaluation of && boolean operator

If i have the following if statement if ( (row != -1) && (array[row][col] != 10) ) { .... } Where row is an int value and array is an int[][] object. My question is, if this will throw ...
AndreiBogdan's user avatar
  • 11.2k
2 votes
1 answer
3k views

Negative array index

I have a pointer which is defined as follows: A ***b; What does accessing it as follows do: A** c = b[-1] Is it an access violation because we are using a negative index to an array? Or is it a ...
ssn's user avatar
  • 2,771
4 votes
2 answers
2k views

Understanding the solution for the Jewelry Topcoder solution

I am fairly new to dynamic programming and don't yet understand most of the types of problems it can solve. Hence I am facing problems in understanding the solution of Jewelry topcoder problem. I am ...
ng.newbie's user avatar
  • 3,302
5 votes
1 answer
808 views

Accessing the -1 element of an array in c

I have an array of structs, which is dynamically allocated. A pointer to this array is passed around to other functions. struct body{ char* name; double mass; // ... some more stuff ...
Rohan's user avatar
  • 547
3 votes
3 answers
2k views

What's the meaning of -2[array]

I recently stumbled upon following code int array[] = {10, 20, 30}; cout << -2[array]; I understand that array is a pointer to the first element of the array but then what does [pointer] means ...
ATK's user avatar
  • 368

15 30 50 per page