I’m trying to get better at using pointers, and not using array notation. So I have a function to read user input and return a pointer to that array. I can do it like this and it seems to work ok:
float *GetValues(float *p, size_t n)
{
float input;
int i = 0;
if ((newPtr = (float *)malloc(n * sizeof(float))) == NULL) {
cout << "Not enough memory\n";
exit(EXIT_FAILURE);
}
cout << "Enter " << n << " float values separated by whitespace: \n";
while (scanf("%f", &input) == 1) {
p[i] = input;
i++;
cout << *p;
}
return p;
}
But then if I do this:
float *GetValues(float *p, size_t n)
{
float *newPtr;
float input;
if ((newPtr = (float *)malloc(n * sizeof(float))) == NULL) {
cout << "Not enough memory\n";
exit(EXIT_FAILURE);
}
cout << "Enter " << n << " float values separated by whitespace: \n";
while (scanf("%f", &input) == 1) {
*newPtr++ = input;
}
return newPtr;
}
I get just 0s entered into p. Why is that?
Also, do I have to allocate memory here for an array of size n? I first tried it with the method above using pointers and not allocating memory but just set p = to input and I was getting garbage values. Thanks!
Edited: Sorry, I allocated a new ptr and was returning the wrong one like you said. I was just trying to input the numbers into my pointer and display it back on the screen to myself and wasn’t paying attention to the return type and I was getting an output of 0 when I would cout << *newPtr.
A few tips:
The first GetValues allocates newPtr (which is not declared within the function, a global variable?) but then does nothing with it. There are two possible ways that your function could work with regards to memory storage:
The function gets a pointer to valid memory for an array of size large enough. In that case the signature of the function ought to be
float *GetValues(float *array, size_t arraySize)
to more clearly state the nature of the arguments. You need not to allocate anything inside the function.
The function should allocate the needed memory itself and let the caller free the memory some time later. In that case you MUST have some kind of hint in the name of the function that it allocates memory. Otherwise you are in for a disaster in terms of maintaining this code because it will be extremely easy to make mistakes in freeing memory. You will not need to pass an array pointer to the function, but if you do it needs to be a double pointer for it to be of any meaning (C and C++ passes arguments by value so it is not possible to change a pointer which is passed as an argument)
float *GetValuesAndAllocateMemmory(size_t n)
float *GetValuesAndAllocateMemmory(float **array_pp, size_t n)
The loop in the second GetValues should be
in order to be identical to the first GetValues.
Here there is upper limit of number of float values is
nbut your code does not check thisand will crash if more than
nfloats are entered. Never trust user input data, even when it comes from yourself. Always verify. Search for the term “input validation” for more information about this.