Solved

pass it array to function and back

Posted on 2007-11-30
8
180 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Suggested Solutions

Title # Comments Views Activity
C++ get user from AD  (VS6) 7 82
Excel/Word Add-in in what language? 5 128
Question of running the program built from sample in codeproject website 3 82
What is sub-make ? 2 71
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

860 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