Hello friends,
i have some doubt and hope i will get an satisfactory answer from here.
1) Can memory addresses be negative........i have initialized an array and check the address of element and found it negative...so i want to know can it be negative and if yes then how addresses are assigned to variables.

2)is negative subscript of array allowed in C? if yes then what is the meaning of it?
i found on net that array[-1] will give the last element of array, and also i found that array[-1] equal to *(array-1), then how these two can be same?

One more thing friends....just take a look this code:

#include<stdio.h>

int main()
{
        int i,array[10];

        array[0]=0;
        array[1]=1;
        array[2]=2;
        array[3]=3;
        array[4]=4;
        array[5]=5;
        array[6]=6;

        for(i=-6;i<10;i++)
        printf("%d \t%d\n ",array+i,array[i]);
        printf("\n");
        return 0;
}

And the Out put on my machine is....

-1078159552 0
-1078159548 -1078159552
-1078159544 -1078159552
-1078159540 0
-1078159536 0
-1078159532 0
-1078159528 0
-1078159524 1
-1078159520 2
-1078159516 3
-1078159512 4
-1078159508 5
-1078159504 6
-1078159500 134518452
-1078159496 -1078159464
-1078159492 134513849

Question is how array[-1],array[-2 ] element are zero......is there ny reason???
help plz

Recommended Answers

All 11 Replies

> Can memory addresses be negative
No - that's just the way you print it.
Use the %p format for printing a pointer.

> 2)is negative subscript of array allowed in C?
No.
Arrays themselves are always 0-based, so there is never a negative subscript.
That's not to say that you can't have say p = &array[5] and then do p[-1]. > i found on net that array[-1] will give the last element of array
Well it's wrong. Got a reference / URL?

> Question is how array[-1],array[-2 ] element are zero......is there ny reason???
Dumb luck.
Like any out of bound memory reference, there's a good chance you'll find "something" there, and zero is as good a value as any other. The result is however meaningless as you can never use that information in any practical way.

commented: Thanks friend +1

I suppose you are using some compiler like TC because if you try to do something like array[-1] in gcc then it gives a segmentation fault.
Actually the concept is like this.When you write a statement like

int array[10];

10 consecutive integer blocks are alloted in memory with name array which can be accessed as array. When you write array[-1] then it tries to access an integer block before the memory allocated for array.Hence is illegal memory access.Hence not a valid statement.

And

array[1] = *(array+1);

because as you know

array=&array[0]

,you can access the 2nd block of memory allocated for array or the [1]th block as array[1] or *(array+1) because

*(array+1)=*(&array[0]+1)

which means get the array starting address increment it by 1 so it will point to second block of memory then get its value by * operator.

commented: Nice explanation :) +8

Pavan, would you mind to post using code tags?

>is negative subscript of array allowed in C?
No, though you can achieve something like that by using pointer arithmetic, it can be very dangerous to do:

[B]// This is completely wrong and very dangerous![/B]
int array[20] = {0};
int *p = array;
*(--p) = 35;

>if yes then what is the meaning of it?
Undefined behavior!

friend i m using gcc nd it is not giving ny error.....

friend i m using gcc nd it is not giving ny error.....

Welcome to the world of C!
It's not because it seems working that it actually works!

BTW, Why would you ever need to specify a negative subscript?
(Maybe because you want to write in some memory not belonging to your program?)

If you really want to see your program crashing, then try specifying a very big negative value, for example: array[-355465] = 56; :P
(However, I do NOT recommend this!!)

commented: Good one there tux ;) nice code to crash the program hahaha... +2

R u sure ? will it crash the program? i really want to see it...

> will it crash the program?
Whether it does or not doesn't matter.

Code which tries to do such things is broken. The lack of a crash doesn't make it any less broken (only harder to diagnose and fix).

friend i m using gcc nd it is not giving ny error.....

According to the standard:

An array subscript is out of range, even if an object is apparently accessible with the
given subscript (as in the lvalue expression a[1][7] given the declaration int
a[4][5])

According to the rules of pointer arithmetic, negative subscripts are possible: int array[10]; array[5] is the same as *(array + 5)
array[-2] is the same as *(array - 2)

About negative subscript in arrays and array notations
Consider the following code:

#include<stdio.h>
int main()
{
    int ia[]={0,1,2,3,4,5,6,7};/*a normal array*/
    int* ip=&ia[4];/*a pointer pointing to 5th element*/

    /*WRONG: will print garbage. Unpredictable value*/
    printf("%i \n",ia[-3]);

    /*OK. Prints 1*/
    printf("%i \n",ip[-3]);

    return 0;
}

The Line 11 will be interpreted as *(ia-3) which is (absolutely) perfect since the location has been declared by the compiler.
While Line 8 is hazardous since it access a element which is out-of-bond.

In no case does negative subscript will produce last elements. (This facility is available in Python)

If you want to design a construct to do so, consider this:

#include<stdio.h>
#define AT(X,Y) ((Y<0)?(*(X+sizeof(X)/sizeof(*X) + Y)):(*X+Y))
int main()
{
    int ia[]={0,1,2,3,4,5,6,7};

    printf("%i \n",AT(ia,-1));/* Prints 7*/
    printf("%i \n",AT(ia,2));/* Prints 2*/
    return 0;
}

Always know the Murphy's Law: "you'll be hit the hardest at the worst possible moment (when the customer is looking, when a high-value transaction is trying to post, etc.)"

According to the C and C++ standards

int array[....];

array[-2] - wrong expression, because array-2 points to inexisting array element (the only exception: pointer to the 1st element after array end). Therefore we can't dereference it in *(array-2) expression (that's array[-2] by definition). Moreover, array-2 value is undefined in that case.

In actual fact binary (with two arguments) built-in subscript operator[] is defined for pointer-integral arguments pair but not for array-integral pair only. That's why p[-2] may be legal subscript if (and only if) p points to an array element with index greater than 1, for example:

int  array[10];
int* p = &array[2];
p[-2] = 0; // same as array[0]

Of course, it's not the same as negative array index.

Excuse me, siddhant3s: I have not seen your post above! :(
No need in my post now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.