Thread: Passing by Address

  1. #1
    Registered User jaaaaaamie's Avatar
    Join Date
    Aug 2013
    Location
    San Diego, California
    Posts
    10

    Cool Passing by Address

    Get error on gcc..

    might be typo but I can't find it.

    gcc -Wall pointer_5.c -o pointer_5
    pointer_5.c:6: warning: return type defaults to ‘int’
    pointer_5.c: In function ‘main’:
    pointer_5.c:11: warning: implicit declaration of function ‘half’
    pointer_5.c: At top level:
    pointer_5.c:16: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token



    Code:
    /* pointer_5.c */
    
    
    #include <stdio.h>
    
    
    main()
    {
        int i;
        printf("Please enter a number... ");
        scanf(" %d", &i);
        /* Now, passes the variable to a function */
        half(&i);    /* i is passed by address */
        printf("In main(), i is now %d.\n", i);
        return 0;
    }
    
    
    half(int i*)    /* Receives address of i */
    {
        *i = *i / 2;    /* Halves i */
        printf("Your value halved is %d.\n", *i);
        return;
    }

  2. #2
    Registered User jaaaaaamie's Avatar
    Join Date
    Aug 2013
    Location
    San Diego, California
    Posts
    10
    hey I found it, thanks anyway, line 19 should be

    Code:
    half(int *i)

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Glad you got it sorted. A few notes:

    Note, those other warnings are significant too. I prefer to always be explicit with a function return type and parameter list. It's safest to never omit either, plus it helps the compiler do some error checking for you, making sure you are calling the functions correctly

    main should be explicitly declared to return an integer. I also like to be explicit that main receives no parameters, though this is technically optional. Good job on actually returning a value though.
    Code:
    int main(void)
    See here for more info: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com.

    Also, if you don't put a return type, C assumes the function returns int. But you don't want your half function to return anything, so you should explicitly declare it to have a void return type. Also, you should either put the half function above main, or put a prototype above main. C compilers read files top-down, and if it hasn't seen a declaration yet, it doesn't know what it is. Thus, it can't throw an error if you call half incorrectly. Even worse, this can theoretically cause the compiler to generate incorrect code in some circumstances, and may cause a program crash. That is unlikely, but it's still important to be correct. Never leave stuff up to chance, instead:
    Code:
    void half(int *i);  // prototype -- looks just like the function itself, except with a ; at the end
    ...
    int main(void)  // takes no parameters, returns an int
    {
        ...
        half(&i);  // now, if you called half incorrectly (wrong parameter count/type or return value type), the compiler would complain
        ...
        return 0;  // good -- we said main would return an int, and we meant it!
    }
    
    void half(int *i)  // takes a pointer to int, returns nothing
    {
        *i = *i / 2;
        // probably no printf here -- a function should do one thing (halve a value) and do it well
        // also, no need for a return statement since it's a void function and there is nothing to return
        // generally, you only use return; if you need to return early from a void function, like when there is an error
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a structure by address
    By Sharke in forum C Programming
    Replies: 2
    Last Post: 03-24-2009, 12:43 AM
  2. Array address passing help please??
    By blackCat in forum C Programming
    Replies: 9
    Last Post: 03-09-2009, 07:33 AM
  3. Passing by refernce of address
    By lruc in forum C++ Programming
    Replies: 14
    Last Post: 09-03-2008, 02:21 AM
  4. Passing an address to a function
    By Vicious in forum C++ Programming
    Replies: 5
    Last Post: 09-12-2004, 12:05 PM
  5. passing by address vs passing by reference
    By lambs4 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2003, 01:25 AM