Thread: passing variables among c functions

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    22

    passing variables among c functions

    hello all
    I have this code where I am trying to retrieve the contents of the variable dev1 and dev2. for some reason when i compile and run I am getting 0 and 0. What am i doing wrong?thanks

    Code:
    typedef struct 
    {
        uint32_t x;
        uint32_t y;
    } sample;
    
    
    void get_sample(sample *one)
    {
      sample var1,var2;
      uint32_t dev1,dev2;
    
    //does something to dev1 and dev2
    
    
    return dev1 and dev2;
    }
    
    
    int main()
    {
    
      uint32_t dev1, dev2;
      sample *one;
      get_sample(&one);
      
      printf("%4x%4x\n",dev1,dev2);
      //I want to retrieve the value dev1 and dev2, print dev1 and dev2 and join them to form one string
    }
    Last edited by amaturequestion; 03-30-2015 at 08:31 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You cannot return dev1 and dev2 because:
    • You can only return one object from a function.
    • The function's return type is void.

    Also, in main you declared one to be a pointer to sample, then you passed &one as the argument to get_sample, but get_sample's parameter is a pointer to sample, not a pointer to a pointer to sample.

    Perhaps, you should declare get_sample like this:
    Code:
    void get_sample(sample *one, uint32_t *dev1, uint32_t *dev2);
    Now, you work with *dev1 and *dev2, and the changes to these variables will be reflected in the caller. The caller, in this case the main function, will presumably call get_sample like this:
    Code:
    sample one;
    uint32_t dev1;
    uint32_t dev2;
    get_sample(&one, &dev1, &dev2);
    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
    Mar 2015
    Posts
    22
    Thank you for your reply.

    I followed your suggestion and changed my code. However, now instead of getting the contents of dev1 and dev2 I am getting their addresses.Did i miss something?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    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

  5. #5
    Registered User
    Join Date
    Mar 2015
    Posts
    22
    Ok My current code is
    Code:
    typedef struct
    {
        uint32_t x;
        uint32_t y;
    } sample;
     
     
      int get_sample(sample *one, uint32_t *dev1, uint32_t *dev2);
    {
      sample var1,var2;
      // do something with dev1 and dev2 
     
      UART_printf(" %4x%4x \r\n ",dev1,dev2);//getting correct value
    
    return dev1;
    }
     
     
    int main()
    {
      sample one;
      uint32_t dev1, dev2; 
    
      get_sample(&one, &dev1, &dev2);
       UART_printf(" %4x \r\n ",dev1);
    //here i am getting some random number
    //I want dev1,dev2 in main to be same as in get_sample function. 
    }
    Also I am returning only dev1 while dev2 is left out.How to return both ?do i need to create a separate func to retrieve it??
    thanks
    Last edited by amaturequestion; 03-30-2015 at 02:02 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That cannot possibly be your current code because it will not compile. Furthermore, dev1 and dev2 in get_sample are pointers, yet you are printing them without dereferencing them: there is no way that you could be "getting the correct value" from that UART_printf, except by some incredible coincidence.

    Here's a variation of what you wrote:
    Code:
    #include <stdio.h>
    #include <stdint.h>
    
    typedef struct
    {
        uint32_t x;
        uint32_t y;
    } sample;
    
    void get_sample(sample *one, uint32_t *dev1, uint32_t *dev2)
    {
        *dev1 = 123;
        *dev2 = 456;
        printf("dev1=%4x; dev2=%4x\n", *dev1, *dev2);
    }
    
    int main(void)
    {
        sample one;
        uint32_t dev1, dev2;
        get_sample(&one, &dev1, &dev2);
        printf("dev1=%4x; dev2=%4x\n", dev1, dev2);
        return 0;
    }
    It prints:
    Code:
    dev1=  7b; dev2= 1c8
    dev1=  7b; dev2= 1c8
    Hence dev1, dev2 in main has the same value as in the get_sample function.
    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

  7. #7
    Registered User
    Join Date
    Mar 2015
    Posts
    22
    let me put my code
    Code:
    //this is just a part of the code
    #include <stdio.h>
    #include <stdint.h>
     
    typedef struct
    {
        uint32_t x;
        uint32_t y;
    } sample;
     
    void get_sample(sample *one, uint32_t *dev1, uint32_t *dev2,uint32_t *dev2)
    
    {
        sample x,y;
        dev1 = *(__IO uint32_t*)(0x1FFFF7E8);
        dev2 = *(__IO uint32_t*)(0x1FFFF7EC);
        dev3 = *(__IO uint32_t*)(0x1FFFF7F0);
        dev1 += dev2;
    
    
        one->x=dev1;
        one->y=dev2;
        UART_printf("%4x %4x\n",dev1,dev2);
    }
    
    
     
    
    int main(void)
    {
        sample one;
        uint32_t dev1, dev2, dev3;
    
        get_sample(&one, &dev1, &dev2, &dev3);
        UART_printf("%4x %4x",dev1,dev2);
    
        return 0;
    }
    
    output   2CD487C2 30354131 (from function get_sample)
               4427EE17BBB25982 (from main)
    Is there something I am doing wrong??Also thank you for your constant efforts.AMAZING

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by amaturequestion
    Is there something I am doing wrong??
    Yes, you did not dereference your pointers when you want to access what they point to. So this:
    Code:
    dev1 = *(__IO uint32_t*)(0x1FFFF7E8);
    dev2 = *(__IO uint32_t*)(0x1FFFF7EC);
    dev3 = *(__IO uint32_t*)(0x1FFFF7F0);
    dev1 += dev2;
    should have been:
    Code:
    *dev1 = *(__IO uint32_t*)(0x1FFFF7E8);
    *dev2 = *(__IO uint32_t*)(0x1FFFF7EC);
    *dev3 = *(__IO uint32_t*)(0x1FFFF7F0);
    *dev1 += *dev2;
    That said, I notice that you wrote:
    Code:
    one->x=dev1;
    one->y=dev2;
    This is also wrong because you need to dereference dev1 and dev2, but it looks like you don't need dev1 and dev2 to be parameters begin with. Rather, you could write:
    Code:
    void get_sample(sample *one)
    {
        uint32_t dev1;
        uint32_t dev2;
        dev1 = *(__IO uint32_t*)(0x1FFFF7E8);
        dev2 = *(__IO uint32_t*)(0x1FFFF7EC);
        dev1 += dev2;
    
        one->x = dev1;
        one->y = dev2;
        UART_printf("%4x %4x\n", dev1, dev2);
    }
    Then in main:
    Code:
    get_sample(&one);
    UART_printf("%4x %4x", one.dev1, one.dev2);
    Quote Originally Posted by amaturequestion
    Also thank you for your constant efforts.AMAZING
    You're welcome
    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

  9. #9
    Registered User
    Join Date
    Mar 2015
    Posts
    22
    Hello laserlight ..Thank u for the suggestion. It is working fine now..
    Now I have to send contents of these two dev using a function

    Code:
    void Send_Packet(UINT8 type, const UINT8* pbuf, UINT8 len)
    I want to combine the two hex numbers and fit it in UNIT8* pbuf.However wen I do a


    uint32_t pbuf=(((uint32_t)one->y)<< 32) | (uint32_t(one->x));

    and print pbuf it gives 30354131 instead of 27CA6159 30354131..Is there any other way of to achieve this?Is it a good idea to use memcopy instead??
    Last edited by amaturequestion; 03-31-2015 at 04:16 AM.

  10. #10
    Registered User
    Join Date
    Mar 2015
    Posts
    22
    Thank u all..I found a way out.Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing variables between functions
    By sababa.sababa in forum C Programming
    Replies: 8
    Last Post: 11-11-2012, 08:52 AM
  2. help with passing variables to different functions
    By Draylath in forum C++ Programming
    Replies: 4
    Last Post: 07-03-2012, 01:22 PM
  3. passing variables between functions
    By owi_just in forum C Programming
    Replies: 6
    Last Post: 05-08-2005, 09:12 AM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. passing variables to functions?
    By aoe in forum C Programming
    Replies: 12
    Last Post: 06-02-2002, 04:19 PM

Tags for this Thread