Thread: Passing struct pointer to function

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    33

    Passing struct pointer to function

    Hi everyone,

    I'm trying to integrate a library (BSXlite Library) into by code and I cannot understand how they pass struct pointer to function.. well this the structure

    Code:
    typedef struct {
        BSX_U8 *accelspec;  
        BSX_U8 *magspec;    
        BSX_U8 *gyrospec;   
        BSX_U8 *usecase;    
        BSX_U8 accelspec_status; 
        BSX_U8 magspec_status;   
        BSX_U8 gyrospec_status;  
        BSX_U8 usecase_status;   
    }initParam_t;
    and this is the function

    Code:
    BSX_S8 bsx_init(initParam_t*); // BSX_S8 is typedef of int8_t
    This is Integration guideline snippet (provided by the manufacturer)


    Code:
    initParam_t s_input;
    BSX_S8 init_status;
    
    
    s_input.accelspec = str_accsensorspec;
    s_input.magspec = str_magsensorspec;
    s_input.gyrospec = str_gyrosensorspec;
    s_input.usecase = str_config;
    
    init_status = bsx_init(initParam_t &s_input);
    I cannot understand why they pass the initParam_t &s_input into bsx_init() function.. It is like they create again the instance s_input..

    It couldn't be like this?

    Code:
    bsx_init(&s_input)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,834
    That could be a copy and paste error since it isn't valid C syntax. They probably wanted to demonstrate what you had in mind.
    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

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,110
    bsx_init(&s_input)
    Yes it should be.

    Did you try it?
    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.

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    33
    Thank you guys!!
    Quote Originally Posted by Salem View Post
    Did you try it?
    I didn't, I was just reading the integration guide.

    Also, I found this syntax into another documentation and I'm not sure why they're doing this.. I know that this is a pointer typecasting but I cannot unterstand how that works..

    Code:
    s_input.accelspec = (BSX_U8 *)&str_accsensorspec;
    s_input.magspec = (BSX_U8 *)&bsxLibConfMag;
    s_input.gyrospec = (BSX_U8 *)&bsxLibConfGyro; 
    s_input.usecase = (BSX_U8 *)&bsxLibConf;
    
    bsx_init(&s_input);

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,834
    What are the types of those variables and what are their values?
    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

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    33
    Code:
    typedef int8_t            BSX_S8;  /**< signed char */
    typedef uint8_t            BSX_U8;  /**< unsigned char */
    
    BSX_S8 bsx_init(initParam_t*);
    Code:
    typedef struct {
        BSX_U8 *accelspec;  
        BSX_U8 *magspec;    
        BSX_U8 *gyrospec;   
        BSX_U8 *usecase;    
        BSX_U8 accelspec_status; 
        BSX_U8 magspec_status;   
        BSX_U8 gyrospec_status;  
        BSX_U8 usecase_status;   
    }initParam_t;
    
    


    Code:
    BSX_U8 bsxLibConfAcc[] = {37,0,3,1,0,9,12,150,0,16,60,0,1,0,1,0,176,4,82,3,0,0,64,65,1,1,1,1,2,2,2,3,3,1,1,180,115};
    BSX_U8 bsxLibConfMag[] = {39,0,2,1,20,5,20,5,196,9,6,9,112,23,0,0,128,61,205,204,76,63,0,0,224,64,1,1,1,1,1,1,1,1,1,1,1,134,84};
    BSX_U8 bsxLibConfGyro[] = {14,0,1,1,3,9,12,136,19,16,1,1,129,46};
    BSX_U8 bsxLibConf[] = {116,6,1,1,...,114,8,0,13,226,109};// <- this one is really long so I skipped a few numbers here initParam_t s_input;
    
    s_input.accelspec = (BSX_U8 *)&bsxLibConfAcc;
    s_input.magspec = (BSX_U8 *)&bsxLibConfMag;
    s_input.gyrospec = (BSX_U8 *)&bsxLibConfGyro;
    s_input.usecase = (BSX_U8 *)&bsxLibConf; 
    
    bsx_init(&s_input);

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,834
    That makes no sense. Consider bsxLibConfAcc: it is an array of BSX_U8, so it is implicitly convertible to a BSX_U8*. Therefore, no cast is needed. What the code you saw did instead was to take the address of bsxLibConfAcc, which is a BSX_U8(*)[N], where N is the number of elements in bsxLibConfAcc. This is the wrong type, so a cast had to be used... but the net effect is to reinterpret the address of bsxLibConfAcc as a pointer to BSX_U8. Since this address is exactly the same in value as the address of the first element of bsxLibConfAcc, the code therefore achieves the exact same net effect as:
    Code:
    s_input.accelspec = bsxLibConfAcc;
    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

  8. #8
    Registered User
    Join Date
    Feb 2019
    Posts
    33
    Thank you very much!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing struct pointer to function.
    By Durango2011 in forum C Programming
    Replies: 3
    Last Post: 10-28-2011, 07:10 PM
  2. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  3. Passing a struct via pointer question
    By Thantos in forum C Programming
    Replies: 3
    Last Post: 11-29-2003, 06:54 PM
  4. Passing Pointer-to-struct to a function!!
    By Lau in forum C Programming
    Replies: 5
    Last Post: 11-18-2002, 12:59 PM
  5. Passing a pointer to a struct
    By Natase in forum C Programming
    Replies: 2
    Last Post: 10-02-2001, 10:29 AM

Tags for this Thread