Results 1 to 7 of 7
  1. Default C Programming- Calling elements in Arrays question.

    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.
    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++)
    //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.

  2. Flatpanel TV
    IGN: HarbingerLey
    Server: Mardia
    Level: 203
    Job: Demon Slayer
    Guild: [L]ittleBusters
    Farm: Dominion

    Default Re: C Programming- Calling elements in Arrays question.

    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?

  3. Default Re: C Programming- Calling elements in Arrays question.

    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.


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

  4. Default Re: C Programming- Calling elements in Arrays question.

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

    #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]);
      return 0;
    What do you think happens when you run the above code? Why?


    Here's why

    I would check elsewhere for your bug.

  5. Default Re: C Programming- Calling elements in Arrays question.

    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.

  6. Default Re: C Programming- Calling elements in Arrays question.

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

    Agreed with above though, the bug is somewhere else.

  7. Orbital Bee Cannon
    IGN: SaptaZapta
    Server: Kradia
    Level: 214
    Job: Hero
    Guild: Matriarchy
    Alliance: Dominion

    Default Re: C Programming- Calling elements in Arrays question.

    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?"



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts