Hi, I am trying to make a method inside a class that will remove the first 2 characters of a string of characters. Sounds simple enough, I though it was, but actually implementing it took a bit of thought and doesn't seem to work.

void classname::removeFirstChars(char * buffer)
{
       char actualMessage[sizeof(buffer)-2];       
       int a=0;

// loop through starting from third character of buffer and copy char 
// by char to actualMessage. (i.e. a=0, a=1, start at a=2)
       while (strlen(buffer) > a)
       {
          if ( a > 1 )
          {
             actualMessage[a-2] = buffer[a];
          }
          a++;
       }

// after doing this i have tried concattenating a null character '\0' to the
// end but this doesn't seem to work either

       if (buffer[0] == 'm')
       {
          MessageBox(0,actualMessage, inet_ntoa(_incoming.sin_addr),0);
       }
       else if (buffer[0] == 's')
       {
          system(actualMessage);
       }
       buffer[0]='\0';
       actualMessage[0]='\0';
}

As you can probably see this code is to take a message in the format of
s#message here or m#message here and then take the actual message part and deal with it accordingly either by putting it into a message box or using it as a system command. Any help?

First of all: Why are you using char[] with C++? You should use strings.
This line : char actualMessage[sizeof(buffer)-2]; is causing some troubles.
Since you have shown effort in making it yourself:

void cremoveFirstChars(char * buffer)
{
char* actualMessage = new char[sizeof(buffer)]; 
for (int a = 0; a <=strlen(buffer) - 1; a++)
actualMessage[a] = buffer[a+2];
actualMessage[strlen(buffer) -1]= '\0';
std::cout << actualMessage;
}

This is what should do the trick I think, I didn't test it but I should work..

Edited 6 Years Ago by Nick Evan: n/a

yea thanks, that works fine, i've just been overthinking things and its a while since i've worked with C++.

How about a slight modification that uses the the knowledge that c-strings are always terminated with \0:

void cremoveFirstChars(char * buffer)
{
    char* actualMessage = new char[sizeof(buffer)]; 
    p = -1;                          // initialize the pointer
    do
    {
        p++;                         // next character
        actualMessage[p] = buffer[p+2];
    } while (!actualMessage[p]);     // stop after the \0 is moved
    std::cout << actualMessage;
}

And niek_e, please watch your intenting -- make your code readable. Teach by example. :)

Just noticing a few things (using WaltP's code as a reference):
1) using sizeof(buffer) will not give you the right length since buffer is a pointer, so it'll probably be the same size as an int. You don't want actualMessage to be 4 chars long (assuming an int is 4 bytes). You should use strlen(buffer) rather than sizeof(buffer) .
2) memory allocated needs to be deleted. ;)

[edit:] here's an example of what I mean in part 1:

#include <stdio.h>

void foo(char* c)
{
        printf("size: %d\n", sizeof(c));
}

int main()
{
        char array[128] = {0};
        printf("size: %d\n", sizeof(array));
        foo(array);
        return 0;
}

output:

$ ./a.out 
size: 128
size: 4

[edit 2:] Also noticed: the OPs method of allocating a dynamic array on the stack is only supported by a couple compilers and isn't very portable. Probably want to use new/delete (or malloc/free) instead, as the other replies have done.

please watch your intenting -- make your code readable. Teach by example

Tss.. By now you should know that I always use indenting, but I missed it just this once. What kind of programmer do you think I am?

memory allocated needs to be deleted.

You're absolutely right, I made the snippet form the top of my head and I just forgot it.

Edited 6 Years Ago by Nick Evan: n/a

Tss.. By now you should know that I always use indenting, but I missed it just this once.

Sorry, but I look at hundreds of posts a day. I don't memorize every individual's style and if I see something wrong, I sometimes point it out. It was just your turn ;)

This article has been dead for over six months. Start a new discussion instead.