pointers, functions, parameters

This is a discussion on pointers, functions, parameters within the C Programming forums, part of the General Programming Boards category; I was hoping someone could help explain to me the difference of the following functions declarations especially those with pointers: ...

  1. #1
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    pointers, functions, parameters

    I was hoping someone could help explain to me the difference of the following functions declarations especially those with pointers:


    Code:
    void add_to_list(struct node **list, int n)   
    
    void myFunc(char *a, char *b)
    
    void *myFunc(char *a, char *b)
    
    void *myStruct(char a[], int b)
    
    int Sum(int a, char b[])
    
    void Sum(void)


    How do you know when to return a value ??
    How do you know when not to ??
    (I know to use void when not returning a value)

    What is happening with/in a function with a pointer to the function itself?? explain pointers to functions
    (void *myFunc(char *a, char b)

    explain what's happening with Pointers as arguments
    Code:
    void decompose(float x, int *int_part, float *frac_part)
    {
         *int_part = (int) x;
         *frac_part = x - *int_part;
    }
    explain pointers to pointers

    Code:
    void add_to_list(struct node **list, int n)   /* what does the **list mean /?? */
    {
        struct node *new_node;
        new_node = malloc(sizeof(struct node));
         if (new_node == NULL)   {
            printf("Error:  malloc failed in add_to_list\n");
            exit (EXIT_FAILURE);
          }
         new_node->value = n;
         new_node->next = *list;         /* what is *list pointing to */
         *list = new_node;
      }
    Sue B.

    dazed and confused


  2. #2
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    That's a lot of diffucult questions, I'll do my best and hope someone more knowledgeable corrects me if I make a mistake.

    void add_to_list(struct node **list, int n)

    That's an easy one, add_to_list is a function that returns nothing and takes a pointer to a pointer to a structure called node, and an int called n. From the names I'll assume that this function is part of a linked list that takes the list as a parameter. But instead of passing the whole list which would take a huge amount of memory if it's even possible, it simply passes a pointer to the head node of the list.

    void myFunc(char *a, char *b)

    This is a function that returns nothing and takes two pointers to char. Meaning the pointers will access the address being pointed to and change variables outside of the function without having to pass them. This one is kind of questionable because I think char holds less space in memory than a pointer to char, so the memory savings would be useless.

    void *myFunc(char *a, char *b)

    This is a function that returns a pointer to void and takes two char pointers as parameters. Not too sure if this was intended to be a pointer to a function or a function with a pointer return.

    void *myStruct(char a[], int b)

    This is a function that returns a pointer to void and takes a char array called a and an int called b.

    void Sum(void)

    This is a function that returns nothing and takes nothing as a parameter.
    ---------------------------------------

    You return a value when you need to use that value outside of the function.
    ie. When you add two integers inside a function, but print it out in the main().

    You return void when the function is all you need to accomplish your goal. If you call a function from main and calculate two integers then print them inside the function, you have no need to return a value and can just exit the function without returning.

    I'm still a bit hazy about pointers to functions. Sorry.

    Pointers being used as parameters will allow you to pass a variable by reference. Meaning you can pass a pointer to a variable and not need to pass the variable itself to change it inside a function. This doesn't sound too great except that pointers usually take up a great deal less memory and when you pass a variable itself to a function, a copy is made of it. You change the copy instead of the original.

    (void *myFunc(char *a, int b)

    That's both an error and I think it's not a pointer to a function anyway. I could be wrong, but
    void (myFunc*)(char *a, int b)
    is a pointer to a function, and don't forget to match opening and closing parens or you'll get errors. Or worse, you won't get errors
    pointer = NULL

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    >> void add_to_list(struct node **list, int n)
    >
    > That's an easy one, add_to_list is a function that returns
    > nothing and takes a pointer to a pointer to a structure called
    > node, and an int called n. From the names I'll assume that this
    > function is part of a linked list that takes the list as a
    > parameter.

    Yup. Good there.

    > But instead of passing the whole list which would take a huge
    > amount of memory if it's even possible, it simply passes a
    > pointer to the head node of the list.

    Close. Passing a linked list NEVER takes a lot of memory. You are
    only ever passing a single pointer, which takes the same amount
    of memory as this does.

    What I believe this is actually doing, is passing the 'anchor node'
    to the function, and then updating the value of the anchor node:

    Node *anchor; //our list

    add_to_list( &anchor, 5 ); //pass the address of anchor

    //and in the function, something like this (pseudocode):
    Code:
    {
        Node *newNode = allocate some space
        newNode->value = n
        newNode->next = *list_n;
        *list_n = newNode;
    }
    Thus, at the end of the function, 'anchor' now points to the new
    node. Assuming that 'anchor' is pointing at an allocated block of
    memory, which likely points to others (ie: a linked list), in the end,
    we're making sure that 'anchor' is still the "top" of the lists, by
    updating its value within the function itself.

    >> void myFunc(char *a, char *b)
    >
    > This is a function that returns nothing and takes two pointers
    > to char. Meaning the pointers will access the address being
    > pointed to and change variables outside of the function without > having to pass them. This one is kind of questionable because I > think char holds less space in memory than a pointer to char,
    > so the memory savings would be useless.

    Close. This could potentially have two meanings:

    1) Pass it a single character, so you can change that variable.
    2) Pass it a "string".

    Remember that a "character pointer" is often treated as a single
    string. Thus, in reality, since we're passing an address of a char
    to this function, we could in effect be passign it 5000 characters
    in the form of a string.

    When you dereference a pointer, you are accessing the location
    it points to directly. Thus:

    int x, *ptrx;

    x = 5; //set the value of x directly
    ptrx = &x; //make 'ptrx' point to the same memory location as x
    *ptrx = x; // same as above.

    ptrx = //pass it an address
    *ptrx = 3; //set the value of x using a pointer


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I don't know if it helps, but it's fairly accurate to think of memory as a large array. The variables are elements of the array. The pointer to an element is the index of that element. This has two effects...

    1. If the elements are large, then it will be efficient to just pass the index to an element rather than the entire element (not always true...).
    2. If you want to change the value in an element, you will need to pass the calling function the index of that element (as opposed to passing it the value of that element).
    Code:
    void decompose(float x, int *int_part, float *frac_part)
    {
         *int_part = (int) x;
         *frac_part = x - *int_part;
    }
    The reason that this function uses pointers is because it is meant to change the valuable of variables. It does this instead of just returning a value because it changes two values, and it can't have 2 returns...
    Code:
    int ip;
    float fp;
    decompose (2.34, &ip, fp)
    // Now ip is 2 and fp is .34
    So the function doesn't give a hoot about being passed an int or float; it just needs the address to an int and the address to a float so it can change their values. As for the logic of the function... well, casting a float to an int will reduce it to it's integer part, and subtracting the integer part from a float will only leave it's fractional part left over.

    A void pointer is really just a generic pointer. If you're making a function where you really don't give a hoot about what kind of variable you are addressing (like, say, malloc), then you should use a void pointer.

    A pointer to a function is the only way to pass a function to a function. Think of a function as just a list of instructions that's already in memory, and when you pass a pointer to a function, you're just telling the machine where those instructions start.
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers to Class Member Functions
    By Dark_Phoenix in forum C++ Programming
    Replies: 6
    Last Post: 09-02-2007, 02:21 PM
  2. Reg pointers to functions
    By sowmi in forum C Programming
    Replies: 1
    Last Post: 06-22-2007, 12:08 AM
  3. Passing arrays of pointers into functions
    By ashley in forum C Programming
    Replies: 5
    Last Post: 01-13-2007, 05:48 PM
  4. File pointers as function parameters
    By Metalix in forum C Programming
    Replies: 21
    Last Post: 01-18-2005, 03:36 AM
  5. function pointers and member functions
    By thor in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2002, 03:22 PM

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