1

I have two arrays:

char line[128] which is populated using:

fgets(line,sizeof line, file)

and

char* array;
  array=(char*) malloc(j*sizeof(char));

where j is some integer.

I'd like to assign an element of "line" to the corresponding element of "array".

Thanks in advance for any help!

1
  • 1
    line[i] = array[i]? Or do I have it backwards? Commented Aug 3, 2012 at 21:44

2 Answers 2

1

As these are char arrays you could just use strcpy

 strcpy(array,line);

taking care that your dynamic array is large enough to accomodate the line array. This would copy the whole array, or for just an individual element,

 array[i] = line[i];

taking care that i is within limits of the arrays.

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

Comments

0
 array[N] = line[N]; // N is the corresponding element's index

But you have to make sure that N is a valid index w.r.t both array and line.

Comments

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.