Advertisement

05.05.2008 at 09:09AM PDT, ID: 23376794
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

class function
I'm trying to pass a pointer to a pointer into below setFruit function. The program compiled without problem but crashed when i run it. I think it is due to the following code strcpy(); Please do not suggest using sring. I want to do everything using c-string, pointer and dynamic memory allocation.
1:
2:
3:
4:
5:
6:
7:
void fruitType::setFruit(char **fruits)
{
     char** start, **end;
     for (start = fruits, end = &fruits[3]; start <= end; start++)
     	strcpy(*FRUIT, *start);
     
}
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: dandeliondream
Solution Provided By: Infinity08
Participating Experts: 2
Solution Grade: A
Views: 27
Translate:
Loading Advertisement...
05.05.2008 at 10:23AM PDT, ID: 21501480

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.05.2008 at 05:16PM PDT, ID: 21503960

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.05.2008 at 10:39PM PDT, ID: 21504964

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.05.2008 at 10:49PM PDT, ID: 21504996

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.05.2008 at 11:38PM PDT, ID: 21505155

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.05.2008 at 11:42PM PDT, ID: 21505171

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2008 at 05:06PM PDT, ID: 21529527

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2008 at 05:43PM PDT, ID: 21529655

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2008 at 10:19PM PDT, ID: 21530523

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2008 at 10:37PM PDT, ID: 21530569

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 09:18AM PDT, ID: 21534353

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.10.2008 at 06:10AM PDT, ID: 21538916

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.10.2008 at 06:47AM PDT, ID: 21539034

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
05.05.2008 at 10:23AM PDT, ID: 21501480

Rank: Genius

A few questions :

1) are there 4 fruits in the parameter array ? Because that's the amount you read in the loop.

2) What is FRUIT ? Why use *FRUIT ? Is memory allocated for *FRUIT ? Why are you overwriting *FRUIT with every iteration ?
 
05.05.2008 at 05:16PM PDT, ID: 21503960
1) oops, it should be 3 fruits
2) Please see my code below. What i'm trying to achieve is to input 3 fruits
e.g.
Australian Mango
New Zealand Kiwi
USA Royal Apple

and display it on the screen like:
Australian Mango
New Zealand Kiwi
USA Royal Apple
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
#include <iostream>
using namespace std;
 
void addFruit();
 
const int fruitSize = 40;
     
class fruitType
{
    public:
          fruitType(); //default constructor: allocate new d.m.
       
          //set functions
          void setFruitInfo(char**);
          
          void setFruits(char**); //set Fruits' name
         
          char* getFruits() const; //return Fruits' name
          
          void showFruits() const; //display Fruits' name
          
      private:
              char *Fruits[3];       //Fruits pointer
              
}; //end class fruitType
 
fruitType::fruitType()
{
      
      for ( int i = 0; i < 3; i++)
      {
          try
          {
               Fruits[i] = new char[fruitSize];
               memset(Fruits[i],'\0',sizeof(char)*fruitSize);
          }
      
         catch (bad_alloc b)
         {
             cout << "Could not allocate Fruits in dynamic memory." << endl;
             exit(1);
         }
      }   
      
}//end function fruitType::fruitType
 
//modifying the data members by passing input to their respective function 
void fruitType::setFruitInfo(char** Fruits)
{
     setFruits(Fruits);
     
}//end function fruitType::setFruitInfo
 
 
void fruitType::setFruits(char **Fruits)
{
     char** start, **end;
     for (start = Fruits, end = &Fruits[2]; start <= end; start++)
     {
         cout << "..........";
         strcpy(*Fruits, *start);
     }
}
 
char* fruitType::getFruits() const //return fruit
{
    for ( int i = 0; i < 3; i++)
         return Fruits[i];
}//end function fruitType::Fruits() 
 
void fruitType::showFruits() const //display Fruits
{
     for ( int i = 0; i < 3; i++)
         cout << Fruits[i];
}//end function fruitType::showFruits
 
