开发者

Character Pointers (allotted by new)

I wrote the following code:

char *pch=new char[12];
char *f=new char[42];
char *lab=new char[20];
char *mne=new char[10];
c开发者_Go百科har *add=new char[10]; 

If initially I want these arrays to be null, can't I do this:

*lab="\0";
*mne="\0";
and so on.....

And after that if I want to add some cstring to an empty array can't I check:

 if(strcmp(lab,"\0")==0)
 //then add cstring by *lab="cstring";

And if I can't do any of these things, please tell me the right way to do it...


In C++11, an easy way to initialize arrays is by using brace-initializers:

char * p = new char[100] { 0 };

The reasoning here is that all the missing array elements will be zero-initialized. You can also use explicit value-initialization (I think that's even allowed in C++98/03), which is zero-initalization for the primitive types:

char * q = new char[110]();


First of all, as DeadMG says, the correct way of doing this is using std:string:

std::string lab; // empty initially, no further initialization needed

if (lab.size() == 0) // string empty, note, very fast, no character comparison
    lab += "cstring"; // or even lab = "cstring", as lab is empty

Also, in your code, if you insist in using C strings, after the initialization, the correct checking for the empty string would be

if (*lab == '\0')


First of all, I agree with everybody else to use a std::string instead of character arrays the vast majority of the time. Link for help is here: C++ Strings Library

Now to directly answer your question as well:

*lab="\0";
*mne="\0";
and so on.....

This is wrong. Assuming your compiler doesn't give you an error, you're not assigning the "null terminator" to those arrays, you're trying to assign the pointer value of where the "\0" string is to the first few memory locations where the char* is pointing to! Remember, your variables are pointers, not strings. If you're trying to just put a null-character at the beginning, so that strlen or other C-string functions see an "empty" string, do this: *lab='\0'; The difference is that with single-ticks, it denotes the character \0 whereas with double, it's a string literal, which returns a pointer to the first element. I hope that made sense.

Now for your second, again, you can't just "assign" like that to C-style strings. You need to put each character into the array and terminate it correctly. Usually the easiest way is with sprintf:

sprintf(lab, "%s", "mystring");

This may not make much sense, especially as I'm not dereferencing the pointer, but I'll walk you through it. The first argument says to sprintf "output your characters to where this pointer is pointing." So it needs the raw pointer. The second is a format string, like printf uses. So I'm telling it to use the first argument as a string. And the 3rd is what I want in there, a pointer to another string. This example would also work with sprintf(lab, "mystring") as well.

If you want to get into C-style string processing, you need to read some examples. I'm afraid I don't even know where to look on the 'net for good examples of that, but I wish you good luck. I'd highly recommend that you check out the C++ strings library though, and the basic_string<> type there. That's typedef'd to just std::string, which is what you should use.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