I need to loop a const char, and I’ve used a simple example of string loop:
const char *str;
for(int i = 0; i < 10; ++i)
{
str += " ";
}
But when I tried to compile, I got this:
ubuntu@eeepc:~/Test_C_OS$ gcc -o kernel.o -c kernel.c -Wall -Wextra -nostdlib -nostartfiles -nodefaultlibs
kernel.c:26: error: ‘for’ loop initial declaration used outside C99 mode
kernel.c:28: error: invalid operands to binary +
ubuntu@eeepc:~/Test_C_OS$
What should I do?
Your first error is because you have done:
instead of:
(alternatively, you can leave that bit alone and add
-std=gnu99to your gcc options).Your second error is because the line:
attempts to add two pointer values. That doesn’t have any defined semantics in C – it doesn’t make any sense. It’s not even particularly clear what you’re trying to do here – perhaps you want to start with an empty string, then append 10 copies of the string
" "to it? If so, then you need to change it to something like:In this particular case though, because you’re always looping 10 times you don’t really need a loop at all – you can just use a string of 10 spaces:
(The
str[0] = '\0';is unnecessary because we’re now usingstrcpy, notstrcat).