I tried to understand C behavior and found some weird things. I debugged and found out that table values are correct until calling printf. I create a void function to test whether it is a problem of scope but after calling this function the table values still remained correct. I wonder now if printf delete previous local variables.
#include <stdio.h>
#include <stdlib.h>
void invertTable(int** tableau,int size){
int temp[size];
for(int i = 0; i < size; i++)
{
temp[i]=-1*tableau[0][i];
}
tableau[0]=temp;
}
void test(){
}
int main(int argc, char const *argv[])
{
int* table=(int*)malloc(5*sizeof(int));
table[0]=1;
table[1]=2;
table[2]=3;
table[3]=4;
table[4]=5;
invertTable(&table,5);
test();
for(int i = 0; i < 5; i++)
{
//Here is the problem
printf("\n %d \n",table[i]);
}
free(table);
return 0;
}
Expected -1 -2 -3 -4 -5
Output: -1 1962295758 1 1962550824 1962295741