Jon
Jon

Reputation: 1801

Passing scalar and array data in C++ with pointers

I have programmed for many years in FORTRAN and am trying to learn C++ and transfer some old programs to C++. I need to be able to create an array in the function and pass it back to the main program as a variable using a pointer as opposed to having it transferred as a value as is traditional in C++. I am including an example in this question where the variables *var1, *varr and *array are created and the data is passed back to the main program and this program works fine. However, if I turn *array into *array[1] or a larger dimension array with multiple values the program does not work. Can this action be completed in C++ or am I forced to incorporate this function into the main program so that the data is available in the main program? Any help would be greatly appreciated!

first example(this one works)

#include<iostream>
using namespace std;
void test(int *var1,int *varr, int *array);

int main()
{
    int var,var2,array1;
    test(&var,&var2,&array1);
    cout << var << "\n";
    cout << var2 << "\n";
    cout << array1 << "\n";
    return 0;
}

void test(int *var1, int *varr, int *array)
{
    *var1=20;
    *varr=30;
    *array=15;
}

second example (this one does not work)

#include<iostream>
using namespace std;
void test(int *var1,int *varr, int *array[1]);

int main()
{
    int var,var2,array1[1];
    test(&var,&var2,&array1[1]);
    cout << var << "\n";
    cout << var2 << "\n";
    cout << array1 << "\n";
    return 0;
}

void test(int *var1, int *varr, int *array[1])
{
    *var1=20;
    *varr=30;
    *array[1]=15;
}

Upvotes: 0

Views: 2298

Answers (4)

gongzhitaao
gongzhitaao

Reputation: 6682

The following test allows you to dynamically create array in a function.

#include<iostream>
using namespace std;

void test(int *&a);

int main()
{
    int *a = NULL;
    test(a);
    cout << a[0] << endl;
    delete a;
    return 0;
}

void test(int *&a)
{
    a = new int [3];
    a[0] = 3;
}

But I doubt you really need it, cause you are using C++, why not consider vector

#include <iostream>
#include <vector>
using namespace std;

void foo(vector<int> &a)
{
    a.push_back(3);
}

int main()
{
    vector<int> a;
    foo(a);
    cout << a[0] << endl;
    return 0;
}

UPDATE

Assume that your file format is:

int1 int2 int3

or

int1
int2
int3

#include <string>
#include <ifstream>
#include <vector>

void foo(const std::string &filename, std::vector<int> &v)
{
    std::ifstream in(filename);
    int a;
    while (in >> a) v.push_back(a);
    in.close()
}

Upvotes: 0

AngelCastillo
AngelCastillo

Reputation: 2435

There are several errors on the second program:

First off, the prototype of your test function:

void test(int *var1,int *varr, int *array[1]);

Remember that when you declare "int array[SIZE]", array is a already a pointer, so if you have your function take "int *array[1]", is actually taking as argument a pointer of a pointer "**int".

The "correct" (read note at the end) way of declaring what you want to do is:

void test(int* var1,int* varr, int array[1]); 

This is equivalent to:

void test(int* var1, int* varr, int* array); 

Second, here:

test(&var,&var2,&array1[1])

&array1[1] what is doing is passing the value at the position "2" (remember arrays start at position 0) to the function, because you are dereferencing the element of the array at that position, I think that what you really whan to is, pass the array as parameter, it should be done like this:

test(&var,&var2, array1);

Arrays are converted to pointers when passed to a function. Then inside the method you can just assing values:

array[1]=15;

This would be the resulting working code:

#include<iostream>
using namespace std;
void test(int *var1,int *varr, int array[1]);

int main()
{
    int var,var2,array1[1];
    test(&var,&var2, array1);
    cout << var << "\n";
    cout << var2 << "\n";
    cout << array1[1] << "\n";
    return 0;
}

void test(int *var1, int *varr, int array[1])
{
    *var1=20;
    *varr=30;
    array[1]=15;
}

As a tip (since you are starting with C++), you should initialize all your variables as soon as you declare them or they will contain garbage:

int var  = 0;
int var2 = 0;
int array1[] = { 0, 0};

For example.

Try to stay away from C-Style arrays and use the containers provided by the STL, like vector for example:

http://www.cplusplus.com/reference/vector/vector/

Upvotes: 1

John Tang Boyland
John Tang Boyland

Reputation: 1088

#include<iostream>
using namespace std;
void test(int *var1,int *varr, int *array);

int main()
{
    int var,var2,array1[10];
    test(&var,&var2,array1);
    cout << var << "\n";
    cout << var2 << "\n";
    cout << array1[1] << "\n";
    return 0;
}

void test(int *var1, int *varr, int *array)
{
    *var1=20;
    *varr=30;
    array[0]=15;
    array[1] = 16;
    array[2] = 17;
}

$ ./a.out

20

30

16

Upvotes: 1

Nbr44
Nbr44

Reputation: 2062

int var,var2,array1[1];

About array1, this declaration is equivalent to :

int array1[1];

Which means, an array of one integer. You can access each integer with this syntax :

array1[x]; // this is an int

Therefore, when you write

&array[1];

You are effectively retrieving the address of an int - an int*, which is in many ways incompatible with the type of the last parameter of your function test.

What you intend to do can however be completed by sligthly modifying your function declaration:

test(&var1, &var, &array1);

void test(int *var1, int *varr, int (*arrayptr)[1]) // arrayptr is a pointer to an array of 1 integer
{
  (*arrayptr)[0] = 15; // assign 15 to the first integer of the array pointed to by arrayptr.
}

Remember that in C/C++, indexing starts with 0 - if you want to reach the first integer of your array, you need to write array[0] and not array[1].

This is a pretty awkward way of doing however, especially in C++ - you might want to use a vector which has a more straightforward approach.

#include <vector>
#include <iostream

int main()
{
  std::vector<int> myVector(1); // A vector is a class that encapsulates a dynamic array. Here, I create a vector with 1 pre-allocated element.

  test(myVector);
  std::cout << myVector[0] << std::endl;
}

void test(std::vector<int> &referenceToMyVector)
{
  referenceToMyVector[0] = 15;
}

Upvotes: 0

Related Questions