1
I have a function I need her to be void
and that a Double Pole be returned from her by the argument.
void cracking::decompose(char input[][100], int size_S, double* &Atm, int* &ID, int &size_out);
{
vector<double> AtmD;
vector<int> XsD;
...
Atm = &AtmD[0];
ID = &XsD[0];
size_out = size(AtmD);
}
on Main she gets:
int main()
{
char oi[900][100] = { "1 0.5 C", "2 0.55 N", "3 .5 S" };
double* xAtm = NULL;
int* xXs = NULL;
int tamanho;
cracking calc;
calc.decompose(oi, 3, xAtm, xXs, tamanho);
return 0;
}
although compiling it returns me completely wrong values. Is there any way to do this with pointers?
In fact I realized that the pointers inside my function turn to garbage when I return them to main. Is there any way to keep pointers saved? For example, if you save the address 0x000000000029d680
memory and it corresponds to 23.5, when I leave the function this address no longer corresponds to this value and becomes garbage, there is a way to keep this value?
what you tried to do with
double* &aponta
? Triesdouble *aponta
only. Something else, to return value, your function has to indicate the type of return, and notvoid
. Passing by reference is not return– Leonardo Alves Machado
I need it to be as double &value, so rather than being a double argument it is a double pointer so I can input it but also take back the resulting value of it as it works for the int &size
– Patrick Machado
"I need" is not justification. Say what you tried to do here, and why it is necessary to point to the address of a variable instead of pointing to the variable. I’m under the impression that you don’t know what you’re doing - in which case, your best help would be a good book of C.
– Leonardo Alves Machado
I have a vector<double> and I need to point to the first double of this vector, so I can by the pointer of the first element and the number of elements get all this vector back. It is not feasible that I have a function of type vector<double> because in some cases I have more than one vector, struct is also not feasible.
– Patrick Machado
We need to see the code snippet where the function is called
– epx
I made the changes
– Patrick Machado
Atm and id return garbage, because the Atmd and Xsd vectors are destroyed in the function return. Do you really need the address of these vectors? Isn’t the value of the first element enough? Is this function signature your creation or did it pass to you? I don’t usually find a reference for a pointer as a function parameter.
– Marcos Banik
Marcos, I need all the elements of the vector but I can’t pass the vector either as a return or as an argument because I’m going to exterminate this function in C. Anything that works to get the whole vector would be of great help.
– Patrick Machado
In this case, I think you better replace the vectors with arrays of C. And you will have to define who will release this memory later.
– Marcos Banik