I want to initialize arbitrary large strings. It is null terminated string of characters, but I cannot print its content.
Can anybody tell me why?
char* b;
char c;
b = &c;
*b = 'm';
*(b+1) = 'o';
*(b+2) = 'j';
*(b+3) = 'a';
*(b+4) = '\0';
printf("%s\n", *b);
Your solution invokes undefined behaviour, because
*(b+1)etc. are outside the bounds of the stack variablec. So when you write to them, you’re writing all over memory that you don’t own, which can cause all sorts of corruption. Also, you need toprintf("%s\n", b)(printfexpects a pointer for%s).The solution depends on what you want to do. You can initialize a pointer to a string literal:
You can initialize a character array:
This can also be written as:
Or you can manually assign the values of your string: