How can I create a dynamic array in a procedure in C?

Fabio Nardelli

I'm new to this fantastic site and I found it very useful in many occasions, but now I'm dealing with a problem which I can't seem to solve. I'm a beginner C student and I'm studying dynamic memory allocation. I want to create a dynamic array of, let's say, integers, allocating memory for it inside a procedure, not in the main function.

This works:

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

int main()
{
        int *numbers; // pointer to create a dynamic array
        int *test; // pointer to test realloc function
        int c;
        size_t i = 0;
        unsigned int dim; // array's length

        // allocate memory for the 1st number 
        numbers = malloc(sizeof(*numbers));
        if (numbers == NULL) {
                fputs("Error while allocating memory!\n", stderr);
        } else {
                printf("Insert the 1 number: ");
                scanf("%d", &numbers[0]);
                ++i;

                /* allocate memory for the other numbers,
                 * until the user inputs EOF
                 */
                while (!feof(stdin)) {
                        test = realloc(numbers, (sizeof(*numbers) * (i + 1)));
                        if (test == NULL) {
                                fputs("Error while allocating memory!\n", stderr);
                        } else {
                                numbers = test;
                                printf("Insert the %u number: ", i + 1);
                                scanf("%d", &numbers[i]);
                                ++i;
                        }
                }
                dim = --i;

                // print the array, 5 numbers per line
                 for (i = 0; i < dim; ++i) {
                        if (i % 5 == 0) {
                                puts("");
                        }
                        printf("%d ", numbers[i]);
                }
                puts("");
        }

        getchar();
        return 0;
}

But I want to do this (which doesn't work):

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

void get_numbers(int **numbers, unsigned int *dim);

int main()
{
        int *numbers; // pointer to create a dynamic array
        size_t i = 0;
        unsigned int dim; // array's length

        get_numbers(&numbers, &dim);

        // print the array, 5 numbers per line
         for (i = 0; i < dim; ++i) {
                if (i % 5 == 0) {
                        puts("");
                }
                printf("%d ", numbers[i]);
        }
        puts("");

        getchar();
        return 0;
}

void get_numbers(int **numbers, unsigned int *dim)
{
        int *test; // pointer to test realloc function
        int c;
        size_t i = 0;

        // allocate memory for the 1st number 
        *numbers = malloc(sizeof(*numbers));
        if (*numbers == NULL) {
                fputs("Error while allocating memory!\n", stderr);
        } else {
                printf("Insert the 1 number: ");
                scanf("%d", &numbers[0]); // Maybe the error is here
                ++i;

                /* allocate memory for the other numbers,
                 * until the user inputs EOF
                 */
                while (!feof(stdin)) {
                        test = realloc(*numbers, (sizeof(*numbers) * (i + 1)));
                        if (test == NULL) {
                                fputs("Error while allocating memory!\n", stderr);
                        } else {
                                *numbers = test;
                                printf("Insert the %u number: ", i + 1);
                                scanf("%d", &numbers[i]);
                                ++i;
                        }
                }
                *dim = --i;
        }
}       

Do I need to use a pointer to a pointer to int, right? I know I made some mistakes but I can't figure out how to fix them. Thanks in advance for your replies! Bye, Fabio Nardelli

R Sahu

In the first block of code, numbers is of type int*.
In the second block of code, numbers is of type int**.

You have not fixed all the places that need to be fixed because of that change.

My suggestion:

  1. Change the input argument to numbersPtr.
  2. Create a local variable int* numbers.
  3. Before you return from the function, set *numbersPtr to number.

void get_numbers(int **numbersPtr, unsigned int *dim)
{
   int* numbers;

   ...

   *numbersPtr = numbers;
}

It will be better to change get_numbers to return numbers.

int* get_numbers(unsigned int *dim)
{
   int* numbers;

   ...

   return numbers;
}

and change its usage as:

numbers = get_numbers(&dim);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I create a two-dimensional dynamic array in C#?

From Dev

How can i create Dynamic method in Objective C

From Dev

How can i use dynamic 2d array in c

From Dev

How can I make a pseudo-dynamic Stored procedure / CTE

From Dev

How can I create a dynamic URL in php?

From Dev

How can I create dynamic CONTAO pages?

From Dev

How can I create dynamic JLists?

From Dev

How can I create a dynamic background with CSS?

From Dev

How can i create an n-dimensional array in c

From Dev

How can I create a global checkbox array in c#?

From Dev

C++ How to create a dynamic array of vectors?

From Dev

How Can I Create Record and Table of That Record Within a Stored Procedure?

From Dev

How to create a dynamic array of dynamic stacks in C or C++

From Dev

How can I create a dynamic array with different random values in random size?

From Dev

How can I create a dynamic array with different random values in random size?

From Dev

How can I create my own dynamic object similar to ViewBag in C#?

From Dev

How can I write a dynamic amount of lines and assign them to array in C

From Dev

How can I write a dynamic amount of lines and assign them to array in C

From Dev

How can I create an array of functions?

From Java

How can I create a stream from an array?

From Dev

How can I create an array of classes in Swift

From Dev

How can I create numpy array of views?

From Dev

How can I Create an array with N zeros?

From Dev

How can I create an array with polymorphic data?

From Dev

How can I create std::function with array?

From Dev

how can i create a table for this array?

From Dev

How can I create array of lines in this case?

From Dev

How do i create a dynamic array of function pointers?

From Dev

How do i create a dynamic array of function pointers?

Related Related

  1. 1

    How can I create a two-dimensional dynamic array in C#?

  2. 2

    How can i create Dynamic method in Objective C

  3. 3

    How can i use dynamic 2d array in c

  4. 4

    How can I make a pseudo-dynamic Stored procedure / CTE

  5. 5

    How can I create a dynamic URL in php?

  6. 6

    How can I create dynamic CONTAO pages?

  7. 7

    How can I create dynamic JLists?

  8. 8

    How can I create a dynamic background with CSS?

  9. 9

    How can i create an n-dimensional array in c

  10. 10

    How can I create a global checkbox array in c#?

  11. 11

    C++ How to create a dynamic array of vectors?

  12. 12

    How Can I Create Record and Table of That Record Within a Stored Procedure?

  13. 13

    How to create a dynamic array of dynamic stacks in C or C++

  14. 14

    How can I create a dynamic array with different random values in random size?

  15. 15

    How can I create a dynamic array with different random values in random size?

  16. 16

    How can I create my own dynamic object similar to ViewBag in C#?

  17. 17

    How can I write a dynamic amount of lines and assign them to array in C

  18. 18

    How can I write a dynamic amount of lines and assign them to array in C

  19. 19

    How can I create an array of functions?

  20. 20

    How can I create a stream from an array?

  21. 21

    How can I create an array of classes in Swift

  22. 22

    How can I create numpy array of views?

  23. 23

    How can I Create an array with N zeros?

  24. 24

    How can I create an array with polymorphic data?

  25. 25

    How can I create std::function with array?

  26. 26

    how can i create a table for this array?

  27. 27

    How can I create array of lines in this case?

  28. 28

    How do i create a dynamic array of function pointers?

  29. 29

    How do i create a dynamic array of function pointers?

HotTag

Archive