Thread: Passing a structure pointer

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    22

    Passing a structure pointer

    I have a file where I have two functions, function1 and function2.

    Both these files are called from the main script.

    I have a structure pre_block ppp:

    Code:
    struct pre_block {
    
    int *blk;
    int no_blk;
    };
    The structure is declared in the main funciton .
    It is then initialised, assigned memory, in function1, and called by:

    Code:
    function1(var1,&var2,...,&ppp)
    Within function1, I first set
    PHP Code:
    ppp->no_blk=0
    then afterwards in the same function it increments through a loop and is
    PHP Code:
    ppp->no_blk=50
    However, in the main function and in function2 which is later called as:

    Code:
     function2(&var5,&var6,var7,...&ppp)
    , I get that ppp->no_blk=0 and not 50.

    I have done it similarly with passing variable by pointer with another structure in another function,and there it worked, so I don't understand what I am doing wrong here.

    Also, when I set ppp->no_blk=0; at the very beginning of the function, it gets some very large value after incrementations which I assum e is the addres.

    And when i set ppp->no_blk=0; later in the function1, just before starting incrementation, then it is 50 within function1 and in the main script and function2 it is as described above.

    Hope I have asked the question clearly enough!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Post a simple code that shows what you want, and actually compiles and runs.

    Single lines and lots of words don't make for clarity.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2017
    Posts
    22

    Extension to the question

    Ok so these are my two functions:

    Code:
    int function1(struct pre_block *ppp){
    
    int a;
    int b;
    int c;
    int i;
    
    
    a=2;
    b=3;
    
    c=a+b;
    
    
        if ((ppp = (struct pre_block *)malloc(0)) == NULL) {
              return EXIT_FAILURE;
            }
    
    
        if ((ppp->blk = (int *)malloc(0)) == NULL) {
              return EXIT_FAILURE;
            }
            
    
        ppp->no_blk=0;
    
        for(i=1;i<c;i++){
            
            if ((ppp->blk = (int *)realloc(ppp->blk, sizeof(int) * (ppp->no_blk+2))) == NULL) {
                return EXIT_FAILURE;
                }
            
            ppp->no_blk=ppp->no_blk+2;
            
        }
        
        
        printf("ppp->no_blk=%i", ppp->no_blk); /* this prints the correct value ppp->no_blk=8 */
        
        return EXIT_SUCCESS;
    
    }
        
        
    int function2(struct pre_block *ppp){
        
        int x=10;
        int y;
        
        y=(ppp->no_blk)+x;
        
        printf("ppp->no_blk=%i", ppp->no_blk); /* this prints preblk->no_blk=0; */
        
         return EXIT_SUCCESS;
    
    }
    I call both of them from the main script as:
    Code:
    ...
    int submain(int argc, char **argv) {
        
        struct pre_block ppp;
        
        if(function1(&ppp)!= EXIT_SUCCESS){
            return EXIT_FAILURE;
        }
        
        printf("ppp.no_blk=%i",ppp.no_blk); /*prints 0*/
        
        if(function1(&ppp)!= EXIT_SUCCESS){
            return EXIT_FAILURE;
            
    ...        
        }
    I get the value 0 when I print ppp->no_blk in the main function and in function2 although it should have the value 8.
    Last edited by Layla_E; 04-18-2018 at 10:07 AM.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,631
    Try this.
    Code:
    // call it like this
        struct pre_block *ppp;  // needs to be a pointer
        function1(&ppp);  // need to pass the address of the pointer
    
    int function1(struct pre_block **ppp) {  // needs to be a double pointer
        int a = 2;
        int b = 3;
        int c = a + b;
        int i;
    
        if ((*ppp = malloc(sizeof **ppp)) == NULL)
            return 1;
        (*ppp)->blk = NULL;  // realloc is okay with an initial value of NULL (it acts like malloc in that case)
        (*ppp)->no_blk = 0;
    
        for (i = 1; i < c; i++) {
            if (((*ppp)->blk = realloc((*ppp)->blk, sizeof(int) * ((*ppp)->no_blk + 2))) == NULL)
                return 1;
            (*ppp)->no_blk = (*ppp)->no_blk + 2;
        }
        printf("ppp->no_blk=%d\n", (*ppp)->no_blk);
        return 0;
    }
    Last edited by john.c; 04-18-2018 at 10:48 AM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Oct 2017
    Posts
    22
    Thanks a lot, it works now! Do you have any idea why the way I did it before your correction works with some other structures and does not work with this one?
    Do function1 and function2 need to be defined in separate files for it to work?
    Last edited by Layla_E; 04-18-2018 at 12:00 PM.

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,631
    The wackiest thing you did in post 3 above is reassigning ppp to a useless address returned from a malloc(0) call, overwriting the address that you had passed in.


    You could also do it like this, which is more like you meant to do:
    Code:
    // call it like this
        struct pre_block ppp;
        function1(&ppp);
     
    int function1(struct pre_block *ppp) {
        int a = 2;
        int b = 3;
        int c = a + b;
        int i;
     
        ppp->blk = NULL;
        ppp->no_blk = 0;
     
        for (i = 1; i < c; i++) {
            if ((ppp->blk = realloc(ppp->blk, sizeof(int) * (ppp->no_blk + 2))) == NULL)
                return 1;
            ppp->no_blk = ppp->no_blk + 2;
        }
    
    
        return 0;
    }
    And, no, the functions don't need to be in separate files.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  7. #7
    Registered User
    Join Date
    Oct 2017
    Posts
    22
    Thank you, this with overwriting explains A LOT!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing structure by reference or pointer?
    By 7force in forum C Programming
    Replies: 8
    Last Post: 12-13-2010, 06:49 PM
  2. Passing Structure Pointer
    By seubjoh in forum C Programming
    Replies: 3
    Last Post: 08-31-2009, 01:38 PM
  3. passing pointer to a structure in a function
    By steve1_rm in forum C Programming
    Replies: 5
    Last Post: 02-03-2008, 02:48 AM
  4. passing a structure pointer by value
    By Bleech in forum C Programming
    Replies: 6
    Last Post: 07-11-2006, 05:58 PM
  5. passing pointer to structure to a function
    By samual_van in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2005, 07:18 PM

Tags for this Thread