Thread: Is there a difference between these prototypes?

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    244

    Is there a difference between these prototypes?

    I'm reading "Teach Yourself C++ in 21 Days" by Jesse Liberty, 3rd ed.

    In a few code samples, but specifically listing 9.8, some function prototypes are defined as follows:

    short Factor(int n, int* pSquared, int* pCubed);

    What is the difference, if any, between that and this:

    short Factor(int n, int *pSquared, int *pCubed);

    It doesn't explain the difference if there is any, but I suspect it is a typographical error.

    Would anyone care to share some insight?

    Thanks!

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    The only difference is where the asterik (*) is placed.
    For the parameter the two are as follows per your example.
    #1) short Factor(int n, int* pSquared, int* pCubed);

    #2) short Factor(int n, int *pSquared, int *pCubed);

    The placing of the * does not matter. It is all personal preference.

    int *pInt = NULL; // Pointer to an integer

    is the same as...

    int* pInt = NULL; // Pointer to an integer

    Personally, I prefer the first notation of placing the asterik next to the name, it is how I learned pointers. Anyway hope this helped you.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    C/C++ doesnt care where you have the asterik(*) for a pointer. int* ptr; and int *ptr; are the same things ... just a matter of style.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Thanks for the quick response.

    I suspected that was the case (as it would have been silly otherwise), but I wish this book had been consistent so as to not confused would-be newbies such as myself

  5. #5
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    actually i read that book and its unfortunatly full of "typographical" errors...
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    I tend to use the "int *ptr;" sytax rather than "int* ptr;" because it sets me up for making this mistake... "int* ptr, ptr1;" that's just maybe only I make that mistake

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding the difference in two times
    By muthus in forum C++ Programming
    Replies: 4
    Last Post: 01-24-2008, 06:36 PM
  2. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  3. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  4. Replies: 6
    Last Post: 08-26-2006, 11:09 PM
  5. Replies: 10
    Last Post: 11-06-2005, 09:29 PM