1

I need an empty char array, but when i try do thing like this:

char *c; 
c = new char [m];
int i;
    for (i = 0; i < m; i++)
    c[i] = 65 + i;

and then I print c. can see that c = 0x00384900 "НННННННээээ««««««««юоюою" after cycle it becomes: 0x00384900 "ABCDEFGээээ««««««««юоюою" How can I solve this problem? Or maybe there is way with string?

5
  • 1
    what are you trying to achieve? And what does "empty char array" means? Commented Jun 18, 2012 at 15:49
  • what output do you expect? Are you trying to create a string that you can then print e.g. with printf? Commented Jun 18, 2012 at 15:49
  • i need string and then print it Commented Jun 18, 2012 at 15:51
  • How big is m? It appears that this worked correctly if m == 7. Can you be more specific with your question? What were you expecting? Commented Jun 18, 2012 at 15:53
  • @twalberg: The lines shown are fine (if questionable). What's important are the lines after this. Commented Jun 18, 2012 at 16:10

5 Answers 5

3

If you're trying to create a string, you need to make sure that the character sequence is terminated with the null character \0.

In other words:

char *c; 
c = new char [m+1];
int i;
    for (i = 0; i < m; i++)
    c[i] = 65 + i;
c[m] = '\0';

Without it, functions on strings like printf won't know where the string ends.

printf("%s\n",c); // should work now
Sign up to request clarification or add additional context in comments.

1 Comment

This won't give an empty char array, as long as c[m] creates only a 'valid' char array. Of course, if strlen(c) == 0 is considered as a criteria of validity.
3

If you create a heap array, OS will not initialiase it. To do so you hvae these options:

  1. Allocate an array statically or globally. The array will be filled with zeroes automatically.
  2. Use ::memset( c, 0, m ); on heap-initialised or stack array to fill it with zeroes.
  3. Use high-level types like std::string.

Comments

2

I believe that's your debugger trying to interpret the string. When using a char array to represent a string in C or C++, you need to include a null byte at the end of the string. So, if you allocate m + 1 characters for c, and then set c[m] = '\0', your debugger should give you the value you are expecting.

2 Comments

He says he's trying to print the string in his comment.
Oh, that wasn't there when I answered. The solution remains the same, though.
1

If you want a dynamically-allocated string, then the best option is to use the string class from the standard library:

#include <string>

std::string s;
for (i = 0; i < m; i++)
    s.push_back(65 + i);

Comments

0

C strings are null terminated. That means that the last character must be a null character ('\0' or just 0).

The functions that manipulate your string use the characters between the beginning of the array (that you passed as parameter, first position in the array) and a null value. If there is no null character in your array the function will iterate pass it's memory until it finds one (memory leak). That's why you got some garbage printed in your example.

When you see a literal constant in your code, like printf("Hello");, it is translate into an array of char of length 6 ('H', 'e', 'l', 'l', 'o' and '\0');

Of course, to avoid such complexity you can use std::string.

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.