Thread: returning two structure value from a single fucntion

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    2

    returning two structure value from a single fucntion

    Hi,

    I have one function :
    Code:
    
    void con_get_dev_con_status_sync(ST_DEV_CONNECTION_STATUS_NOTIFY *dev_info)
    { 
        printf("HF PMASK :: %d\n",SyncData.dev_info1.profile_mask);
        printf("AUD PMASK :: %d\n",SyncData.dev_info2.profile_mask);
        if(SyncData.dev_info1.profile_mask == 0x01)
        {
            printf("Dev1 SYNC INFO\n");
            dev_info>connection_status=SyncData.dev_info1.connection_status;
            dev_info->devHandle=SyncData.dev_info.devHandle;
            dev_info->devNameInfo=SyncData.dev_info.devNameInfo;
        }
        else if(SyncData.aud_dev_info.profile_mask == 0x06)
        { 
            printf("Dev2 SYNC INFO\n");
            dev_info>connection_status=SyncData.dev_info2.connection_status;
            dev_info->devHandle=SyncData.dev_info2.devHandle;
            dev_info->NameInfo=SyncData.dev_info.devNameInfo;
        }
    }
    In above implentation, dev_info struct will be over written when
    SyncData.dev_info1.profile_mask = 0x01 and
    SyncData.dev_info2.profile_mask = 0x06

    Can anyone please suggest how i can get the structure ST_DEV_CONNECTION_STATUS_NOTIFY for both dev1 and dev2 at the same time using a single function call.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Om178890
    Can anyone please suggest how i can get the structure ST_DEV_CONNECTION_STATUS_NOTIFY for both dev1 and dev2 at the same time using a single function call.
    I am not clear as to what you are trying to do. Are you trying to modify con_get_dev_con_status_sync so as to get both with a single call to con_get_dev_con_status_sync? If so, the solution is simple: change con_get_dev_con_status_sync to have two parameters.
    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
    Registered User
    Join Date
    Apr 2015
    Location
    Bangalore, Karnataka, India
    Posts
    34
    Quote Originally Posted by Om178890 View Post
    Hi,

    I have one function :
    Code:
    
    void con_get_dev_con_status_sync(ST_DEV_CONNECTION_STATUS_NOTIFY *dev_info)
    { 
        printf("HF PMASK :: %d\n",SyncData.dev_info1.profile_mask);
        printf("AUD PMASK :: %d\n",SyncData.dev_info2.profile_mask);
        if(SyncData.dev_info1.profile_mask == 0x01)
        {
            printf("Dev1 SYNC INFO\n");
            dev_info>connection_status=SyncData.dev_info1.connection_status;
            dev_info->devHandle=SyncData.dev_info.devHandle;
            dev_info->devNameInfo=SyncData.dev_info.devNameInfo;
        }
        else if(SyncData.aud_dev_info.profile_mask == 0x06)
        { 
            printf("Dev2 SYNC INFO\n");
            dev_info>connection_status=SyncData.dev_info2.connection_status;
            dev_info->devHandle=SyncData.dev_info2.devHandle;
            dev_info->NameInfo=SyncData.dev_info.devNameInfo;
        }
    }
    In above implentation, dev_info struct will be over written when
    SyncData.dev_info1.profile_mask = 0x01 and
    SyncData.dev_info2.profile_mask = 0x06

    Can anyone please suggest how i can get the structure ST_DEV_CONNECTION_STATUS_NOTIFY for both dev1 and dev2 at the same time using a single function call.
    Looks like you have not placed the complete code here.

    You have passed (ST_DEV_CONNECTION_STATUS_NOTIFY *dev_info) to the function but you are accessing some values from struct SyncData (must be a global structure or at least a static struct which can be accessible from the same file).

    The SyncData has SyncData.dev_info1 and SyncData.dev_info2.

    If I am not wrong then you are thinking to pass both the SyncData.dev_info1 and SyncData.dev_info2 structure pointers to the function instead of accessing them from the global struct SyncData.

    You can do it by taking an array of pointers to structures of type ST_DEV_CONNECTION_STATUS_NOTIFY.

    Have a look on the below declaration:
    ST_DEV_CONNECTION_STATUS_NOTIFY *dev_info_array[2];

    dev_info_array[0] = SyncData.dev_info1;
    dev_info_array[1] = SyncData.dev_info2;

    Now call the function with:
    con_get_dev_con_status_sync(dev_info_array);

    and inside the function definition access the dev_info1 from index 0 and dev_info2 from index 1 off dev_info_array.

    If this is not you want then place the complete code if possible and clarify what you want.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 09-06-2011, 02:59 PM
  2. problem returning structure
    By madusha in forum C Programming
    Replies: 3
    Last Post: 07-16-2011, 03:06 AM
  3. Having trouble returning a structure.
    By awr7126 in forum C Programming
    Replies: 7
    Last Post: 03-28-2011, 05:08 PM
  4. method returning a pointer to a structure
    By nacho4d in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2009, 10:01 PM
  5. Reading a File and returning single words
    By kapri in forum C++ Programming
    Replies: 5
    Last Post: 05-11-2005, 05:13 AM