0
char first_array[5][4] = {"aaa","bbb","ccc","ddd","eee"};
char second_array[1][4];

How would I copy, for example, the third element in first_array ("ccc") and save it to second_array?

The syntax below is clearly wrong, but this is what I'm asking for:

second_array[0] = first_array[2];

Also, after copying, I also want to know how to compare elements in the two arrays. Again, the syntax below might be wrong, I'm just explaining what I'm trying to do:

if(second_array[0] == first_array[2]){ printf("yes"); } //should print yes
2
  • 1
    strncpy(second_array[0], first_array[2], 4); or perhaps memcpy()? Commented Mar 13, 2015 at 23:14
  • 1
    Perfect, strncpy works Commented Mar 13, 2015 at 23:19

2 Answers 2

2

You can't assign to arrays in c, you can fill arrays with some library functions like strcpy(), so

second_array[0] = first_array[2];

would be

strcpy(second_array[0], first_array[2]);

you must however ensure that the destination array fits the number of characters you are copying to it.

If you try to compare two strings in c, you can't do it through the == operator, because strings in c are arrays of char which contain a sequence of non-nul characters followed by a nul character, so if you write this

if (second_array[0] == first_array[2])

even when you succeeded at copying the data, the result will be most likely false, because you are not comparing the contents of the arrays, but their addresses, so to compare them correctly there is also a function strcmp() then the correct way of comparing the strings is

if (strcmp(second_array[0], first_array[2]) == 0)

The functions above require you to include the string.h header, and also that the passed strings are strings in the c sense, i.e what I described above.

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

2 Comments

Great! strncpy and strcmp is exactly what I need
@0100100001101001 be careful with strncpy as it does not generate a null-terminated string in some cases. Instead check the buffer sizes and use strcpy/memcpy; or use snprintf.
-1

I was recently trying to do this, as well: it is not possible to do this sort of direct assignment in C.

When you write first_array[0], the compiler will read that as an address which points to the first element (character) of first_array[2], not the entire string. When you run the assignment, if it were to work, it would only set the first character.

The easiest way is to use strncpy or memcpy (or a loop to cycle through the string.

6 Comments

Did you ask this on SO?
Why would a loop be necessary?
We have a 2D array; this direct assignment isn't possible. A loop is necessary if you use strong copy.
@baum - Not sure what you mean by strong copy, however a loop is not required since you only need to use strcpy or strncpy to get the desired result.
Curious, why Unfortunately?
|

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.