Thread: Bad code in book?

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    43

    Question Bad code in book?

    Ok. I'm reading "Programming Roleplaying Games with DirectX" and at the beginning of the book, there's a C++ primer. The code on function overloading seems like nonsense to me.

    Prototypes/Definitions:

    Code:
    long AddNumbers ( long Num1, long Num2 );
    long AddNumbers ( long * NumArray, long NumOfNums );
    
    long AddNumbers ( long Num1, long Num2 )
    {
         return ( Num1 + Num2 );
    }
    
    long AddNumbers ( long * NumArray, long NumOfNums )
    {
         long Result, i;
    
         Result = 0;
    
         while ( NumOfNums-- )
              Result += NumArray [i];     // Hmm? i was not initialized!
    
         return Result;
    }
    In main:

    Code:
    long Array [5] = { 10, 20, 30, 40, 50 };
    
    Result = AddNumbers ( 10, 20 );
    Result = AddNumbers ( Array, 5 );     // Hmm? Shouldn't it be passing an address?

    Is this bad code? Or am I seeing wrong?

    Also, how could I fix this to pass the entire array? Would I be passing the address of Array?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Darklighter
    In main:

    Code:
    long Array [5] = { 10, 20, 30, 40, 50 };
    
    Result = AddNumbers ( 10, 20 );
    Result = AddNumbers ( Array, 5 );     // Hmm? Shouldn't it be passing an address?
    An array name used in an expression becomes the address of the first element.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    The code is correct. An array name generally decays into a pointer to the first element.

    Code:
    int array[3] = {12,13,14};
    std::cout << *array << std::endl;
    When i'm using the name array alone it is decaying into a pointer to int, pointing to the first element of the array. The * (dereference operator) is getting the value of array. The output of the code will be 12.

    Your code is taking advantage of that property of arrays. By passing the name of the array only it is actually passing a pointer to the first element of the array. It is passing a pointer to long. And your function is expecting exactly a pointer to long.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    43
    Alright, this explains part of it.

    But according to the book, Result should give 150 when passing the array, so shouldn't the function take the whole array and not a pointer to the first element, since it should add up all the elements of the array?

    Also, what is i's purpose anyways?

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    long AddNumbers ( long * NumArray, long NumOfNums )
    {
         long Result, i;
    
         Result = 0;
    
         while ( NumOfNums-- )
              Result += NumArray [i];     // Hmm? i was not initialized!
    
         return Result;
    }
    You're correct. 'i' needs to be initialized before the loop, and incremented during the loop.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    43
    Ah, it all makes sense now! Thanks a lot!

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >An array name generally decays into a pointer to the first element.
    Why are you people so mean to pointers? They didn't do anything to you.
    My best code is written with the delete key.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Prelude
    >An array name generally decays into a pointer to the first element.
    Why are you people so mean to pointers? They didn't do anything to you.
    C'mon. Everyone should know it's not nice to point.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. URGENT: Help wanted...in C
    By iamjimjohn in forum C Programming
    Replies: 16
    Last Post: 05-18-2007, 05:46 AM
  2. Books on C and C++
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-28-2002, 04:18 PM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Linux Programming
    Replies: 0
    Last Post: 10-14-2002, 01:30 PM
  4. Replies: 4
    Last Post: 01-16-2002, 12:04 AM