C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 14th, 2006, 07:15 PM
brad sue brad sue is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 41 brad sue User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 49 m 31 sec
Reputation Power: 3
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

Reply With Quote
  #2  
Old March 14th, 2006, 08:56 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
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?

Reply With Quote
  #3  
Old March 15th, 2006, 12:05 AM
brad sue brad sue is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 41 brad sue User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 49 m 31 sec
Reputation Power: 3
Quote:
Originally Posted by Cirus
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?


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

Reply With Quote
  #4  
Old March 17th, 2006, 07:18 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 997 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 26 m 27 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
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

Reply With Quote
  #5  
Old March 17th, 2006, 07:43 AM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
Quote:
Originally Posted by Itsacon
How did you declare destination[][] and source[][]? the code isn't posted.


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().

Reply With Quote
  #6  
Old March 17th, 2006, 07:49 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 997 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 26 m 27 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
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

C Code:
Original - C Code
  1. strcpy((char*) destination[i], (char*) source);

or
C Code:
Original - C Code
  1. strcpy(&destination[i][0], &source[0]);


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:
C Code:
Original - C Code
  1. strcpy(&destination[i][0], &source[0]);

and
C Code:
Original - C Code
  1. strcpy(&destination[0][i], &source[0]);

Last edited by Itsacon : March 17th, 2006 at 07:52 AM.

Reply With Quote
  #7  
Old March 17th, 2006, 08:17 AM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
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 .

Reply With Quote
  #8  
Old March 17th, 2006, 08:43 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 997 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 26 m 27 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
So source is an array of character pointers?

This would be so much easier if we had the complete code...

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Characters 2 dimension array


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT