C++/C++

Advertisement


Question
string* addEntry(string *dynomicArray, int &size, string newEntry)


first:
     why did i write * before the function addEntry ?
second:
     here is my code and it gives me error on compilation
in the main fuction, at the line addEntry

#include <iostream>
#include <string>
using namespace std;
string *addEntry(string *dynamicArray, int &size, string newEntry)
{
   size = size+1;
   string *newarray;
   newarray = new string[size];
   for(int i=0 ; i<(size-1) ; i++)
   {
       newarray[i] = dynamicArray[i];
   }
   newarray[(size-1)] = newEntry;
   cout<<endl;
   cout<<"The New Array is : "<<endl;\
   cout<<endl;
   delete []dynamicArray ;
   return newarray;
}
int main()
{
   string *p;
   p = new string[5];
   for(int i=0 ; i<5 ; i++)
   {
       cin>>p[i];
   }
   cout<<endl;
   for(int i=0 ; i<5 ; i++)
   {
       cout<<p[i]<<" ";
   }
   cout<<endl;
   addEntry (p, 5, "elwa7sh");
   return 0;
}
please explain it to me !
thanks,

Answer
Hello yousry

In the line:
string *addEntry(string *dynamicArray, int &size, string newEntry)
The * before addEntry means that the function will return a pointer to a string, instead of returning an copy of a string. Your code is actually returning an array of strings, but arrays are passed around as pointers to the first element of the array. There is no way to distinguish between returning a pointer to a single string, and a pointer to an array of strings.

The second parameter (size) is a reference to an integer. Passing a parameter by reference allows the function to change the parameter, and have the change visible to the caller of the function. For example, your function takes size, and adds 1 to it. The change in size would be visible in main.

To pass in an integer by reference, you must have an integer variable and the variable must be passed in. You cannot pass in a number like 5 to a function which takes an integer reference, because it would not make sense to change the value of 5. 5 is always 5.

Since addEntry returns a string pointer, you will need to receive the result. Have a look at how I did it below.

#include <iostream>
#include <string>
using namespace std;
string *addEntry(string *dynamicArray, int &size, string newEntry)
{
   size = size+1;
   string *newarray;
   newarray = new string[size];
   for(int i=0 ; i<(size-1) ; i++)
   {
       newarray[i] = dynamicArray[i];
   }
   newarray[(size-1)] = newEntry;

   delete []dynamicArray ;
   return newarray;
}
int main()
{
   string *p;
   p = new string[5];
   for(int i=0 ; i<5 ; i++)
   {
       cin>>p[i];
   }
   cout<<endl;
   for(int i=0 ; i<5 ; i++)
   {
       cout<<p[i]<<" ";
   }
   cout<<endl;

   int arraySize = 5;
   p = addEntry (p, arraySize, "elwa7sh");

   cout<<endl;
   cout<<"The New Array is : "<<endl;\
       cout<<endl;
   for(int i=0 ; i<arraySize ; i++)
   {
       cout<<p[i]<<" ";
   }
   cout<<endl;
   return 0;
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2016 About.com. All rights reserved.