Southperry.net
C Programming- Calling elements in Arrays question. - Printable Version

+- Southperry.net (https://www.southperry.net)
+-- Forum: Social (https://www.southperry.net/forumdisplay.php?fid=14)
+--- Forum: Rubik's Cube (https://www.southperry.net/forumdisplay.php?fid=58)
+--- Thread: C Programming- Calling elements in Arrays question. (/showthread.php?tid=63561)



C Programming- Calling elements in Arrays question. - Lozmaster - 2013-04-02

Relatively straightforward question: When you call elements in an array, say data[i][j], where elements have been defined from i,j=0 to some higher (integer) number.

Is it possible to use a command when calling array elements, so that if somehow in data[i][j] "i" ended up being less than zero, it would instead call the value stored in the corresponding positive value of i?

For example, with an array called data, calling the element data[-1][0], in this case it would call data[1][0].

I tried adding abs() commands to array calls,such as data[abs(i)][0] but that just caused the compiler to crash (In case I needed to make it clear that I have no idea what I'm doing).

I'm fairly sure the code is calling values of negative [i] elements in the array after a the first loop (I would expect it to), and it's making the compiler crash.

A little bit of what my code is, in case anyone feels its important. It could probably be a lot more intuitive somehow, but I'm not worried about that, as long as it works.
Code:
double **data;
        data=(double**)malloc(nradius*sizeof(double*)); //dynamically allocate memory for the array "data".
        
        int i;
        for(i=0; i<nradius; i++)
        {
        data[i]=(double*) malloc(ntime*sizeof(double));
        }
          
        //initialise the array
        for(i=0; i<nradius; i++)
        {
                 data[i][0]=1+sin(k*r);
                 r=r+deltar;
        }

//horrible differential equation loop that isn't relevant to southperry goes here
Ignore any variables that aren't defined in the above snippet, they were earlier.


I suppose I could shift everything definitions up by an arbitrary number, but it would require a lot of unintuitive redefinitions that I would rather avoid if there it is possible to just redirect the error causing array elements.


C Programming- Calling elements in Arrays question. - Dudewitbow - 2013-04-02

how did i become negative in the first place. regardless, my noob tier programming would say make an if statement saying if(i < 0){//insert absolute value function here} before reading into the loop?


C Programming- Calling elements in Arrays question. - Locked - 2013-04-02

data[abs(i)][0] causes it to crash because it returns a double, while array indices expect a size_t, consider casting it to an int.

data[(int)abs(i)][0]

Disregarding the fact why the indices are negative to begin with.


C Programming- Calling elements in Arrays question. - Fiel - 2013-04-02

Something to understand is that putting a negative number in an array is not what makes a program crash.

Code:
#include <stdio.h>
#include <Windows.h>

int main(void)
{
  int data[5] = {0};
  int moredata[5] = {0};

  data[-1] = 20;

  printf("%d\n", moredata[4]);
  system("pause");
  return 0;
}

What do you think happens when you run the above code? Why?

 Answer

[spoiler=Here's why]
data[i] is syntactic sugar for *(data + i). So it would be fetching *(data + sizeof(data)*i)

So, because of how the stack works in assembly, putting -1 in the array indice invades upon a different array.

One of the most powerful ideas of C is "trust the programmer". C doesn't care if you go outside of array bounds. It assumes that you know what the hell you're doing.
[/spoiler]

I would check elsewhere for your bug.


C Programming- Calling elements in Arrays question. - Locked - 2013-04-02

Accessing out of bound elements, whether positive or negative is undefined behaviour. Undefined behaviour are parts of the standard that don't explicitly define their behaviour when ran.

While Paul's compiler (VC++) might print 20, other compilers will have completely different outputs. In my machine it prints 0, because we cannot be sure of the defined behaviour of the program.


C Programming- Calling elements in Arrays question. - Dusk - 2013-04-02

Compiled that code with gcc on a Linux terminal; it also prints 0 for me.

Agreed with above though, the bug is somewhere else.


C Programming- Calling elements in Arrays question. - SaptaZapta - 2013-04-03

That's not the way to do it.

If i is getting negative values (though I don't see anything in your code snippet that would make it do so), you must find out where and prevent it there. Otherwise, how do you know that when i is -5 it's really supposed to be 5? Maybe it should be forced to 3? Or 100?

Debug your program. With a debugger if you know how to use it (recommended) or by debug messages (printing out where you are and what the values of relevant variables are).

First question I'd ask about this code snippet is "what is the value of ntime?"