Passing Pointers to Functions question..

This is a discussion on Passing Pointers to Functions question.. within the C Programming forums, part of the General Programming Boards category; why do we write only the signature of a function addNumbers at the start of the code and later we ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    Passing Pointers to Functions question..

    why do we write only the signature of a function addNumbers at the start of the code and later we write the whole function??

    how do we define that scanf command will be separated by spaces??



    Code:
    #include <stdio.h>
    
    int addNumbers(int *fiveNumbers);  
    
    int main() {
      int array[5];
      int i;
    
      printf("Enter 5 integers separated by spaces: ");
    
      for(i=0 ; i<5 ; i++) {
        scanf("&#37;d", &array[i]);
      }
    
      printf("\nTheir sum is: %d\n", addNumbers(array));
      return 0;
    }
    
    int addNumbers(int *fiveNumbers) {  /* define function */
      int sum = 0;
      int i;
    
      for(i=0 ; i<5 ; i++, fiveNumbers++) {
        sum+= *fiveNumbers);            /* work out the total */
      }
      return sum;                       /* return the total   */
    }
    Last edited by transgalactic2; 10-18-2008 at 02:52 PM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,609
    Because when the compiler sees you call addNumbers, it must know how that function looks like, and by the time it finds the function call, it must already know the function prototype.
    Since the actual function is below the code calling it, you have to make a prototype at top to help the compiler.

    And just a thought, but take a stab if you're interested in the difference between int* vs int *: http://www.research.att.com/~bs/bs_faq2.html#whitespace
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so each time
    when i am building a function i need to write its signature bellow the #include <stdio.h>

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Before the first time it's called. Conventionally above the definition of main().

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,609
    Yes, it's good practice.
    And it should usually be in a header file of its own, which you include too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    when you said

    "Yes, it's good practice."
    you meant its a good example for examining this subject

    practice by my vocabulary means experience in doing some thing

    or you mean practice=definition of this function signature

    ??

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Practice in the sense of, say, "sound accounting practices" or "practicing medicine". "Good practice" == "Things you better do if you don't want your boss to beat you up and put you in the dumpster".

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Structure Pointers to Functions
    By samus250 in forum C Programming
    Replies: 15
    Last Post: 03-20-2008, 03:13 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question about pointers
    By maxhavoc in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2004, 12:46 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  5. Pointers, arrays , functions
    By sballew in forum C Programming
    Replies: 19
    Last Post: 09-16-2001, 11:12 PM

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