Solved

pass it array to function and back

Posted on 2007-11-30
8
177 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
what would it mean if the function has * ?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
which one?
0
 

Author Comment

by:Troudeloup
Comment Utility
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
Comment Utility
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
Comment Utility
not a single clue of what that does ;p
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

772 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

10 Experts available now in Live!

Get 1:1 Help Now