Thread: Pointer

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    Unhappy Pointer

    Hi everyone, I am still having a few small problems understanding some of the rules of C. I've successfully converted a program from C++ to C with the help of a book. Unfortunately there is one part that I do not understand. i understand the concept somewhat but does not make much sense. For example

    [code]
    if ((*Group).Packs.Climb.Num > 4)
    {
    (*Group).Packs.Climb.Discount = (*Group).Packs.Climb.Charges * .10;
    (*Group).Packs.Climb.Charges -= (*Group).Packs.Climb.Discount;
    }
    else
    (*Group).Packs.Climb.Discount = 0;
    [\code]

    My question is why do I need the paranthesis around pointer group. In C++ I would not need the paranthesis or the pointer but I tried to write it in c using -> but received an error. The way I saw it written in the book was written with the paranthesis but I do not want to complete the program before I completely understand why? I receive an illegal inderection error when I use *Group->Packs.Climb.Charges * .10; or if I remove the paranthesis. Please let me know if it is something simple of if there is a book that goes deep into C programming so that I can understand this better


    **I've included more code below just in case you need to see it**

    Code:
    #include<iostream.h>
    #include <iomanip.h>
    #include<stdio.h>
    
    //The data structures
    
    struct Package1				  //Climbing package
    {
    	int Num;					  //Number in party 
    	int Beginners;				  //Those needing instructions 
    	int Advanced;				  //Those not needing instructions 
    	int NeedEquip;				  //Those renting camping equipments 
    	float BaseCharges;		      //Base charges 
    	float Charges;				  //Total charges 
    	float Instruction;		      //Cost of instruction 
    	float Equipment;			  //Cost of equipment rental 
    	float Discount;			      //Discount
    	float Deposit;				  //Required deposit 
    };
    
    struct Package2				  //Scuba package 
    {
    	int Num;					  //Number in party 
    	int Beginners;				  //Those needing instructions 
    	int Advanced;				  //Those not needing instructions 
    	float BaseCharges;		      //Base charges 
    	float Charges;				  //Total charges 
    	float Instruction;		      //Cost of instruction 
    	float Discount;			      //Discount 
    	float Deposit;				  //Required deposit
    };
    
    struct Package3				  //Sky Diving Package 
    {
    	int Num;					  //Number in party 
    	int Lodge1;					  //Number at 1st lodging choice 
    	int Lodge2;					  //Number at 2nd lodging choice 
    	float BaseCharges;		      //Base charges 
    	float Charges;				  //Total charges 
    	float Discount;			      //Discount 
    	float Lodging;				  //Cost of lodging 
    	float Deposit;				  //Required deposit 
    };
    
    struct Package4				  //Spelunking Package 
    {
    	int Num;					  //Number in party 
    	int NeedEquip;				  //Those renting camping equipments 
    	float BaseCharges;		      //Base charges
    	float Charges;				  //Total charges 
    	float Equipment;			  //Cost of equipment rental 
    	float Discount;			      //Discount 
    	float Deposit;				  //Required deposit 
    };
    
    union Pack						  //Combines all four structs 
    {
      struct Package1 Climb;
      struct Package2 Scuba;
      struct Package3 Sky;
      struct Package4 Spel;
    };
    
    struct Reservation
    {
    	int PackNum;				   //Indicates which package is stored 
        union Pack Packs;			   //All Structs are combined into union Pack and passed on to Packs
    };
    
    //Constants for the charges 
    const float ClimbRate = 350.0;			//Base rate - Devil's Courthouse 
    const float ScubaRate = 1000.0;			//Base Rate - Bahamas 
    const float SkyDiveRate = 400.0;		//Base Rate - Sky diving 
    const float CaveRate = 700.0;			//Base Rate - Spelunking 
    const float ClimbInstruct = 100.0;		//Climbing instructions 
    const float ScubaInstruct = 100.0;		//Scuba instructions 
    const float DailyCampRental = 40.0;		//Daily camping equip. rental 
    const float DayLodge1 = 65.0f;			//Lodging option (sky diving)
    const float DayLodge2 = 120.0f;			//Lodging option (sky diving)
    
    //Function prototypes 
    void Climbing(struct Reservation*);
    void Scuba(struct Reservation*);
    void SkyDive(struct Reservation*);
    void Spelunk(struct Reservation*);
    int Menu(void);
    void DisplayInfo(const struct Reservation*);
    void DisplayPack1(const struct Reservation*);
    void DisplayPack2(const struct Reservation*);
    void DisplayPack3(const struct Reservation*);
    void DisplayPack4(const struct Reservation*);
    
    
    //Main function 
    main()
    {
    	int Selection;
    	struct Reservation Group;
       
    	do
    	{
    		Selection = Menu();
    		switch (Selection)
    		{
    		case 1: Climbing(& Group);
    				break;
    		case 2: Scuba(& Group);
    				break;
    		case 3: SkyDive(& Group);
    				break;
    		case 4: Spelunk(& Group);
    				break;
          case 5: printf("Exiting program.\n\n");  //(cout had been changed to printf for c conversion)
    		}
    
    		if (Selection <5)
            DisplayInfo(& Group);
    	} while (Selection != 5);
    
    	return 0;
    }
    
    //Definition of function Menu.
    //Displays the main menu and asks the user to select an option.
    //Returns an integer in the range 1 - 5. 
    
    int Menu(void)
    {
    	int Choice;
    	do
    	{
          printf("\nHigh Adventure Travel Agency\n");
          printf("------------------------------\n");
          printf("1) Devil's Courthouse Adventure Weekend\n");
          printf("2) Scuba Bahama\n");
          printf("3) Sky Dive Colorado\n");
          printf("4) Barron Cliff Spelunk\n");
          printf("5) Exit program\n\n");
          printf("Enter 1, 2, 3, 4, or 5: ");
    		scanf("%d", &Choice);                   //(cin has been changed to scanf for c conversion)
    
    		if (Choice < 1 || Choice > 5)           //If choice is less than 1 or greater than 5
          printf("Invalid Selection\n");            //Cout/printf "Invalid Selection
    
    	} while (Choice < 1 || Choice > 5);          //?????????????
    
    	return Choice;
    }
    
    //Definition of Climbing function.
    // Uses a Reservation reference parameter to hold the vacation
    // package information. This function calculates the charges for
    // the Devil's Courthouse Adventure Weekend package.  
    
    void Climbing(struct Reservation *Group)
    {
       (*Group).PackNum= 1;      
    
       printf("\nDevil's Courthouse Adventure Weekend\n");
    	printf("--------------------------------------\n");
    	printf("How many will be going who need an instructor? ");
    	scanf("%d", &(*Group).Packs.Climb.Beginners);
    	printf("How many advanced climbers will be going? ");
    	scanf("%d", &(*Group).Packs.Climb.Advanced);
    
       (*Group).Packs.Climb.Num =(*Group).Packs.Climb.Beginners + (*Group).Packs.Climb.Advanced;
    
    	printf("How many will be rent camping equipment? ");
       scanf("%d", &(*Group).Packs.Climb.NeedEquip);
    
    	//Calculate base charges 
    	 (*Group).Packs.Climb.BaseCharges = (*Group).Packs.Climb.Num * ClimbRate;
    	 (*Group).Packs.Climb.Charges = (*Group).Packs.Climb.BaseCharges;
    
    	//Calculate 10% discount for 5 or more 
    	if ((*Group).Packs.Climb.Num > 4)
    	{
    		 (*Group).Packs.Climb.Discount = (*Group).Packs.Climb.Charges * .10;
    		 (*Group).Packs.Climb.Charges -= (*Group).Packs.Climb.Discount;
    	}
    	else
    		 (*Group).Packs.Climb.Discount = 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You could start by editing your post to make it much more presentable.
    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.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >My question is why do I need the paranthesis around pointer group.

    Precedence. When referencing a member of a struct or union via a pointer, it is more common to use the -> operator.
    Code:
    #include <stdio.h>
    
    struct sType
    {
       long member;
       double trouble;
    };
    
    void foo(const struct sType *ptr)
    {
       printf("member = %ld = %ld\n", ptr->member,  (*ptr).member);
       printf("trouble = %g = %g\n",  ptr->trouble, (*ptr).trouble);
    }
    
    int main(void)
    {
       struct sType object = {42,3.1415926535897932384626433832795};
       foo(&object);
       return 0;
    }
    
    /* my output
    member = 42 = 42
    trouble = 3.14159 = 3.14159
    */
    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. 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