| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Characters 2 dimension array
Hi , I am stuck with two dimension array of characters.
for example, I have 4 lines in a file( lines should be treated as arrays of string) called source: i am not good at C. i hope improve soon. i need help. it is very cold. I want read the first two lines and store them in a 2-dimension array of characters called destination. I know how to read (all the lines but not the first two) but when I tried to input the line in destination I get errors: I tried strcpy, memcpy... What can I do please? B |
|
#2
|
|||
|
|||
|
I am not clear with your quesition. Your task is to read first two lines in a 2D array called destination. What is the problem exactly?
You say you know how "to read all lines but not first two?"What error s are you getting? |
|
#3
|
|||
|
|||
|
Quote:
Ok this is my code: Code:
while(fgets(source,19,fin)!=NULL) {/* while begins*/
for(i=0;i<2;i++)
{
strcpy(destination[i],source[i]);/*this is where I mess up*/
fputs(source,fout);
}
for(i=0;i<19;i++)
fputs(destnation[i],fout);}
The problem is this: I have a file called source.dat,which contain a number of lines. My program must ( after reading of course the first two lines) store them into another 2-D arry called destination.then I print them sepraretely in the output file. My idea is to copy what I read from the file and put it in the array using srtcpy. I get the message :[Warning] passing arg 1 of `strcpy' makes pointer from integer without a cast whem I try to introduce the double subscript like destination [i][j], it still give me error. please, I need help since I dont suceed to find a solution. Thank you B |
|
#4
|
||||
|
||||
|
How did you declare destination[][] and source[][]? the code isn't posted.
__________________
This is my code. Is it not nifty? "The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools." ---Douglas Adams Join the Itsacon fanclub! Zero Tolerance: Spammers banned so far: 280
![]() |
|
#5
|
|||
|
|||
|
Quote:
Destination is a 2D array of characters. Source array is 1 D array of characters as it is filled by the input file vie getline(). |
|
#6
|
||||
|
||||
|
Could be the compiler gets confused with the double array.
Try hard-casting the value to a pointer, like this: Also, if source is a 1D array, you probably shouldn't index it in the strcpy or If all else fails, you might be better of copying the characters one by one. This also avoids any possible problems with assigning the wrong dimension of the 2D array. These two make a huge diffence memory-wise, for example: and Last edited by Itsacon : March 17th, 2006 at 07:52 AM. |
|
#7
|
|||
|
|||
|
Problem is that strcpy takes a single pointer to character. The code given as problem takes a double pointer to character as first argument where it is given error.
source is a sinlge pointer to character. and theus used as a source[i] --- with index . |
|
#8
|
||||
|
||||
|
So source is an array of character pointers?
This would be so much easier if we had the complete code... |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Characters 2 dimension array |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|