1

I'm trying to create a dynamic float array. The user should type values untill a negative one is found. Then show the array in the screen. As I dont know the size of the array, everytime a valid value is entered I use realoc() to increment the size of the array

My code allocates memory for all the elements, but when I print the array, I get a segmentation fault. Don't know if I assign values in a wrong way or fail while reading them.

Here is the code:

#include <stdio.h>
#include <stdlib.h>

unsigned int createArray(float*);
void printArray(float *, unsigned int);

int main(){
    float *pArray = NULL;
    unsigned int arrayLength = 0;
    arrayLength = createArray(pArray);  
    printArray(pArray, arrayLength);
    return 0;
}

/**
 * Ask the user to fill an array untill he types a negative value
 * 
 * @param A pointer to the array we want to create
 * @return The final length of the array
 * 
*/
unsigned int createArray(float *pArray){
    float number = 1;
    int arrayLength = 0;

    while(number>0){
        printf("\nSetting number %d: ", arrayLength);
        scanf("%f", &number);
        arrayLength++;
        pArray=realloc(pArray, sizeof(float)*arrayLength);
        if(pArray==NULL){
            printf("\nERROR: Not enough memory");
            free(pArray);
            exit(0);
        }
        pArray[arrayLength-1] = number; 
    }


    return arrayLength;
}
/**
 * Prints an array given by the user 
 * 
 * @param Pointer to the array
 * @param Length of the array
 * 
*/
void printArray(float *pArray, unsigned int arrayLength){
    for(int i=0;i<arrayLength;i++){
        printf("\nItem[%d]: %.2f", i, *pArray+i);
    }
}
13
  • A dynamic array is not handled differently than a static or automatic array. Commented Feb 7, 2017 at 18:42
  • 2
    what is the point to free(NULL) ? Commented Feb 7, 2017 at 18:44
  • Touché, Didn't realized about that. Commented Feb 7, 2017 at 19:00
  • the line: while(number>0) does not agree with the question nor the user prompt. That is because 0 is not negative, but is excluded from the valid input values, so will cause the loop to exit. suggest: while(number>=0.0f) Commented Feb 9, 2017 at 10:43
  • when writing float literals, the number must contain a decimal point AND a trailing f. With out the decimal point, the number is an integer, not a float. With out the trailing f the number is a double, not a float Commented Feb 9, 2017 at 10:45

1 Answer 1

2

You are reserving space for a local variable in createArray() whose lifetime ends with the function, you need to pass the address of pArray:

arrayLength = createArray(&pArray);

And then dereference the passed pointer to pointer in the function:

unsigned int createArray(float **pArray)
{
    ...
    *pArray = realloc(*pArray, sizeof(float)*arrayLength);
     if (*pArray == NULL) {
    ...
Sign up to request clarification or add additional context in comments.

6 Comments

So inside the function pArray would point to the direction where the array is in memory, *pArray point to the memory where I want to store the numbers and **pArray would point to a single element? Excuse me if it's simple question but I never seen pointers before as im learning them now.
Yes, in your question you are passing a copy of pArray to the function, therefore you are reserving space for this (local) variable and when you exit from the function this variable no longer exists, in C you "simulate" pass by reference using the address of operator as in my example (Excuse my poor english)
Still got the segmentation fault in createArray(). Dont know what is happening, first assignement goes ok but now it always fails at the second one. Maybe because of this **(pArray+arrayLength-1) = number;
Try *(*pArray+arrayLength-1) = number; or (*pArray)[arrayLength-1] = number;
Thank you so much for being patient with me :). Now it works, but also the most important, I understand why. I also had to change this in printArray(): printf("\nItem[%d]: %.2f", i, *(pArray+i));
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.