int main()
{
    addFruit();
    system("PAUSE");
    return 0;
}
 
void addFruit()
{
     fruitType a; //declare an object of fruitType
     
     char *Fruits[3];
     char **startA, **endA;
         
     //allocate dynamic memory for 4 Fruits
     try
     {  
          
          for (startA = Fruits, endA = &Fruits[2]; startA <= endA; startA++)
              *startA = new char[fruitSize];
     }
     catch (bad_alloc b)
     {
          cout << "Could not allocate memory to Fruits.";
          exit(1);
     }  
     
     cout << "---------Add Fruit---------" << endl;
     int count = 1; //use to display fruit position
     //input up to 4 Fruits
     for (startA = Fruits, endA = &Fruits[2]; startA <= endA; startA++)
     {
         cout << "Enter Fruit " << count << " : "; //e.g Australian Mango
         cin.getline(*startA, fruitSize);
         count++;
     }
     
     a.setFruitInfo(Fruits);
     a.showFruits();
          
}//end of addFruit function
Open in New Window
 
05.05.2008 at 10:39PM PDT, ID: 21504964

Rank: Wizard

I compiled and ran your program and it didn't crash.  Does it still crash for you?

It did fail to display anything.

What is your question now?
 
05.05.2008 at 10:49PM PDT, ID: 21504996
yeah, the program doesn't crash now but how can i display the fruits....and i'm also unsure if the following is incorrect. Actually i wanted to use pointer notation instead of array notation to do it but i dunno how.

char* fruitType::getFruits() const //return fruit
{
    for ( int i = 0; i < 3; i++)
         return Fruits[i];
}//end function fruitType::Fruits()
 
05.05.2008 at 11:38PM PDT, ID: 21505155

Rank: Genius

1) in your setFruits method, you have this :

         strcpy(*Fruits, *start);

Notice that the parameter of the setFruits method is also called Fruits :

        void fruitType::setFruits(char **Fruits)

It's hiding the class member.

You should use a different name as parameter, or use this->Fruits to refer to the clas member.

Also, notice that you're always overwriting the first fruit with the strcpy !



2) in your getFruits method, you have a return inside a loop - when the first return is executed, the others won't matter any more !!!
Accepted Solution
 
05.05.2008 at 11:42PM PDT, ID: 21505171

Rank: Wizard

Well, Infinity08 beat me to it, but I will post my painstakingly composed answer (which says the same thing) anyway, in case it adds any clarity.

In the fruitType::setFruits function, you have a parameter named Fruits.  The function copies Fruits[0], [1], and [2] to Fruits[0].  Then the function exits and the Fruits parameter disappears.  You wanted to copy these strings into the fruitType member Fruits, but because the parameter had the same name, it was used as the destination instead.  There was no way for the compiler to know which Fruits you had in mind and the language rules say to use the parameter rather than the class member, because it is more local.  If you give these two variables different names, it might work better.

The getFruits function you showed is indeed incorrect.  The return statement exits the for loop, so the function will always return Fruits[0].  To fix it, you could add a parameter to specify which array element the function is to return, or change the return type so it can return a pointer to an array of pointers.
Assisted Solution
 
05.08.2008 at 05:06PM PDT, ID: 21529527
>>"No, the start address does not change with every iteration. Where do you think it does that ?"

i thought start++ will change the start address?

For the report abuse, I will use that next time.
 
05.08.2008 at 05:43PM PDT, ID: 21529655

Rank: Wizard

Infinity08 is correct that the first element of the Fruits array gets overwritten.  dandeliondream is correct that the start address changes for every loop.

The first argument to strcpy specifies the destination of the copying operation.  The second argument specifies the source to be copied.  The start variable is initialized to Fruits, so the first time through the loop, the strcpy copies *Fruits to itself.  Then the for statement increments start so it points to Fruits[1].  The destination Fruits is still the same as Fruits[0].  So on the second iteration of the loop, it copies Fruits[1] to Fruits[0], overwriting the original content of Fruits[0] with the content of Fruits[1].

