Array indexing starting at a number not 0

后端 未结 13 2364
Happy的楠姐
Happy的楠姐 2021-02-20 04:02

Is it possible to start an array at an index not zero...I.E. you have an array a[35], of 35 elements, now I want to index at say starting 100, so the numbers would be a[100], a[

相关标签:
13条回答
  • 2021-02-20 04:48

    Just have a variable that takes into account the offset. Even if possible, having an array that doesn't start at the conventional 0 would be highly unreadable.

    0 讨论(0)
  • 2021-02-20 04:49

    Perhaps you can use union?

    #pragma pack(0)
    union 
    {
      char array[15000];
      struct { char sram[10000]; char bram[5000];  } map;
    } combination;
    

    They are physically contiguous. If you access 'array' then it will go into either bram or sram based on the offset

    You need the pragma to tell the compiler to NOT align struct members on word boundaries. This prevents a number of bytes of space between the two parts of the map structure.

    0 讨论(0)
  • 2021-02-20 04:50

    You're not very clear on exactly how you want to use these arrays, but its easy enough to set up a single contiguous array that can appear to be two different arrays:

    int   ram[15000]; 
    int * sram=&ram[0];
    int * bram=&ram[10000];
    

    I used the &foo[xxx] notation just to make it explicit what you're doing. Anyway, you can now use ram to index anywhere into the entire array, or sram and bram to index into particular parts.

    0 讨论(0)
  • 2021-02-20 04:51

    As others have noted, you can technically achieve what you want by invoking pointer arithmetic like this:

    int* myArray = malloc((upperBound - lowerBound) * sizeof(*myArray));
    myArray -= lowerBound;
    ...
    for(int i = lowerBound; i < upperBound; i++) {
        myArray[i];
    }
    

    While this will work flawlessly with -O0, it is undefined behaviour from a language point of view. The machine, however, has absolutely no objections to this, to the machine the pointer arithmetic involved is the same as any unsigned integer arithmetic with uintptr_t. Thus, the only danger to this approach is the optimizer.

    Now, you can easily defeat the optimizer by splitting the code above into two different compilation units, i. e. have one ".c" file with the function

    int* fancyIntArray(size_t lowerBound, size_t upperBound) {
        return intMalloc(upperBound - lowerBound) - lowerBound;
    }
    

    and put the function intMalloc() into a different ".c" file:

    int* intMalloc(size_t size) {
        return malloc(size_t*sizeof(int));
    }
    

    Now, you are safe, because when the optimizer looks at the pointer arithmetic in fancyIntArray(), it does not know that there is no allocated memory in front of the address that intMalloc() returns, as it does not know anything about intMalloc() itself. As such, it is forced to leave your code intact.

    And, of course, you should use independent compiler calls to compile the different ".c" files, so that the optimizer really cannot deduce anything about intMalloc().

    0 讨论(0)
  • You could cheat with macro:

    int myarray[35];
    
    #define a (myarray - 100)
    
    a[100] = 0;
    

    A pointer could also be used.

    0 讨论(0)
  • 2021-02-20 04:54

    Is it possible to start an array at an index not zero...I.E. you have an array a[35], of 35 elements, now I want to index at say starting 100, so the numbers would be a[100], a[101], ... a[134], is that possible?

    No, you cannot do this in C. Arrays always start at zero. In C++, you could write your own class, say OffsetArray and overload the [] operator to access the underlying array while subtracting an offset from the index.

    I'm attempting to generate a "memory map" for a board and I'll have one array called SRAM[10000] and another called BRAM[5000] for example, but in the "memory" visiblity they're contiguous, I.E. BRAM starts right after SRAM, so therefore if I try to point to memory location 11000 I would read it see that it's over 10000 then pass it to bram.

    You could try something like this:

    char memory[150000];
    char *sram = &memory[0];
    char *bram = &memory[100000];
    

    Now, when you access sram[110000] you'll be accessing something that's "in bram"

    0 讨论(0)
提交回复
热议问题