Firstly, look at the following simple code:
char str[80] = "This is - my text - for test";
const char s[2] = "-";
char *token;
token = strtok(str, s);
while (token != NULL) {
printf(" %s\n", token);
token = strtok(NULL, s);
}
The function strtok()
returns the data type char*
and as you seen we created a variable named token
that variable not initialized.
Now, look at the next code:
char *buff;
int num = 500;
sprintf(buff, "%d", num);
The result of the previous code is an error uninitialized local variable 'buff'
.
My question is, why in the first code does not occurs any problem, while, in the second code occurred an error ?