#include <iostream>
//and some other libraries
usingnamespace std;
int function(string* S, int* I, double* D, string A);
int main()
{
string A[10];
//filling out the array 'A'
int size = 10;
string* S = new string[size];
int* I = newint[size];
double* D = newdouble[size];
function(S, I, D, A);
for(int i = 0; i < size)
{
cout << S[i] << I[i] << D[i] << endl;
}
return 0;
}
int function(string* S, int* I, double* D, string A[])
{
int size = 10;
S = new string[size];
I = newint[size];
D = newdouble[size];
//function computations: basically using the input string array 'A' to
//get arrays 'S', 'I', 'D'
return 0;
}
No, it's not correct.
You're using new[] instead of std::vector, the types for the parameter A in function mismatch, for is incomplete and you have a memory leak inside function, not to mention that there's no point passing all these parameters if you're not even using them anyway.
Perhaps you should explain what you're trying to do.
void func(int*);
int main()
{
int* foo = newint[3];
foo[0] = 0;
foo[1] = 1;
foo[2] = 2;
func(foo);
cout << foo[0]; // prints '3', not '0' (see func() below)
delete[] foo;
}
void func(int* ptr)
{
// ptr already points to something. We don't need to allocate memory here
ptr[0] = 3; // because 'ptr' points to the same thing 'foo' points to, this changes the same memory
}
The functiuon function will establish arrays (S, I, D) that will be used in the main and the other functions that the main will call.
I wonder if I dont pass S, I, D arrays, can the main still access the values in S, D, I?
The pointer concept is really confusing to me.
Would you be kind to just write a brief template for passing mutiple arrays from the function to the main?