0

New to C, still getting a grasp on pointers. I'm trying to add a copy of a c string into a char array such that

char temp[40];
temp[0] = 'a';
temp[1] = 'b';
temp[2] = 'c';
temp[3] = NULL;
char *container[40] = {0};
strcpy(container[0], temp);
cout << container[0] << endl;

prints "abc". Trying to make a copy because temp is constantly being replaced with new characters and a pointer to temp obviously won't suffice as i want to do this after abc is printed.

char temp[40];
temp[0] = 'd';
temp[1] = 'e';
temp[2] = 'f';
temp[3] = NULL;
char *container[40] = {0};
strcpy(container[1], temp);
cout << container[1] << endl;

prints 'def'. P.S this is what i have so far, but it doesn't work. I'm not too sure if im using strcpy incorrectly, but any alternatives would be helpful.

4
  • 3
    You're not using C but C++. Commented Jun 2, 2015 at 20:21
  • container[0] is just a pointer to a char. You must point it to enough memory before you call strcpy. Commented Jun 2, 2015 at 20:22
  • C or C?? Which is it? These are C techniques, but the C++ language. And you use both terms in your question.... Commented Jun 2, 2015 at 20:46
  • Also, a C string is a char array.... Commented Jun 2, 2015 at 20:46

1 Answer 1

3

You are using strcpy correctly, but you are not giving it proper memory. That is why both your programs have undefined behavior - they write to memory pointed by uninitialized pointers.

To fix this, allocate memory to each element of container using malloc:

char temp[40];
temp[0] = 'a';
temp[1] = 'b';
temp[2] = 'c';
temp[3] = NULL;
char *container[40] = {0};
container[0] = (char*)malloc(strlen(temp)+1);
strcpy(container[0], temp);
cout << container[0] << endl;

You need to add 1 to the result of strlen in order to accommodate the null terminator of your C string. Note that the cast of malloc is necessary only because you are using C++; in C, the cast in front of malloc can be dropped.

Note: I assume that this is a learning exercise to learn ways to work with "raw" pointers, because C++ provides much better facilities for working with strings. In particular, your container could be replaced with a std::vector<std::string> which would grow dynamically as you add items to it, and manage its own memory, thus eliminating the need to use malloc and strcpy.

Sign up to request clarification or add additional context in comments.

3 Comments

Might be worthwhile warning against doing any of this in the first place? Also you misspelt "container".
question. would this be the equiv. to char *container[] = {"abc" ,"def"}. behind the scenes are the parameters being stored as cstrings and the memory is allocated for it? note: i realise they're static values.
@zzzzz Yes, this would be equivalent in terms of a memory layout, except for two things: the container would have two elements instead of 40, and you would need to declare it const char *container[40] with a const, because C++ requires C strings to be declared constant.

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.