Thread: Dynamic array from Function to Function

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    2

    Dynamic array from Function to Function

    Hello, everyone.
    So, my program is just counting the maximum of the array, however, I'd like to do that with the dynamic array (with pointers). Could someone explain me what am I doing wrong ? Thank you.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    void ArrayScan(int n, int *h);
    void ArrayMaximum(int n, int *h, int x);
    int main()
    {
        int n, *array;
        int max = 0;
    
    
    
    
        printf("Write the number of random numbers: ");
        scanf("%d", &n);
        ArrayScan(n,&array);
        ArrayMaximum(n,array, max);
        free(array);
        getch();
    }
    void ArrayScan(int n, int *h)
    {
     int i = 0;
     h = (int*)malloc(n* sizeof(int));
     for ( ; i<n; i++)
     scanf("%d", &h[i]);
    
    
    }
    void ArrayMaximum(int n, int *h, int x)
    {
     int i=0;
     x = h[0];
     for( ;i<n;i++)
     {
      if (h[i]>x)
      x = h[i];
     };
     printf("The maximum is: %d", x);
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Your compiler has to be warning you about an invalid call to ArrayScan() (if it's not, it's broken, and you should get a new compiler).

    You're passing &array to ArrayScan(). array has type int*, so &array has type int**. However, ArrayScan() expects an int*. int** and int* are incompatible types.

    Passing &array is right, because you need ArrayScan() to modify array (you need it to make array point somewhere). But that means that ArrayScan() needs to take int** instead of int*; and each time you use "h" in the function, you should be using "*h". Since h points to an int*, *h is an int*, which is to say *h is the same thing as "array" in your main function.

    This is no different than passing, say, an int* to a function when it needs to modify the int. Instead, you're just passing an int** so the function can modify the int*.

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    2
    Thank you very much for the response, cas. I tried to change my code (wrote int **, instead of int *, changed to *h in functions). However, it doesn't compile me.
    What am i doing wrong now?
    Code:
    #include <stdio.h>#include <stdlib.h>
    void ArrayScan(int n, int **h);
    void ArrayMaximum(int n, int *h, int x);
    int main()
    {
        int n, *array;
        int max = 0;
    
    
    
    
        printf("Write the number of random numbers: ");
        scanf("%d", &n);
        ArrayScan(n,&array);
        ArrayMaximum(n,array, max);
        free(array);
        getch();
    }
    void ArrayScan(int n, int **h)
    {
     int i = 0;
     h = (int*)malloc(n* sizeof(int));
     for ( ; i<n; i++)
     scanf("%d", *(h+i));
    }
    void ArrayMaximum(int n, int *h, int x)
    {
     int i=0;
     x = *(h+i);
     for( ;i<n;i++)
     {
      if (*(h+i)>x)
      x = *(h+i);
     };
     printf("The maximum is: %d", x);
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,855
    This is a mistake:
    Code:
    h = (int*)malloc(n* sizeof(int));
    You want a dynamic array of int, so an int* makes sense. However, you want to "return" this int* to the caller. If you are doing this via a pointer, then you have the caller pass a pointer to a pointer. But this means that you need to dereference the pointer:
    Code:
    *h = malloc(n * sizeof(int));
    I suggest making use of an extra pointer to int to simplify the code, e.g.,
    Code:
    void ArrayScan(int n, int **result)
    {
        int *numbers = malloc(n * sizeof(numbers[0]));
        if (numbers)
        {
            int i;
            for (i = 0; i < n; i++)
            {
                scanf("%d", &numbers[i]);
            }
            *result = numbers;
        }
    }
    Note that you are assuming that n numbers will be entered, but this might not hold true, or there might be input errors, so at some point you should think about this and check the return value of scanf.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic 2D array to function
    By MaynardJ222 in forum C Programming
    Replies: 7
    Last Post: 07-15-2013, 11:48 AM
  2. Passing a dynamic array to a function
    By esmeco in forum C Programming
    Replies: 15
    Last Post: 06-05-2010, 04:25 PM
  3. passing dynamic array to function
    By rocketman03 in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2009, 08:24 AM
  4. Help Returning a new Dynamic Array from a Function
    By KeithS in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2009, 12:29 PM
  5. Dynamic Array Allocation function
    By P4R4N01D in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2009, 02:04 AM