Suppose we have this first code :
#include <stdio.h>
int main(int argc, char *argv[]){
char **campos;
char *palabra;
int i = 3;
if(argc > 1){
while(i > 0){
palabra = argv[i];
campos[i] = palabra;
printf("Éxito: %s\n",campos[i]);
i--;
}
}
return 1;
}
Which, when compiling, warns you that the "fields" variable, ditto, is used uninitialized. Specifically:
test.c:10:10: warning: 'fields' may be used uninitialized in this function [-Wmaybe-uninitialized] fields[i] = word;
Upon execution, the first iteration already results in a segmentation fault .
Now suppose the second code :
#include <stdio.h>
int main(){
char **campos;
char *palabra;
palabra = "Hola Mundo";
campos[0] = palabra;
printf("%s\n",campos[0]);
}
This time, a similar warning pops up when compiling:
test2.c:7:12: warning: 'fields' is used uninitialized in this function [-Wuninitialized] fields[0] = word;
However, the execution happens without any problem.
So, we have two codes: both follow the same idea (storing words in "fields"), but the second only has one iteration. The structure of both codes is also similar (removing the loops). So why doesn't the first one execute and the second one does?
I see the following:
(1) fields[i] returns a char* pointer.
(2) "word" is a char* pointer.
(3) Make word = "Hello World"; involves changing the direction pointed by "word" to the direction in which the letter 'H' of the string "Hello World" is found. Thus, "word" points to "Hello World".
(4) So, if I make fields[i] = word I am making fields[i] = argv[i] , which implies that the char* pointer that is fields[i] points to the same address that argv points to [Yo].
So: what is happening in these codes so that, despite their similarity, one executes and the other does not?
Thanks.
That is undefined behavior . A program with undefined behavior may work, it may not work, it may act strangely, or it may summon daemons into your nostrils .
Why in one case does X happen and in another case does Y happen? because it is undefined behavior and anything else could have happened.
According to the C standard (translation and highlights are mine):
What difference there are? In principle none.
Both codes have a major error, which is that the double pointer
campos
is not initialized . Any attempt to access any position ofcampos
will generate an indeterminate situation.Indeterminate situations are characterized by not making it clear what the behavior of the program should be in that case. This means that depending on the compiler, the Operating System, the hardware... in some cases the program will behave in one way and in others in another.
Although for you the operations are equivalent, for the compiler they do not have to be.
Here you have, for example, the assembly corresponding to the first example [link] :
And then the assembly of the second code [link] :
You will see how you are not able to find a direct relationship between both codes... although it is true that the code of the second example is almost almost in the first example... but it remains almost almost.
In any case, what has been said, the origin of the error is that
campos
it is not initialized. You have to reserve memory for that array:Once that is done both codes should work.