Thread: pointer

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    16

    pointer

    We have two programs. First program is
    whit POINTER and the second program is without the Pointer.
    Both program print out the same result. but why we should use POINTER.

    Code:
    /*Cube a variable using call-by-reference with a pointeargument*/
    #include <stdio.h>
       void cubeByReference( int *nPtr ); 
       int main()
    {
            int number = 5; 
            printf( "The original value of number is %d", number );
     
            cubeByReference( &number );
            printf( "\nThe new value of number is %d\n", number );
      return 0; 
    } 
    void cubeByReference( int *nPtr )
    {
       *nPtr = *nPtr * *nPtr * *nPtr; 
    }
    Code:
    #include <stdio.h>
    int cubeByValue( int n ); /* prototype */
      int main()
    {
       int number = 5; 
       printf( "The original value of number is %d", number );
       number = cubeByValue( number );
    
       printf( "\nThe new value of number is %d\n", number );
       return 0; 
    } 
    int cubeByValue( int n )
    {
       return n * n * n;   
    
    }

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Well, just as it was mentioned in this thread of yours, that it all depends on what you want to do. Opinions vary, but I would tend to think that if you don't need a pointer, don't use it. On the other hand, if its the easiest way to get the job done, go for it.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Look at the two functions, and notice a difference in how they work. That's the point of this exercise.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM