Thread: Passing value structure

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    102

    Passing value structure

    How do i pass a value from the main to a function which involved structure? I get always error. Please do help me to correct

    Code:
    #include<stdio.h>
    	
    	void PrintPhoneX (phone x.brand, phone x.model, phone x.price){
    	printf("====Phone x====\n");
     	printf("Brand: %s\n Model: %s\n Price: %d\n",x.brand,x.model,x.price);	
    	}
    	
    	void PrintPhoneY (phone y.brand, phone y.model, phone y.price){
    	printf("====Phone y====\n");
    	printf("Brand: %s\n Model: %s\n Price: %d\n",y.brand,y.model,y.price);	
    	}
    	
    	
    	void PrintPhoneZ (phone z.brand, phone z.model, phone z.price){
    	printf("====Phone z====\n");
        printf("Brand: %s\n Model: %s\n Price: %d\n",z.brand,z.model,z.price);
    	}
    	
    
    struct phone{
    	char brand[10];
    	char model[10];
    	int  price;
    };
    
    int main (){
    	struct phone x;
    	struct phone y;
    	struct phone z;
    	
    	printf("====Phone x====\n");
    	printf("Brand:");
    	scanf("%s",x.brand);
    	printf("Model:");
    	scanf("%s",x.model);
    	printf("Price:");
    	scanf("%d",&x.price);
    	
    	printf("====Phone y====\n");
    	printf("Brand:");
    	scanf("%s",y.brand);
    	printf("Model:");
    	scanf("%s",y.model);
    	printf("Price:");
    	scanf("%d",&y.price);
    	
    		
    	printf("====Phone z====\n");
    	printf("Brand:");
    	scanf("%s",z.brand);
    	printf("Model:");
    	scanf("%s",z.model);
    	printf("Price:");
    	scanf("%d",&z.price);
    	
    	PrintPhoneX (phone x.brand, phone x.model, phone x.price);
    	PrintPhoneY (phone y.brand, phone y.model, phone y.price);
    	PrintPhoneZ (phone z.brand, phone z.model, phone z.price);
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Something closer to this perhaps:
    Code:
    #include<stdio.h>
    
    struct phone{
        char brand[10];
        char model[10];
        int  price;
    };
         
    void PrintPhone (struct phone temp){
        printf("====Phone====\n");
        printf("Brand: %s\n Model: %s\n Price: %d\n",temp.brand,temp.model,temp.price); 
    }
    
    int main (){
        struct phone x;
        struct phone y;
        struct phone z;
        
        ...
         
        PrintPhone(x);
        PrintPhone(y);
        PrintPhone(z);
    }
    The PrintPhone function has a struct phone parameter. This means that it has to know what a phone struct looks like... which means you need to put the definition of the phone struct before the code for the function so the compiler will see it and can understand what a phone struct is.

    Your functions are all somewhat redundant. They all do the same thing with the data passed in so you really only need one function and just have to pass in the different variables x/y/z to that one function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing structure to function
    By danieldcc in forum C Programming
    Replies: 4
    Last Post: 10-10-2011, 11:17 AM
  2. Passing a structure by address
    By Sharke in forum C Programming
    Replies: 2
    Last Post: 03-24-2009, 12:43 AM
  3. passing a structure
    By ckeener in forum C++ Programming
    Replies: 2
    Last Post: 05-11-2005, 08:01 AM
  4. Passing structure to function
    By Sereby in forum C Programming
    Replies: 4
    Last Post: 07-28-2004, 10:05 PM
  5. Passing Structure Pointers
    By mattz in forum C Programming
    Replies: 12
    Last Post: 11-18-2001, 06:23 AM