1

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];
char *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...

Nathan Arthur
  • 8,287
  • 7
  • 55
  • 80
AvinashK
  • 3,309
  • 8
  • 43
  • 94

3 Answers3

3

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]();
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 1
    I wouldn't have put the C++11 answer first; it's likely to be irrelevant to the questioner at this point in time. – Mark Ransom Sep 19 '11 at 22:09
  • @Mark: I was confused at first and got it the wrong way round. After checking, I literally swapped the two standards around. Now I'm not sure if I should edit it, time is clearly on my side here :-) – Kerrek SB Sep 19 '11 at 22:10
  • @KerrekSB...my compiler isnt accepting char *p=new char[100](0)....so i am using the latter method...does this also initialize each element of the array to 0 i.e. '\0'... – AvinashK Sep 20 '11 at 03:59
  • @Avinash: You got it mixed up: either `char[100] {0}`, or `char[100]()`. What's you're compiler? Yes, everything will be zero. – Kerrek SB Sep 20 '11 at 08:07
1

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')
Diego Sevilla
  • 28,636
  • 4
  • 59
  • 87
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.

Kevin Anderson
  • 6,850
  • 4
  • 32
  • 54