Solved

[noob][c++] passing by reference, pointers how

Posted on 2007-11-19
21
371 Views
Last Modified: 2010-04-01
how do I pass an array of strings by value, and by reference?


int ( int n, string array1 )   ?




to access it in the function

is this what i do?



*array[n]


?
0
Comment
Question by:Troudeloup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 5
  • 5
  • +1
21 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 260 total points
ID: 20315230
To pass an array of strings by pointer, you can just use the following:

void function(string *array, int Qty);

Call it like this:

string Myarray[123];

function(Myarray, 123);
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 50 total points
ID: 20315231
>> how do I pass an array of strings by value, and by reference?

You don't pass an array by value, but by reference :


void fun(std::string arr[]) {
    std::cout << arr[0];
}
 
std::string array[5] = { "this", "is", "a", "test", "array" };
fun(array);

Open in new window

0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 190 total points
ID: 20315235
E.g.

int func( int n, string array1 );

would pass the parameters per value, whereas

int func( int& n, string& array1 );

uses references. To pass data by reference, simply use a reference. You could also use pointers like in

int func( int* n, string* array1 );

but a reference is easier to handle, since it cannot be NULL.
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 30

Assisted Solution

by:Axter
Axter earned 260 total points
ID: 20315237
To pass an array by reference is a bit trickier.
It's better to create a type def, and then pass the reference of the type def.
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 190 total points
ID: 20315238
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 260 total points
ID: 20315262
Here's an example for passing by reference, without using typedef.



void function( string (&Myarray)[123] )
{
}
 
//Example usage:
string Myarray[123];
 
function(Myarray);

Open in new window

0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 260 total points
ID: 20315293
FYI:
>>void fun(std::string arr[]) {

That's really passing an array by pointer.
Same thing as the following:
void fun(std::string *arr)

To pass by reference you use the following syntax:
void function( string (&arr)[5] )
0
 

Author Comment

by:Troudeloup
ID: 20315315
in short,

#include <iostream>
using namespace std;

int test ( *array );

int main()
{
    string stringarray[400];
   
    test ( stringarray)
   
}


int test ( *array )
{
    return array;
}








?
   
0
 

Author Comment

by:Troudeloup
ID: 20315317
and change * to & for reference?
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 260 total points
ID: 20315321
A more readable method is to use the typedef.
Example:

typedef string StringArrayOf123[123];
 
void function( StringArrayOf123 &array )
{
}
 
//Example usage:
StringArrayOf123 Myarray;
 
function(Myarray);

Open in new window

0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 190 total points
ID: 20315326
Make that

#include <iostream>
using namespace std;

int test ( string*& );

int main()
{
    string stringarray[400];
   
    test ( stringarray)
   
}


int test ( string*& array )
{
    return 0;
}

0
 

Author Comment

by:Troudeloup
ID: 20315332
wait a minute,

I thought * is supposed to be in the front!

and
what's


*&?
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 190 total points
ID: 20315334
BTW, you might be better off with a 'vector<string>' for that, e.g.

#include <iostream>
using namespace std;

int test ( vector<string>& );

int main()
{
    vector<string> stringarray;
   
    test ( stringarray)
   
}


int test ( vector<string>& array )
{
    return array.size();
}
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 260 total points
ID: 20315344
Example for a reference:

#include <iostream>
using namespace std;
 
typedef string StringArrayOf400[400];
 
int test ( StringArrayOf400 &array );
//or
int test ( string (&array )[400]);
 
int main()
{
    string stringarray[400];
   
    test ( stringarray)
   
}
 
 
int test ( StringArrayOf400 &array )
{
    return 0;
}

Open in new window

0
 

Author Comment

by:Troudeloup
ID: 20315347
no no , no vector for now

i need to learn the dumb way of reinventing the wheels for at least another month
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 190 total points
ID: 20315348
>>what's
>>*&?

That is a reference to a pointer. Regular arrays in C/C++ when used without [] are treated like a pointer to the 1st element, thus the notation.
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 260 total points
ID: 20315359
For pointer:


int test (string *array, int Qty);
 
int main()
{
    string stringarray[400];
   
    test ( stringarray, 400)
   
}
 
int test (string *array, int Qty )
{
    return 0;
}

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20315385
>> >>void fun(std::string arr[]) {
>> 
>> That's really passing an array by pointer.

Depends what definition you use for "by reference" :

        http://www.parashift.com/c++-faq-lite/value-vs-ref-semantics.html#faq-31.1

And it also depends on what Troudeloup meant ...
0
 

Author Comment

by:Troudeloup
ID: 20315392
i wasn't sure (am still not) about the terms ..
0
 
LVL 30

Expert Comment

by:Axter
ID: 20315467
>>Depends what definition you use for "by reference" :

In C++ context, it would be a pointer, and not a reference.
One of the main difference between a pointer and a reference, is that a pointer can be reassigned to point to something else.
You can't do that with a reference.
Example:
void fun1(string arr[])
{
      arr = NULL; //This will compile
}

void ThisWillNotCompile( string (&array )[5] )
{
      arr = NULL; //This will NOT compile
}
 

int main(int argc, char* argv[])
{
      std::string array[5] = { "this", "is", "a", "test", "array" };
      fun1(array);
      ThisWillNotCompile(array);

      return 0;
}

If variable arr was a reference, then you would get a compile error if you tried to assign it to NULL.
You'll notice that func1 compiles, because it's not passing by reference.  The second function does not compile, because it is passing in by reference, and you can not reassign a reference.

That's one was you can tell if you have a reference or a pointer.
0
 
LVL 30

Expert Comment

by:Axter
ID: 20315482
In other languages, a reference means something different, so my above comments only applies to C++ context.

For example, in Java, a reference is really what C++ would call a pointer.
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question