开发者

Pointers and char*'s

开发者 https://www.devze.com 2023-04-13 04:40 出处:网络
My C++ syntax is quite rusty.This is my code: /* Add hdr of length length to the msg. */ void Message::msgAddHdr(char *hdr, size_t length)

My C++ syntax is quite rusty. This is my code:

/*
Add hdr of length length to the msg.
*/

void Message::msgAddHdr(char *hdr, size_t length)
{
    char *temp;          //temp to iterate through hdr
    size_t i;            //iterator

    temp=hdr;//Set temp to beginning of header

    //Iterate temp to the last letter of the header
    for(i=0; i < length; i++)
    {
        *temp = temp -> next;
    }
    //Delete the null terminator after the hdr
    delete *temp -> next;   
    //Set what temp is pointing to (which should be the last letter of the
    //header) to point to the msg.
    *temp -> next = Message.msg;
    //Delete temp when you're done with it?
    delete temp;

    return;
}

The two big problems I'm having are:

  1. How do I change what my pointer's pointee is pointing at? (i.e. temp points to a pointer inside of the hdr which needs to point to a new location) I have "*temp->next for lack of the correct syntax knowledge.
  2. I have Message.msg being added after the header, but the way this method is going to be called is like this:

    m->msgAddHdr(h1,开发者_StackOverflow社区5);
    

How do I use that m inside of my method?


for(i=0; i < length; i++)
{
    *temp = temp -> next;
}

I think here you mean to increment temp by length elements. That is achieved with

temp += length;

There are no methods available on a char*.

//Delete the null terminator after the hdr
delete *temp -> next;   

Cannot call delete here. You only call delete on something made with new. What's more you cannot delete characters from the middle of a char* C string.

//Set what temp is pointing to (which should be the last letter of the
//header) to point to the msg.
*temp -> next = Message.msg;

Not really sure what you are trying to do here. There is no next. I think we need to know more about Message.msg. At the moment we don't even know what type it is.

//Delete temp when you're done with it?
delete temp;

You did not allocate temp with new so you cannot and should not call delete on it.

return;

No need to do this, a void function will do this when it reaches the end.


More generally I think you need to go back to your textbook and brush up on the basics. Since you are using C++ you can make use of higher level constructs like std::vector and std::string. Once you start using C++ you should really be avoiding char* work altogether. It's messy and hard to get right. The native C++ constructs are much simpler.

I'd strongly recommend you simply throw all this code away and try to come up with a std::string based version.


  1. The temp = temp->next thing you are searching for is ++temp;. So you can simply do temp += length.
  2. You can access to the actual instance's msg with this->msg, or simply msg, if msg is declared in your Message class.


I'm not sure what syntax your code is written in, but it's not really C++, and I'm not really sure what you're trying to accomplish. Anyways, a correct syntax for your code could be:

void Message::msgAddHdr(char* hdr, size_t length)
{
    char* temp = hdr; // declare a pointer to the header
    temp += length; // this sets the pointer to the last letter in header
    for (int i = 0; i < LENGTH_OF_MSG; ++i)
        *(++temp) = Message.msg[i]; // copy message to the end of header
    // also, you sure it's not Message::msg and Message.msg?
    // you need to make sure you have room, and you also need the length of
    // the message

    *temp = '\0'; // null-terminate the header
    // no deletion required since you're not allocating anything here
}

I'm not sure if this will do what you're trying to do. It seems you're just trying to do a basic concatenation.


As other people have mentioned in their answers, you use ++temp to increment a pointer (there is no ->next member on a raw pointer).

But you have a bigger problem, that your function can't work. You have a pointer to a string buffer which you want to append to, but you don't know that the buffer is sufficiently long to do so. There are various options here, but the string manipulation you're doing looks very C-like; I suggest you would be better off using C++ idioms instead, ie. have your function return a string object instead of manipulating an existing buffer, for example:

std::string Message::msgAddHeader(const char* hdr, size_t length) {
    return std::string(hdr, hdr + length) + msg;
}
0

精彩评论

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

关注公众号