Solved

pass it array to function and back

Posted on 2007-11-30
8
181 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
[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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Troudeloup
ID: 20386776
what would it mean if the function has * ?
0
 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

696 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