So what the loop does is:

copy Fruits[0] to Fruits[0]
copy Fruits[1] to Fruits[0]
copy Fruits[2] to Fruits[0]

This doesn't seem very useful.  Assuming the intention was to copy the strings from the Fruits parameter to the Fruits member, one part of the solution is to use different names for the parameter and the member, as we noted above, and another part is to change it so it doesn't copy all three strings to the same place, but instead copies them to the corresponding different members of the destination array.  That is, the desired function is:

copy fruitsParameter[0] to fruitsMember[0]
copy fruitsParameter[1] to fruitsMember[1]
copy fruitsParameter[2] to fruitsMember[2]

I hope this helps.

--efn
 
05.08.2008 at 10:19PM PDT, ID: 21530523

Rank: Genius

>> i thought start++ will change the start address?

Yes, but that doesn't change the destination array start address ... in other words, you're always writing to the same location in the destination array.

But I see efn already explained it nicely :)
 
05.08.2008 at 10:37PM PDT, ID: 21530569

Rank: Genius

>> For the report abuse, I will use that next time.

Btw, that doesn't make a lot of sense ... Do you plan to close questions prematurely again ?

There's two options as far as I can see :

1) either you acknowledge that the B grade was incorrect, and then you can fix it with the "Report Abuse" functionality
2) or you think the grade was correct, and in that case I'd like an explanation for the B grade to know what I could have done better, and to learn from that for future questions
 
05.09.2008 at 09:18AM PDT, ID: 21534353
efn & infinity08  thanks for clearing my doubt.

...i have submitted the report abuse and hopefully the grade can be upgraded :)
 
05.10.2008 at 06:10AM PDT, ID: 21538916
Regraded to 'A' at dandeliondream's request.

modus_operandi
EE Moderator
 
05.10.2008 at 06:47AM PDT, ID: 21539034

Rank: Genius

Thank you, dandeliondream. So, do you have further problems with this ?
 
 
05.07.2008 at 10:11PM PDT, ID: 21522456
May I ask why you gave a B grade ? That usually means that something was missing in the answers and/or that something is still unclear. If so, you don't have to accept an answer yet, and you can always ask for clarification.
 
 
05.08.2008 at 07:44AM PDT, ID: 21525170
yah i'm still unsure of the following:
>>Also, notice that you're always overwriting the first fruit with the strcpy !

I don't understand why the first fruit is overwritten? Ain't the start address change for every loop?
 
 
05.08.2008 at 07:49AM PDT, ID: 21525221
>> Ain't the start address change for every loop?

No. Where ?

What is the current code you are using ?


Btw, as I said earlier : since your question has not been completely answered, there's no need to close it yet. You can ask to get the question re-opened by clicking the "Report Abuse" link and explaining what you want.
 
 
05.08.2008 at 09:06AM PDT, ID: 21525933
this is the part where you mentioned >>Also, notice that you're always overwriting the first fruit with the strcpy !
I don't understand why the first fruit is overwritten? Ain't the start address change for every loop?

for (start = Fruits, end = &Fruits[2]; start <= end; start++)
     {
         cout << "..........";
         strcpy(*Fruits, *start);
     }

Report abuse: i have not use it b4 i think it's ok to leave it as it is....or is there a need to use it?
 
 
05.08.2008 at 10:48AM PDT, ID: 21526883
>> I don't understand why the first fruit is overwritten? Ain't the start address change for every loop?

The "No. Where ?" in my previous post was a response to your question ;) Here's a more elaborate version :

"No, the start address does not change with every iteration. Where do you think it does that ?"


>> Report abuse: i have not use it b4 i think it's ok to leave it as it is....or is there a need to use it?

Do you understand what the grading system is for ?

        http://www.experts-exchange.com/help.jsp#hi97
 
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628