Thread: not getting the right values by passing struct members to functions

  1. #1
    Registered User
    Join Date
    Mar 2012
    Location
    Butuan City, Philippines
    Posts
    1

    not getting the right values by passing struct members to functions

    I'm trying to create a dynamic array program which is implemented using structs, which struct members are passed into functions, and functions changes the values for the variables being passed to it.

    The problem is, I'm not getting the right values.
    For example: I'm passing an integer 5 into function A, then updates a struct member N within the function with 5. In the main function, after executing function A, I then passed the updated struct member N into function B, but function B doesn't get the value 5, instead i get this value 2293532 when printing the struct member N.

    What's wrong with my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct _da1
    {
        int *dynamic;
        int array_s;
    } da1;
    
    
    
    int main()
    {
        da1 array;
        array.dynamic = (int*)realloc(array.dynamic, 0);
        array.array_s = 0;
        int input;
        printf("Input size: ");
        scanf("%d",&input);
        test_resize(5, array.dynamic, &array.array_s);
        printf("%d\n",array.array_s);
        test_input(array.dynamic, &array.array_s);
        test_print(array.dynamic, &array.array_s);
        return 0;
    }
    
    void debug(char c)
    {
        printf("debug %c\n", c);
    }
    
    void test_resize(int x, int *a, int s)
    {
        debug('r');
        a = realloc(a, x * sizeof(a));
        s = x;
    }
    
    void test_input(int *a, int s)
    {
        debug('i');
        int i = 0;
        printf("%d \n", s);
        for(i = 0; i < s ; i++)
        {
            printf("input in %d: \n", i);
            scanf("%d", &a[i]);
        }
    }
    
    void test_print(int *a, int s)
    {
        debug('p');
        int i = 0;
        for(i = 0; i < s ; i++)
        {
            printf("in %d = %d \n", i, a[i]);
        }
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    For the first realloc, either initialize array.dynamic to NULL beforehand, or pass NULL instead of array.dynamic, or use malloc instead of realloc there. And the second parameter shouldn't really be 0 (why would you want that?). Maybe just init array.dynamic to NULL and get rid of that allocation.

    In order to realloc array.dynamic in test_resize, you'd have to pass a double pointer.
    Code:
    test_resize(5, &array.dynamic, &array.size);
    
    ...
    
    void test_resize(int x, int **a, int *s) {
        *a = realloc(*a, x * sizeof(**a));
        *s = x;
    }
    There's no reason to pass array.array_s as a pointer to test_input and test_print.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    test_resize(5, array.dynamic, &array.array_s);
    test_input(array.dynamic, &array.array_s);
    test_print(array.dynamic, &array.array_s);

    If you prototyped these before calling them, you'd get a sack-full of errors.

    Plus your realloc may move the memory, so you need to be able to return a modified pointer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Values From Functions
    By skmightymouse in forum C Programming
    Replies: 21
    Last Post: 03-25-2012, 11:48 AM
  2. Passing members of structs to functions
    By CV3 in forum C Programming
    Replies: 6
    Last Post: 09-23-2004, 01:56 PM
  3. default values for struct members (noob q)
    By Mr_Jack in forum C++ Programming
    Replies: 3
    Last Post: 12-04-2003, 09:30 PM
  4. functions passing values
    By srinurocks in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2002, 11:49 PM
  5. Passing Values to Functions
    By shad0w in forum C Programming
    Replies: 2
    Last Post: 12-25-2001, 08:28 PM