Solved

pass it array to function and back

Posted on 2007-11-30
8
178 Views
Last Modified: 2010-04-01
#include <iostream>

using namespace std;

int parse ( int array );

int main ()
{
      
      int array[5];
      array[0] = 4;
      array[1] = 3;
      array[2] = 2;      
      array[3] = 1;
      array[4] = 0;
      
      array = parse ( array );
      
      cout << array[0] << endl;
      return 0;
}

int parse ( int array )
{
      
      array[0] = 0;
      array[1] = 1;
      array[2] = 2;      
      array[3] = 3;
      array[4] = 4;

      
      
      
      return array;
}




this doesn't work, how do I fix it?
0
Comment
Question by:Troudeloup
  • 4
  • 4
8 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20386745
you are not "receiving"  an array, it should be:

int *parse ( int *array )
{
     
      array[0] = 0;
      array[1] = 1;
      array[2] = 2;      
      array[3] = 3;
      array[4] = 4;
     
      return array;
}

but there are other problems:

array = parse ( array );
     
this is useless and not allowed, should be:

parse ( array );
     

0
 

Author Comment

by:Troudeloup
ID: 20386750
yes i get it, because the function works on a pointer.

but i don't get why the function also has *

what is the * of ?




is this the fixed version of that?



#include <iostream>

using namespace std;

int parse ( int array );

int main ()
{
     
      int array[5];
      array[0] = 4;
      array[1] = 3;
      array[2] = 2;      
      array[3] = 1;
      array[4] = 0;
     
      parse ( array );
     
      cout << array[0] << endl;
      return 0;
}

int *parse ( int *array )
{
     
      array[0] = 0;
      array[1] = 1;
      array[2] = 2;      
      array[3] = 3;
      array[4] = 4;

     
     
     
      return array;
}


0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20386767
indeed it could be:

void parse ( int *array )
{
// etcetera
}

also, the prototype at the beginning always must match the real function:

void parse ( int *array );

0
 

Author Comment

by:Troudeloup
ID: 20386776
what would it mean if the function has * ?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20386789
which one?
0
 

Author Comment

by:Troudeloup
ID: 20386811
int *parse ( int *array )
{
     
      array[0] = 0;
      array[1] = 1;
      array[2] = 2;      
      array[3] = 3;
      array[4] = 4;
     
      return array;
}
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 20386826
in that case you return a copy of the original array, this could be helpful in some situations like:

instead of:
parse (array)
func1 (array)

you can do:
func1 (parse(array));

but I think it is not the case here.
0
 

Author Comment

by:Troudeloup
ID: 20386829
not a single clue of what that does ;p
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

895 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now