开发者

Looping a Const Char

开发者 https://www.devze.com 2022-12-17 04:45 出处:网络
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)

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:

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).


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 :)


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.


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.


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.


If you want to loop/iterate your const char *str do this:

const char *str;
for (int i = 0; i < sizeof(str); i++)
{
   doSomethingWith(str[i]);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消