Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using strcpy with a string array in C

I have a character array defined as follows: char *c[20];
I'm trying to do: strcpy(c[k], "undefined); but it's not working

I've also tried defining it as char c[20][70] with no luck.

Edit: I actually know it's an array of character arrays, I need it like that.

like image 297
Adam Avatar asked Aug 31 '11 00:08

Adam


People also ask

Can you use strcpy on array?

C++ c_str() function along with C++ String strcpy() function can be used to convert a string to char array easily. The c_str() method represents the sequence of characters in an array of string followed by a null character ('\0'). It returns a null pointer to the string.

Does strcpy work with string?

The strcpy() function operates on null-ended strings. The string arguments to the function should contain a null character (\0) that marks the end of the string. No length checking is performed. You should not use a literal string for a string1 value, although string2 may be a literal string.

What is the correct syntax for strcpy ()?

Syntax: char* strcpy(char* dest, const char* src); Parameters: This method accepts the following parameters: dest: Pointer to the destination array where the content is to be copied.

Does strcpy overwrite a string?

If the destination index is greater than the source index, destination characters will be overwritten. For example: strcpy(s, "abcdef"); strcpy(s+1, s); The second strcpy() copies s[0], i.e. 'a' into s[1]; then copies s[1] into s[2]; then copies s[2] into s[3]; etc.


2 Answers

That's not a character array; that's an array of character pointers. Remove the * to make it a character array:

char c[20];

Then you can use strcpy:

strcpy(c, "undefined");

If you really did want an array of character arrays, you'll have to do what you said you tried:

// array that holds 20 arrays that can hold up to 70 chars each
char c[20][70];

// copy "undefined" into the third element
strcpy(c[2], "undefined");

The problem could have been you're missing the closing ", I don't know if that was a paste error though. Or, the problem could have been that you're using k without defining it, we can't know without seeing the error message you get.

If you want to set them all to that string, then just loop over them:

char c[20][70];

int i;
for (i = 0; i < 20; ++i)
    strcpy(c[i], "undefined");
like image 149
Seth Carnegie Avatar answered Sep 19 '22 00:09

Seth Carnegie


If what you want is to have 20 strings of 70 chars each then your second option should work:

char c[20][70];
for (int k = 0; k < 20; k++)
    strcpy(c[k], "undefined");

The char *c[20] definition is incorrect because you are just defining an array of 20 pointers, and the pointers are not initialized. You could make it work in this way:

char *c[20];
for (int k = 0; k < 20; k++) {
    c[k] = malloc(70);
    strcpy(c[k], "undefined");
}

// then when you are done with these strings
for (int k = 0; k < 20; k++)
    free(c[k]);
like image 38
Miguel Avatar answered Sep 19 '22 00:09

Miguel