Convert code to use Pointers, Functions, etc.

This is a discussion on Convert code to use Pointers, Functions, etc. within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Source attached. How close am I to getting this to work with passing pointers as args? The second function seems ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    2

    Question Convert code to use Pointers, Functions, etc.

    Source attached. How close am I to getting this to work with
    passing pointers as args? The second function seems to be the problem.

    The objective is to store 5 integers(array). GET_DATA() accepts an array
    pointer, and prompts the user. FIND_NUM() accepts 'choice' and the array
    and searches for the value, and returns a value to MAIN().

    I had to improvise in MAIN(IF statement), because the pointer functionality
    isn't working.

    Thanks for the time.

    -JY
    Attached Files Attached Files

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Hmm...

    Hmmm... lab7.cpp? uhh... <HINT> homework </HINT>


    Sorry, I don't have time to open and look through your code. Put it directly into the post next time ...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    > FIND_NUM() accepts 'choice' and the array
    and searches for the value, and returns a value to MAIN().
    The question is, what value should it return?
    And what should it return if the value is not found?

    The loop in main is a waste, it's just duplicating what FIND_NUM is supposed to do.

    How about
    Code:
    int find_num( int *aptr, int found ) {
        int i;
        for(i = 0; i < 5; i++) {
            if(aptr[i] == found)
                return i;   // found - result is array position
        }
        return -1;   // not found
    }
    Then main just has
    Code:
    if ( num != -1 ) {
        // found
    } else {
        // not found
    }
    Oh, and
    void main(void)
    should be
    int main(void)
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. So I decided to use function pointers in my code for the first time...
    By A10 in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-13-2007, 06:13 AM
  2. how to convert decimal number to ASCII code?
    By oie in forum C Programming
    Replies: 11
    Last Post: 11-03-2006, 05:19 PM
  3. Replies: 14
    Last Post: 11-23-2005, 07:53 AM
  4. Problem with pointers and functions
    By Kheila in forum C++ Programming
    Replies: 5
    Last Post: 10-13-2005, 12:40 PM
  5. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM

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