0

Need help with copying array object to a temp array object using a for loop (see code + comments below)..... Thanks in advance!!!!

    int counter;
    char buffer[] = "this is what i want 0 ignore the rest after the zero"; //
    char command[sizeof(buffer)];

    for ( counter = 0; counter < sizeof(buffer); counter++ ){
        if ( buffer[counter] == '0' ){      
            break; // Exit loop (Should Exit)
        }
        command[counter] = buffer[counter]; // Copy array object into new array
        printf("%c",command[counter]);
    }
    printf("\n",NULL);
    printf("%s\n",command); // However when I print it contains the whole array this shouldnt be is should only contain "this is what i want "
5
  • that code is C - not c++ Commented Jun 3, 2011 at 17:47
  • 3
    Verify that both zeros are a zero, not an uppercase 'O'. Also, ensure that you terminate the command[] buffer with a null character -- '\0'. Also, the behavior would occur if you accidentally compared ==0, or =='\0'. Commented Jun 3, 2011 at 17:49
  • @Seth If something is valid, doesn't mean it should be used. The above is an example how a c++ program shouldn't look like. Commented Jun 3, 2011 at 18:26
  • @VJo just because in your opinion it shouldn't be used doesn't mean it isn't C++. It works fine in my C++ compiler, therefore it is C++. It just happens to also be valid C code. You may make similar arguments against the ternary operator and macros. But they're C++ too. Commented Jun 3, 2011 at 18:37
  • @Seth The thing that it compiles on your compiler doesn't prove anything. Why against ternary operator and macros? Commented Jun 3, 2011 at 18:51

4 Answers 4

5

Strings are terminated by a '\0' character

So simply add after the for loop

command[counter]=0;

(when you exit the for loop the value of counter will be "pointing" at the last character's place in the command variable)

Sign up to request clarification or add additional context in comments.

Comments

3

http://ideone.com/haCBP

Your code is working fine:

output:
this is what i want
this is what i want

Edit: That being said, you need to initialize your output buffer:

char command[sizeof(buffer)]={}; // now the string will be null-termiated
                                 // no matter where the copy ends

4 Comments

sigh Sucks when you can't be sure code is wrong when people say it's wrong.
Using Visual C++ it doesn't print that way :( however after terminating command[counter] = 0 it works
That's why I added my edit. If you initialize your buffer (like you should in most cases) you won't need the termination.
@Blindy: the code is not correct as given in the question. zero-filling the buffer is an effective but wasteful way of covering up the bug in the code (and not something you should do in most cases, or anything like it).
0

There's a bit easier way to do the job:

sscanf(buffer, "%[^0]", command);

This copies the right data and assures it's properly terminated, all in one (reasonably) simple operation.

Note, that in C++ you probably want to use std::string instead of NUL-terminated arrays of char for situations like this though.

2 Comments

sizeof(buffer) can be determined at compile-time, and thus is a perfectly valid size for the command array, even in C++.
@Karl: Oops, you're entirely correct. I apologize for the error.
-1
counter = 0;
while (buffer[counter] != '0'){
command[counter] = buffer[counter];
counter ++;
}

try something like this...but add control to make sure you do not excide the buffer/command dimension!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.