Thread: using typedef struct and pasisng pointers to functions

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    19

    using typedef struct and pasisng pointers to functions

    I am haivng problms with passing my values through functions. Can someone help find my problem or provide a solution.

    Here is my code:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    void test(int**);
    void test2(int**);
    typedef struct
    {
        int **x;
    }PRACTICE;
    int main()
    {
        int i;
        int j;
        i = 0;
        j = 0;
        PRACTICE y;
        test(y.x);
        test2(y.x);
        for(i = 0;i < 5; i++)
            for(j = 0; j < 5;j++)
                printf("%d\n", *(*(y.x + i) + j));
        return 0;
    }
    void test(int**u)
    {
        int i;
        i = 0;
        u = (int**) calloc(5,sizeof(int*));
        for(i = 0; i < 5; i++)
            *(u + i) = (int*)calloc(5,sizeof(int));
    }
    void test2(int **u)
    {
        int i;
        int j;
        i = 0;
        j = 0;
    
        for(i = 0; i < 5; i++)
        {
            for(j =0; j < 5; j++)
            {
                *(*(u + i) + j) = i + j;
       //             printf("%d\n", *(*(u + i) + j));
            }
        }
    }
    Thank you

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    If your intention is to change x from within your functions then as always, you must pass the address of that variable and the function's parameter must be a pointer to the type that x is. And you must use the correct indirection.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    19
    Thats what I did? If you take out the typedef struct and declare X as a variable in main (int **x) and is using X it works. But with typedef it wont work like that and not sure why. It is however workng in the function, and if I pass X from test to test2 it will work but thats not what I am trying to do.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by plot View Post
    If you take out the typedef struct and declare X as a variable in main (int **x) and is using X it works.
    I'm pretty sure it doesn't work. This
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    void test(int**);
    void test2(int**);
    
    int main(void)
    {
        int i;
        int j;
        int **x;
        test(x);
        test2(x);
        for(i = 0;i < 5; i++)
            for(j = 0; j < 5;j++)
                printf("%d\n", *(*(x + i) + j));
        return 0;
    }
    
    void test(int**u)
    {
        int i;
        i = 0;
        u = (int**) calloc(5,sizeof(int*));
        for(i = 0; i < 5; i++)
            *(u + i) = (int*)calloc(5,sizeof(int));
    }
    
    void test2(int **u)
    {
        int i;
        int j;
     
        for(i = 0; i < 5; i++)
            for(j =0; j < 5; j++)
                *(*(u + i) + j) = i + j;
    }
    still crashes at line 36. "x" is passed by value to test(), so whatever you do to it inside test() won't be visible to test2().

    Please tell us what you really want to achieve.

    Bye, Andreas

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quote Originally Posted by plot View Post
    Thats what I did? If you take out the typedef struct and declare X as a variable in main (int **x) and is using X it works. But with typedef it wont work like that and not sure why. It is however workng in the function, and if I pass X from test to test2 it will work but thats not what I am trying to do.
    Stripping away the struct is no proof of correctness. x is still what it is and the only thing that changes is how you refer to it (x vs. y.x).

    But no, that is not what you did. Are you passing the address of x as I previously stated? Is the parameter u a pointer to x's type? I don't know of any good sites to point you to, but the rule is always the same.

    If Q is an object of some data type - int, float, struct, array, pointer to something, pointer to pointer to something, etc., then
    Code:
    {
       foo( &Q );
    }
    
    foo( Q_type* P )
    {
       *P = new_value_for_Q;
    }

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    19
    U is a pointer to y.x

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    19
    Okay I see what you are trying to say now. But how would you do this to array pointers in pointer notations?

  8. #8
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quote Originally Posted by plot View Post
    U is a pointer to y.x
    Nope.

    u is a copy of y.x

  9. #9
    Registered User
    Join Date
    Feb 2013
    Posts
    19
    Okay so I solved it by doing this:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    int **test();
    void printData(int**);
    
    typedef struct
    {
        int **x;
    }PRACTICE;
    int main()
    {
        int i;
     //   int *x;
        PRACTICE k;
    //    x = &i;
        k.x = test();
        printData(k.x);
     
    
    }
    int **test()
    {
        int *p;
        int **y;
        int i;
        i = 0;
        y = (int**) calloc(10,sizeof(int*));
        for(i = 0; i < 10; i++)
            *(y + i) = (int*)calloc(5,sizeof(int));
        *(*(y + 7)+2) = 10;
    //    y = &p;
        printf("%d\n", *(*(y + 7) + 2));
        return y;
    }
    void printData(int**t)
    {
            printf("%d\n", *(*(t + 7) + 2));
    }
    But what if I have more varibles in struct that I need to allocate memory for. for example:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    int **test();
    void printData(int**);
    
    typedef struct
    {
        int **x;
        int **o;
    }PRACTICE;
    int main()
    {
        int i;
     //   int *x;
        PRACTICE k;
    //    x = &i;
        k.x = test();
        printData(k.x);
     
    
    }
    int **test()
    {
        int *p;
        int **y;
        int i;
        i = 0;
        y = (int**) calloc(10,sizeof(int*));
        for(i = 0; i < 10; i++)
            *(y + i) = (int*)calloc(5,sizeof(int));
        *(*(y + 7)+2) = 10;
    //    y = &p;
        printf("%d\n", *(*(y + 7) + 2));
        return y;
    }
    void printData(int**t)
    {
            printf("%d\n", *(*(t + 7) + 2));
    }
    Is there a better way to allocate memory for a struct without having to set it equal to a function everytime?
    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. typedef struct: naming structures using pointers
    By BinaryBlock in forum C Programming
    Replies: 9
    Last Post: 02-05-2012, 08:11 PM
  2. Replies: 17
    Last Post: 07-06-2011, 11:44 AM
  3. how do you put struct pointers into functions?
    By SORAKH2756 in forum C Programming
    Replies: 19
    Last Post: 09-04-2010, 12:24 AM
  4. struct pointers and functions
    By alfaceor in forum C Programming
    Replies: 8
    Last Post: 10-20-2009, 09:11 AM
  5. Replies: 1
    Last Post: 05-07-2002, 04:18 AM