Just a simple explanation!

Discussion in 'C' started by Alex Xander, Mar 21, 2015.

  1. Alex Xander

    Alex Xander New Member

    Joined:
    Mar 21, 2015
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I'd really like to know what type of arguments does this function take, and what happens in the for loop.

    void f(unsigned a[],unsigned b[],unsigned n,unsigned *c,unsigned(*t)(unsigned))
    {
    for(int i = 0; i<n;i++)
    b = (*t)(a);
    }

    I realize that first 2 are arrays, the next one is an unsigned number, what about the last two?
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    c is a pointer to unsigned int and is not used by the function.

    t is a pointer to a function which takes an unsigned int and returns an unsigned int.

    The loop reads the content of a[] (which is assumed to have been initialised by the caller), passes it to t(), and stores the return value of t() in b[]. b should be at least the same size as a.

    Useful to keep this example as a reference of how function pointers work.
    It's also a good example of how NOT to comment your code.
     
  3. todd12345

    todd12345 New Member

    Joined:
    Sep 30, 2015
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    C is a general-purpose, imperative computer programming language, supporting structured programming, lexical variable scope and recursion, while a static type system prevents many unintended operations.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice