Solved

pass it array to function and back

Posted on 2007-11-30
8
179 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Handle Exceptions during instantiation 28 416
c++ substatte a varabe for a string in a LPCTSTR statment 8 86
Best book to learn C++ 4 79
Issues with C++ Class 19 92
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…
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 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.

803 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