views:

204

answers:

5

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?

+4  A: 

C89 does not support declaration of a loop counter in the first part of the for expression. C99 does, but most compilers need and explicit option (-std=c99 for gcc) to turn it on. Do:

int i;
for ( i = 0; i < 10 ; ++i ) ...

And also, don't add characters to character pointers :)

Nikolai N Fetissov
Well, as long as they are uninitialized...
Pascal Cuoq
He is trying to ADD a string to a string pointer. This will not concatenate - it will add the actual value of the pointer to `" "` to the value of the pointer `str`. It's valid, too, since `const char*` is a mutable pointer to a `const char` - I think what he means to use is a `char const str[11]`
BlueRaja - Danny Pflughoeft
It's not valid to add one pointer to another - pointer arithmetic is defined such that `pointer + integer = pointer`, and `pointer - pointer = integer`.
caf
+2  A: 

You have two problems here

1) C89 Standard does not allow for declarations to be inside the loop

for(int i = 0; i < 10; ++i)

should be:

int i
for(i = 0; i < 10; ++i)

2) You are attempting to add two pointer values:

str += " ";

What should be done here depends on what exactly you are trying to do with the string str.

Alan H
Point 2 is incorrect. This code has a problem, but he is not changing a constant. Please review your understanding of pointers.
asveikau
Improved point 2, thanks.
Alan H
C99 does allow int to be declared in a for loop, you probably mean C89 in point 1.
Dan Olson
Yes. Fixed that also. Thanks.
Alan H
+1  A: 

The code you've shown doesn't do anything meaningful. There are a couple things you might be trying to do:

My first guess is that you're trying to make the string that has the 10 spaces. To do that:

char str[10];
int i;
for (i = 0; i < 10; i++)
{
  str[i] = ' ';
}

The second thing you might be trying to do is change where your char * is pointing. Since you declared it as const, this won't work, as others have pointed out. But this is what your code is trying to do, hence why it is failing. If you are in fact trying to increase where the point is pointing (by the value of the ASCII value of space), then you need to make your pointer not constant.

EDIT: good catch. needs to be single quotes.

As to it not being a string, since it isn't null terminated, I was trying to as closely as possible predict what the OP was trying to do. Null terminating would be needed if it were to be treated as a true string, but I left that out, since the OP also did.

David Oneill
The line `str[i] = " ";` is still not correct - perhaps you meant `str[i] = ' ';` instead? Then, you still haven't created a "string", since there's no nul terminator (and no space for one, either).
caf
Change " " to ' ', please.
John Bode
+4  A: 

Your first error is because you have done:

for (int i = 0; ...

instead of:

int i;
for (i = 0; ...)

(alternatively, you can leave that bit alone and add -std=gnu99 to your gcc options).

Your second error is because the line:

   str += " ";

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:

char str[100]; /* Allocate space for a 99 character string, plus terminator */
int i;

str[0] = '\0'; /* Start with empty string */
for (i = 0; i < 10; i++)
{
    strcat(str, " ");
}

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:

char str[100];

strcpy(str, "          "); /* 10 spaces */

(The str[0] = '\0'; is unnecessary because we're now using strcpy, not strcat).

caf
Nathan Campos: I rolled back your edit, because the edited version didn't leave the destination as a valid string.
caf
A: 

There are several things that are wrong with your code.

Firstly, you are trying to build a string using a char pointer that does not point to anything. Either use an array of chars or allocate memory using malloc. Then, you are declaring int i inside the for loop. This won't compile on most compilers. You need to declare i before you use it in a for loop. You could also compile it using gcc using gcc --std=c99 test.c. After that, you try to concatenate strings by using the + operator. Remember, C does not have any built-in string type and therefore does not define the + operator for string concatenation ( a char* or char[] are not strings in the same way as the string classes in C++ and Java, they are just used as strings). You should use the concat function from string.h for that. Actually, repeatedly calling concat for adding a single char to the end of a string is not a good idea, but that's another story and let's just cross that bridge when we get there :).

It might be a good idea to go through the chapter on pointers and strings in any good C book. These things work a little differently than you are probably thinking.

MAK