Copying string into char array.

Description:
I have to pass a c string into a char array, but if the string is larger then the char array size then it would copy the string array to the just the char array size then end it with a null character. Here is what I have so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
MyString::MyString(const char* s)
   {
   int i;
   for (i = 0; s[i] != 80; i++)
      {
      strcpy(s[i], s.c_str());
      if (s < 80)
         {
         strcpy(s[i], s.c_str());
         s+1 = '\0';
         }      
      }
   
   }


Problem: I know i have to deference the string s and pass it into the char array, but I do not know how to put the limitation on it while putting a null character. I am stumped.

line 4: s[i] != 80, makes no sense, what are you trying to do here?
line 6: s is already a C-string, but strcpy(s[i], s); will get you into trouble, and the step is done again in line 9, why?

strcpy(): Copies the C string pointed by source into the array pointed by destination, including the terminating null character, source: http://www.cplusplus.com/reference/clibrary/cstring/strcpy/
So you are trying to convert a string into a char array?

If so, there is a function you will be very happy about:
1
2
string str = "Hello!";
cout << str.at(0) << str.at(1) << endl;


This will output:
He

So in essence, you can treat a string like a character array, which will be very helpful in your for loop! Just remember to check both conditions that i is less than the number you need (for max copying and the terminating character) AND that the terminating character isn't present in your character array at that spot.

Or if you are trying to convert a character array into a string, this can be easily accomplished with the string concatanation (spelling?) operator: +, and a for loop.

1
2
3
4
string str = "";
/*for loop*/
str += chararray[i]; // str = str + chararray[i]
/*end for loop*/


Hope this helps!

P.S. One other piece of information is that just the name of the array acts as a pointer to it...
1
2
3
4
5
6
7
8
9
char a[5];
function1(a);
/*code here*/

void function1(char *a)
{
  a[0] = 2;
  //etc.
}


The above should work, if not I apologize as it is getting late here. But a quick google of passing arrays to functions in C++ will get you code examples that work if that doesn't.
Last edited on
What I was trying to do was pass a C string in to a char array. When I type the code
s != 80 then that would increment the string until it was to the size 80. The next line of code if (s < 80) makes the C String S pass the string into the char array but adding a null character at the end.

I have passed C Strings into arrays before, but I just do not know how to make it where if hte string is more then 80 then the string only copies 79+null.
In C the normal* thing to do is use strcmp() and/or strncmp() to copy C-strings into another, I think what you're tasked to do is emulate strcmp(), but with limit check, i.e make a safer strcmp(), but easier to use than strncmp(), so probably you want to try this:
- get (declare) a C-string
- pass that string into a function that accepts C-strings
- and copy it into another C-string?
 
char* function_to_copy_one_string_into_another(const char* original, char* copy, int size_of_copy);

Boltftw wrote:
I have passed C Strings into arrays before

then it'll also be good to remember that functions that accept arrays also need the size of the array
*i.e what the functions actually does is copy one array element to another, copy[0] = str[0], etc

edit: arghh! I blame it on hunger pangs -for strcmp() read strcpy(), for strncmp() read strncpy() ;)
Last edited on
strcmp() and strncmp() don't do any copying - they compare the C-strings passed. strcpy() is used for copying.
1
2
3
4
5
6
7
MyString::MyString(const char* s)
   {
   int i;
   
   for (i = 0; *s != '\0'; i++) //loop to go through C string
      strcpy(stringarray, s);   
   }


Okay. I have gotten to the point where I can pass the C string into the array. I just need to implement the limit checker. Could anyone give me any guidance on how I would do this?
1
2
3
4
5
6
7
MyString::MyString(const char* s)
   {
   int i;
   
   for (i = 0; *s != '\0'; i++) //loop to go through C string
      strcpy(stringarray, s);   
   }


So what is 'stringarray'? This will loop infinitely or not all. It doesn't make sense.

EDIT: If you just want to copy: omit the loop.
If you want to copy until a limit use
1
2
strncpy(stringarray, s, LIMIT); // Assuming char stringarray[LIMIT + 1]
stringarray[LIMIT] = '\0'; // Note: Don't forget the terminating 0 
Last edited on
char stringarray[80] is what stringarray is.
Thanks coder you helped me with your example. Sorry I am a week late on the thanks.
Topic archived. No new replies allowed.