-5

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("%d ", array[i]);
    }
    printf("\n");
}
int aufgabe4(int array[], int count){
    
    for ( i = -5; i < 20; i++)
    {
        array[i] = i + 3;
    }
    

}
int main (void){
printf("4.Aufgabe\n");
    int data3[9] = {0};
    aufgabe4(data3, 10);
    printArray(&data3[0], 10);
}

The expected output should be -5 -2 1 4 7 10 13 16 19 be But the shell gives me 3 4 5 6 7 8 9 10 11 12. I really don't know what is wrong because I calculate i + 3.

xDMG126
  • 13
  • 4
  • 2
    `array[i]` is probably undefined behaviour when i is negative, maybe you meant `array[i+5] = i + 3;` – Jean-François Fabre Dec 18 '22 at 15:37
  • What makes you think that it does not? Please provide a [mre] which demonstrates that observation? Did you print the value of `i` in the loop? Everything else I would consider too indirect for decent debugging. – Yunnosch Dec 18 '22 at 15:42
  • 1
    There is no `i` declared in the code you show, so that code cannot be compiled, let alone executed. Therefore, it is not the code that produced the output “3 4 5 6 7 8 9 10 11 12”. You have not shown the actual code. Do not expect anybody to debug code you have not shown them. – Eric Postpischil Dec 18 '22 at 15:55
  • The reason that you're not getting the correct output is that you need two values: the array index and the value to put into the array at that index. The index should go from 0 to 8, and the value should go from -5 to 19. – beaker Dec 18 '22 at 16:42
  • 1
    Does this answer your question? "[Are negative array indexes allowed in C?](/q/3473675/90527)", "[How dangerous is it to access an array out of bounds?](/q/15646973/90527)" – outis Dec 19 '22 at 07:29

2 Answers2

1

Answer 1:
If it does not, it is because you have undefined behaviour.
The undefined behaviour is caused by accessing outside of an array.
Which happens here array[i] = i + 3; for the cases of i being any of -5,-4,-3,-2,-1.

Answer 2:
This answer is not really the answer, because in the presence of undefined behaviour, all explanation attempts are moot.
It is however possible that among all those evil things which the compiler and runtime environment are allowed to do in case of undefined behaviour (basically EVERYTHING...) is the following:

  • this loop for ( i = -5; i < 20; i++) does indeed count from -5 to 19
  • this line array[i] = i + 3; inside that loop accesses first before the array (causing undefined behaviour) and later inside and writes values to illegal memory places (i.e. those you should not write to) and then into the array writes some values which are three higher than then counter i
  • later you print those values from index 0 to index 9, and get an output of them, each three higher than the corresponding index, i.e. what you observe

3,4,5,6,7,8,9,10,11,12

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
0

A few issues ...

  1. In your example, writing to array[i] with a starting value for i of -5 is trying to write before the start of the array in main. This is UB (undefined behavior)
  2. You are setting the array value to i + 3.
  3. This does not increment/count by 3.
  4. We want to use i and increment by 3
  5. In main, the array has 9 elements, but we're passing 10 as a count.
  6. The function does not check the array index against the count, so it relies on serendipity that the limit of the value will prevent overflow.

So, change:

for (i = -5; i < 20; i++) {
    array[i] = i + 3;
}

Into:

for (int i = 0, j = -5; i < count; i++, j += 3)
    array[i] = j;

Here is the corrected code. It is annotated:

#include <stdio.h>

int
printArray(int array[], int count)
{
    for (int i = 0; i < count; i++) {
        printf("%d ", array[i]);
    }
    printf("\n");
}

int
aufgabe4(int array[], int count)
{

// NOTE/BUG: this is indexing _before_ the start of the array so this is
// UB (undefined behavior)
// NOTE/BUG: we don't want "i + 3" -- we want to increment by 3
#if 0
    for (int i = -5; i < 20; i++) {
        array[i] = i + 3;
    }
#else
    for (int i = 0, j = -5; i < count; ++i, j += 3) {
        array[i] = j;
    }
#endif
}

int
main(void)
{
    printf("4.Aufgabe\n");

// NOTE/BUG: too small for passed count value
#if 0
    int data3[9] = { 0 };
#else
    int data3[10] = { 0 };
#endif

    aufgabe4(data3, 10);

    printArray(&data3[0], 10);

    return 0;
}

In the code above, I've used cpp conditionals to denote old vs. new code:

#if 0
// old code
#else
// new code
#endif

#if 1
// new code
#endif

Note: this can be cleaned up by running the file through unifdef -k

Craig Estey
  • 30,627
  • 4
  • 24
  • 48