Calling functions from functions

This is a discussion on Calling functions from functions within the C Programming forums, part of the General Programming Boards category; Hi I have in my application two functions which I call from main, called void write() and void ArrayFill(), which ...

  1. #1
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83

    Calling functions from functions

    Hi

    I have in my application two functions which I call from main, called

    void write() and void ArrayFill(), which I can call from main() and operating as expected.

    I also have a function called defaultFunction which takes a number of parameters

    Code:
    defaultFunction(mult_ptr, tenMinCount_ptr, hourCount_ptr, ee_addr01, ee_addr02, hours_array_ptr);
    and I want to call write() and ArrayFill() from there, but they do not operate as I would expect. This is what my defaultFunction looks like

    Code:
    void defaultFunction(uint8_t *multPtr, uint8_t *tenmin_ptr, uint32_t *hrcount_ptr, uint8_t *e2p_add01, uint8_t *e2p_add02, uint8_t *hrsarr)
    {
        //uint32_t RTC_Val = *rtcPtr;
        
        uint32_t hours_count = *hrcount_ptr;
        uint8_t multiple = *multPtr;
        uint8_t tenMin_count = *tenmin_ptr;
        uint8_t eep_add01 = *e2p_add01;
        uint8_t eep_add02 = *e2p_add02;
        uint8_t hr_arr_ptr = *hrsarr;
        
        GPIO_SetBits(GPIOB, GPIO_Pin_2);                                    // set the PCH power control ON        
        
        RTC_SetCounter(0);
    
        while(1)
        {
            uint32_t RTC_counter = RTC_GetCounter();
            IWDG_ReloadCounter();                                        // reset IWDG timer to 0x186A (10 secs)
        
            if ((multiple > 6) || (tenMin_count > 5))
            {
                multiple = 1;
                tenMin_count = 0;
            }
        
            if (RTC_counter == (600 * multiple))                                    // triggers at 600,1200,1800,2400 seconds
            {
                tenMin_count++;                                                      // increment ten minute counter
                multiple++;                                                        // increment multiplier value
                EEPROMWrite(multiple, tenMin_count, eep_add01, eep_add02);
            }
    
            if (RTC_counter == 3600)                                            // triggers each hour
            {
                hours_count++;                                                       // increment hours counter
                tenMin_count = 0;                                               // reset ten minute count                                                                          
                multiple = 1;                                                    // reset multiplier
                RTC_SetCounter(0);
                numArrayFill(hr_arr_ptr, hours_count, sizeof(hours_array));
            }
        }
    }
    Why would the call from this function operate differently than a call from main? This code is a duplicate of a section of main, so I am expecting its the way I have called my parameters, but I can't work out where I have gone wrong.

    Thanks
    Dave

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by droseman View Post
    Why would the call from this function operate differently than a call from main?
    It shouldn't, so you are right to be suspicious. You will have to describe better what you mean by differently tho, to solve the problem.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    > numArrayFill(hr_arr_ptr, hours_count, sizeof(hours_array));
    What is this sizeof() doing?

    If the array was in scope (in main), it would do one thing.
    If all you have is a pointer, it will do another.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83
    Quote Originally Posted by Salem View Post
    > numArrayFill(hr_arr_ptr, hours_count, sizeof(hours_array));
    What is this sizeof() doing?
    hours_array is a global array variable which is also declared extern in a header file

    Quote Originally Posted by MK27 View Post
    You will have to describe better what you mean by differently
    The function EEPROM write calls a function which writes data to an attatched EEPROM device to be read back when the application is restarted - I get some incorrect characters read back - I would post the code for that, but probably no point as its full of processor specific libs which would be no help.

    The function numArrayFill takes hours_count and outputs a 5 element array, which is stored in hours_array. When this is called, the timer stops and the array is not incremented.

  5. #5
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83

    fixed - sort of...

    due to looming deadline - the whole function was moved to scope main, choosing with an if...else statement.

    Would have been nice to do the other way, since it made main() a bit simpler, but customers have to be kept happy...

    --dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions calling other functions.
    By kbro3 in forum C++ Programming
    Replies: 2
    Last Post: 12-26-2009, 11:10 PM
  2. Problems calling functions
    By barryr in forum C Programming
    Replies: 12
    Last Post: 12-03-2009, 07:44 AM
  3. Replies: 12
    Last Post: 04-12-2009, 05:49 PM
  4. calling functions: exit and return
    By 911help in forum C Programming
    Replies: 3
    Last Post: 12-28-2007, 12:24 PM
  5. Calling functions help
    By ForlornOdium in forum C++ Programming
    Replies: 14
    Last Post: 09-29-2003, 08:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21