0

I have an array called array, and I want to copy a word that I have read in from some file and store it into that array at a certain index, how can I don it?

I thought when a file is read all input from the file is in "string" form. So if a number 100 is in the file its represented as "100".

I have tried the following:

#define maxlength 10

char array[10];
int i = 0;
char word[maxlength];

strcpy(array[i], word); //error
array[i] = word; //error

My guess is because the word is an array of characters it's not actually a string to copy through. Is there a way I can copy the word[] into the array[] in a string format? I.E. index 0 in array = ["hello"] index 1 = ["world"] etc.

4
  • Post true code - a minimal reproducible example. What is maxlength? Commented Sep 2, 2019 at 3:49
  • 1
    "Is there a way I can copy the word[] into the array[] in a string format. i.e index 0 in array = ["hello"] index 1 = ["world"] etc." no. You've got an array of characters. The question is: how do you intend to pack hello in one character? Commented Sep 2, 2019 at 3:51
  • Have you tried strcpy(array, word); ? Commented Sep 2, 2019 at 4:21
  • char array[10] declares an array of 10-char. When referencing an element of an array [ ] acts as a dereference. So while array may be type char * (due to C11 Standard - 6.3.2.1(p3)), array[i] is type char resulting in your attempt to strcpy failing due to array[i] being an incompatible type. Commented Sep 2, 2019 at 4:40

2 Answers 2

2

Your understanding of the concept of a "string" might be wrong.

A string in C is nothing else than an array of characters, with the NUL terminator (character '\0') at the end.

What you have here:

char array[10];

Is not an array of strings, it's just an array of characters. If you want to store an entire string inside array[0], then you'll have to declare your array as an array of arrays of char, like this:

char array[10][10];

Now, each element of your array can hold a string up to 9 characters long (remember, the last one has to be the terminator '\0').

To copy your word, you first have to be sure that it is a valid string (it should, again, end with a '\0'), and make sure it's less than 9 characters long (10 if counting the terminator), because you declared array[10][10]. Once you know that it's a correctly formed string, you can copy it:

strcpy(array[0], word);

If you don't know if word is correctly NUL-terminated, or you don't know if it's too long, then you can fisrt try to copy all the 10 characters with strncpy(), and then ensure it is terminated correctly putting a '\0' at the end manually:

strncpy(array[0], word, 10);
array[9] = '\0';
Sign up to request clarification or add additional context in comments.

1 Comment

Well, I did read the question - it was a bit vague, to be honest, and my answer was an attempt, at least! Still as it seems unpopular, I'll wipe it. But, some of your answer simply repeats what I already said. Hmmm.
-1

First let's clear some syntax errors then we can copy.

  1. cahr word[maxlength]; ---> char word[maxlength]; I hope you have defined maxlength = 10 somewhere because both length should be the same otherwise it will get run time exception.

  2. strcpy(array[i], word); //error ---> strcpy(array, word); strcpy expects destination and source both should be char* type and you are passing array[i] which is only char type as you access element of array by "[i]" So only use "array" in strcpy.

  3. array[i] = word //error ---> don't need this in array copy and even syntactically it is wrong you are trying assigning char* to char.

So our final code snippet would be like this:

char array[10];

char word[10] = {'T','h','a','n','k',' ','y','o','u'};

strcpy(array, word); // word is copied to the array

cout << array; ---> Thank you

2 Comments

Your answer will crash (probably) as word is not null-terminated!
@Adrian as it happens there are 9 initializers for an array of size 10, and the rules are that any elements without initializer in this situation get set to 0 . However it's hard to understand why someone would write this instead of just using a string lteral

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.