is it possible to create an array then you pass it to a function? That function will change the values in the array then return it back as an array.

Recommended Answers

All 13 Replies

Yes, ever use strcpy?

here is an example:

this code doesn't work it shows here what i want to do w/ the array

changeArray(array[])  {
    
    //  this is where you change the values inside the array.
    return array[];  // return the array if it is even possible..
}

int main(void)  {
    int x[5];

    for(int y=0;y<5;y++)   // Input five integers in the array
            cin >> x[y];

    x[]=changeArray(x); // pass the array to the changeArray() function
               // will this code work? is there any way to do this?
    return 0;
}
#include <iostream>
using namespace std;

int *changeArray(int array[], int size)
{
    //  this is where you change the values inside the array.
   return array;  // return a pointer to the start of the array
}

int main(void)
{
   int x[5];

   for ( int y=0;y<5;y++ )   // Input five integers in the array
      cin >> x[y];

   changeArray(x, 5); // pass the array to the changeArray() function
   return 0;
}

hey thanks... that was fast...

#include <iostream>
using namespace std;

int *changeArray(int array[], int size)
{
    //  this is where you change the values inside the array.
   return array;  // return a pointer to the start of the array
}

int main(void)
{
   int x[5];

   for ( int y=0;y<5;y++ )   // Input five integers in the array
      cin >> x[y];

   changeArray(x, 5); // pass the array to the changeArray() function
   return 0;
}

i removed the * in changeArray() function and i also removed the return array; and it still works fine. like this one.

#include <iostream>

using namespace std;

int changeArray(int array[], int size)  {
   for(int y=0;y<size;y++)
      cin >> array[y];
}


int main(void)  {
   int x[5];

   for(int y=0;y<5;y++)   // Input five integers in the array
      cin >> x[y];
      
   changeArray(x, 5); // pass the array to the changeArray() function
   return 0;
}

which one is better the previous one with the * in the changeArray() and with the return statement or my revision? they have the same output i just want to know which one is better to use...

which one is better the previous one with the * in the changeArray() and with the return statement or my revision? they have the same output i just want to know which one is better to use...

If you're not going to use a return value, then you don't need one; you could just as well make it void.

void changeArray(int array[], int size)  {
   for(int y=0;y<size;y++)
      cin >> array[y];
}

It depends on how you intend to use it. For example, you can call strcpy by itself and not use the return value, or you can pass its return value to another function, puts perhaps.

char text[20];
    strcpy(text, "Hello");
    puts(text);
    puts(strcpy(text, "Goodbye"));

If you're not going to use a return value, then you don't need one; you could just as well make it void.

void changeArray(int array[], int size)  {
   for(int y=0;y<size;y++)
      cin >> array[y];
}

yes i know... but what is the difference between:

void changeArray(int array[], int size)  {
   for(int y=0;y<size;y++)
      cin >> array[y];
}

and this one?

int *changeArray(int array[], int size)  {
   for(int y=0;y<size;y++)
      cin >> array[y];
   return array;  // return a pointer to the start of the array
}

they still have the same output.

changeArray(x, 5); // pass the array to the changeArray() function

Whether or not the function returns a value, you do not use it -- so there will be nothing to notice. One returns a value that you ignore, the other doesn't return a value that you could care less about.

here is the main function...
it has the same output but i don't understand the use of * in the changeArray() function.

int main(void)  {
   int x[5];

   for(int y=0;y<5;y++)   // Input five integers in the array
      cin >> x[y];
   cout << endl;
   for(int y=0;y<5;y++)
      cout << x[y] << endl;
   cout << endl;
   changeArray(x, 5); // pass the array to the changeArray() function
   cout << endl;
   for(int y=0;y<5;y++)
      cout << x[y] << endl;
   cin.get();
   cin.get();
   return 0;
}

here is the main function...
it has the same output but i don't understand the use of * in the changeArray() function.

Look, if it troubles you that much, then use the other form. Many functions return values, sometimes you want them, other times you don't care.

#include <iostream>
using namespace std;

int main(void)
{
   cout << "The inner cout returned " << cout << "\nHello\n";
   return 0;
}

/* my output
The inner cout returned 00000001
Hello
*/

It can be useful in this way:

#include <iostream>
using namespace std;

int *changeArray(int array[], int size)
{
   cout << "Input " << size << " items: ";
   for ( int i = 0; i < size; ++i )
   {
      cin >> array[i]; 
   }
   return array;
}

void printArray(int array[], int size)
{
   cout << "Your " << size << " items: ";
   for ( int i = 0; i < size; ++i )
   {
      cout << array[i] << ' '; 
   }
   cout << endl; 
}

int main(void)
{
   int x[5];
   printArray(changeArray(x,5), 5);
   printArray(changeArray(x,5), 5);
   return 0;
}

/* my input/output
Input 5 items: 1 2 3 4 5
Your 5 items: 1 2 3 4 5 
Input 5 items: 6 7 8 9 10
Your 5 items: 6 7 8 9 10 
*/

how about a two dimensional array? how can i pass it to a function and return it back? I tried it but it won't work. It has an error saying...

"5 C:\cpp\pointer array.cpp declaration of `array' as multidimensional array must have bounds for all dimensions except the first "...

what is wrong with the code?

#include <iostream>

using namespace std;

int *changeArray(int array[][], int size)  {
   for(int y=0;y<size;y++)
      cin >> array[0][y];
   return array;  // return a pointer to the start of the array
}


int main(void)  {
   int x[1][5];

   for(int y=0;y<5;y++)   // Input five integers in the array
      cin >> x[0][y];
   cout << endl;
   for(int y=0;y<5;y++)
      cout << x[0][y] << endl;
   cout << endl;
   changeArray(x, 5); // pass the array to the changeArray() function
   cout << endl;
   for(int y=0;y<5;y++)
      cout << x[0][y] << endl;
   cin.get();
   cin.get();
   return 0;
}

btw, thanks Dave you've been a great help...

how about a two dimensional array? how can i pass it to a function and return it back? I tried it but it won't work. It has an error saying...

"5 C:\cpp\pointer array.cpp declaration of `array' as multidimensional array must have bounds for all dimensions except the first "...

The compiler hits the nail right on the head. Here would be one way.

#include <iostream>
using namespace std;

void changeArray ( int array[][5], size_t size )
{
   for ( size_t i = 0; i < size; ++i )
   {
      for ( size_t j = 0; j < 5; ++j )
      {
         cout << "array[" << i << "][" << j << "]? ";
         cin  >> array [ i ] [ j ];
      }
   }
}

void printArray ( int array[][5], size_t size )
{
   for ( size_t i = 0; i < size; ++i )
   {
      for ( size_t j = 0; j < 5; ++j )
      {
         cout << "array[" << i << "][" << j << "] = "
              << array [ i ] [ j ] << '\n';
      }
   }
}

int main(void)
{
   int myarray[2][5];
   changeArray ( myarray, sizeof myarray / sizeof *myarray );
   printArray  ( myarray, sizeof myarray / sizeof *myarray );
   changeArray ( myarray, sizeof myarray / sizeof *myarray );
   printArray  ( myarray, sizeof myarray / sizeof *myarray );
   cin.get();
   cin.get();
   return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.