Pointer practice -- need critique please

This is a discussion on Pointer practice -- need critique please within the C Programming forums, part of the General Programming Boards category; Hi I am just learning pointers in C. I made an example program to test how they work. The program ...

  1. #1
    Registered User jimtuv's Avatar
    Join Date
    May 2010
    Location
    Sylvania, Ohio
    Posts
    94

    Pointer practice -- need critique please

    Hi I am just learning pointers in C. I made an example program to test how they work. The program works and does what I thought it would. My question is this. My comments are how I thought that the program works. I need to make sure that what I said is true. I am pretty sure I understand what is happening but have no one to check my work.

    Code:
    /* 
    Pointer practice using function calls. Here I set up two variables
    (a) and (b). Then two pointers (pa to point at a) and (pb to point at b)
    next I built two functions that will update the value in (a) and (b)
    using the pointers (pa) and (pb). Notice that I do not have to use the
    & symbol in the scanf because the (v) and (w) are already pointers pointing
    to the address of (a) and (b) via (pa and (pb).
    */
    #include <stdio.h>
    
    
    void ChangeIt ( int *x, int *y );
    void UserChange ( int *v , int *w );
    
    
    int main (void){
    	
    	int a;	//	Initialize a
    	int b;	//	Initialize b
    	int* pa;	//	Initialize pointer pa
    	int* pb;	//	Initialize pointer pb
    	
    	pa = &a;	//	pa = address of a set the pointer to point
    	pb = &b;	//	pb = address of b set the pointer to point
    	
    	*pa = 10;  //	place 10 in (a) via dereferencing pa
    	*pb = 20;	//	place 20 in (b) via dereferencing pb
    	
    	printf ( " a = %d: b = %d:\n", a, b); //   print what is in a and b
    	ChangeIt ( pa ,pb );//		This calls ChangeIt with the address for a and b
    	printf ( " a = %d: b = %d:\n", a, b);	//print the result of ChangeIt
    	UserChange ( pa , pb);//			This uses the same technique as ChangIt 
    	printf ( " a = %d: b = %d:\n", a, b);
    	
    	
    	return 0;
    }
    
    void ChangeIt ( int *x, int *y ){
    
    	*x = *x +5; // 	add 5 to the value at the adress pointed to by x >> a
    	*y = *y - 8;//	subtract 8 from the value at the address pointed to by y >> b
    
    	
    }
    void UserChange ( int *v , int *w ){
    	
    	printf ( "Enter a new number for a:");
    	scanf ( "%d", v );   // v is already a pointer so no & is needed
    	printf ( "\nEnter a new number for b:");
    	scanf ( "%d", w); // v and w point to the pointer pa and pb which point to a and b
    }
    I know I could have change this
    Code:
    ChangeIt ( pa ,pb );
    to
    Code:
    ChangeIt ( &a, &b );
    and done away with pa and pb. But I wanted to play around with dereferencing as in
    Code:
    *pa = 10;  //		place 10 in (a) via dereferencing pa
    *pb = 20;	//		place 20 in (b) via dereferencing pb

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your comments marked as "initializing" don't; you are declaring variables, but at that point they are still uninitialized and could contain anything.

    Otherwise, everything looks all correct. Once you get pointer arithmetic down, you'll be all set.

  3. #3
    Registered User jimtuv's Avatar
    Join Date
    May 2010
    Location
    Sylvania, Ohio
    Posts
    94
    Ooops. You are right I corrected the comments. Initialization is when the variable first get assigned a value otherwise it is undefined what it contains.

    Here is the corrected code
    Code:
    /* 
    Pointer practice using function calls. Here I set up two variables
    (a) and (b). Then two pointers (pa to point at a) and (pb to point at b)
    next I built two functions that will update the value in (a) and (b)
    using the pointers (pa) and (pb). Notice that I do not have to use the
    & symbol in the scanf because the (v) and (w) are already pointers pointing
    to the address of (a) and (b) via (pa and (pb).
    */
    #include <stdio.h>
    
    
    void ChangeIt ( int *x, int *y );
    void UserChange ( int *v , int *w );
    
    
    int main (void){
    	
    	int a;	//	Declare a
    	int b;	//	Declare b
    	int* pa;	//	Declare pointer pa
    	int* pb;	//	Declare pointer pb
    	
    	pa = &a;	//	Initialize pa = address of a set the pointer to point
    	pb = &b;	//	Initialize pb = address of b set the pointer to point
    	
    	*pa = 10;  //	Initialize (a) with 10 via dereferencing pa
    	*pb = 20;	//	Initialize (b) with 20  via dereferencing pb
    	
    	printf ( " a = %d: b = %d:\n", a, b); //     print what is in a and b
    	ChangeIt ( pa ,pb );//		This calls ChangeIt with the address for a and b
    	printf ( " a = %d: b = %d:\n", a, b);	//	print the result of ChangeIt
    	UserChange ( pa , pb);//				This uses the same technique as ChangIt 
    	printf ( " a = %d: b = %d:\n", a, b);
    	
    	
    	return 0;
    }
    
    void ChangeIt ( int *x, int *y ){
    
    	*x = *x +5; // 	add 5 to the value at the adress pointed to by x >> a
    	*y = *y - 8;//	subtract 8 from the value at the address pointed to by y >> b
    
    	
    }
    void UserChange ( int *v , int *w ){
    	
    	printf ( "Enter a new number for a:");
    	scanf ( "%d", v );   // v is already a pointer so no & is needed
    	printf ( "\nEnter a new number for b:");
    	scanf ( "%d", w); // v and w point to the pointer pa and pb which point to a and b
    }
    Thanks for checking it over for me. I have to remember to be careful with the terms.

  4. #4
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Code:
    scanf ( "%d", w); // v and w point to the pointer pa and pb which point to a and b
    This is off. v and w are not pointers-to-pointers. They are pointers that point to the same address as pa and pb.

    Think like this:
    Code:
    UserChange ( int *v = pa, int *w = pb);   // Initialize v and w to point to the same address as pa and pb, and call the function

  5. #5
    Registered User jimtuv's Avatar
    Join Date
    May 2010
    Location
    Sylvania, Ohio
    Posts
    94
    Thank you so much! This is exactly why I need some input.

    You are right v and w do in fact contain only the address of a and b not the address of pa nor pb. My bad. They got the address of a and b from pa and pb when I used them as arguments during the function call to UserChange.

    -----------------------------------------------------------------------------------------------------
    Just to check my understanding of the terms:

    Declaration: Is stating the type and name of the variable as in:

    int foo; // declares an integer named foo
    double bar; // declares an double floating point variable named bar
    char bleap; // declares a character type named bleap
    float numb;// declares a floating point variable named numb;

    Initialization: Is the first time a variable is assigned a value. If not initialized it can and does contain some random undefined value.

    pointer: is a variable that holds the address of another variable. A pointer must have the same type of the variable it points to.

    A pointer does not point to any specific variable address until it is initialized or assigned. Uninitialized pointers contain a undefined value.

    The value in the variable that the pointer points too can be changed or referenced through dereferencing the pointer.

    Dereferencing: Is to obtain the value at the address the pointer is pointing at. A pointer must have an valid address of a variable of the same type to be dereferenced.

    --------------------------------------------------------------------------------------------------------------

    I don't want to move on until I am sure I have a solid understand of exactly what I have learned up until now. I can see if I don't get this beginning right I will encounter problems later understanding more complex ideas.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    All variables hold values.
    All variables hold random values unless specifically initialized.*
    Pointers hold memory locations as their values.
    Pointers can access the value of what is at that memory location through dereferencing.

    *Some things are actually initialized by default, such as stat variables.


    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

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