struct struct struct problem....please....help!!!

This is a discussion on struct struct struct problem....please....help!!! within the C Programming forums, part of the General Programming Boards category; I believe i am wrong in the two red points...is it right? also whats the problem? thanks in advance.... Code: ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    81

    struct struct struct problem....please....help!!!

    I believe i am wrong in the two red points...is it right? also whats the problem?
    thanks in advance....
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct Info{
    	char name[81];  
    	int phone;    
    	char street[81];
    	char email[81];
    };
    
    
    void print(struct Info info.name, struct Info info.phone, struct Info info.street, struct Info info.email){
    
    			printf("Name: %s\n", info.name);
    			printf("Phone Number: %d\n", info.phone);
    			printf("Street address: %s\n", info.street);
    			printf("E-mail: %s\n", info.email);
    }
    
    
    int main(void){
    	FILE *infile;
    	struct Info info;
    	
    		infile = fopen("file.txt","r");
    
    		if(infile==NULL){
    			printf("Error :(\n");
    			exit(1);
    		}
    	
    		while(fscanf(infile,"%s %d %s %s", info.name, &info.phone, info.street, info.email)!=EOF){
    			print(info.name, info.phone, info.street, info.email);	
    		}
    	
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Try something simpler:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct Info{
    	char name[81];  
    	int phone;    
    	char street[81];
    	char email[81];
    };
    
    
    void print(struct Info info){
    
    			printf("Name: %s\n", info.name);
    			printf("Phone Number: %d\n", info.phone);
    			printf("Street address: %s\n", info.street);
    			printf("E-mail: %s\n", info.email);
    }
    
    
    int main(void){
    	FILE *infile;
    	struct Info info = {"Buzz Lightyear", 12345, "Infinity Way", "bl@tomorrow.com" };
    	
    		/*infile = fopen("file.txt","r");
    
    		if(infile==NULL){
    			printf("Error :(\n");
    			exit(1);
    		}
    	
    		while(fscanf(infile,"%s %d %s %s", info.name, &info.phone, info.street, info.email)!=EOF){
        */
    			print(info);	
    		//}
    	
    	return 0;
    }

  3. #3
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    983
    The way you're trying to do things more or less defeats the purpose of a struct. Why would you bother putting it all together if you always had to deal with the struct members individually? It'd be useful as a sort of namespace, but that's about it.

    Thankfully, C's not like that:
    Code:
    #include <stdio.h>
    struct info
    {
      int a, b, c;
    };
    void f(struct info info)
    {
      printf("%d %d %d\n", info.a, info.b, info.c);
    }
    int main(void)
    {
      struct info info = { 10, 20, 30 };
      f(info);
      return 0;
    }
    You can just pass a struct around; you needn't try to pass each individual member.

    You'll probably want to pass a pointer around instead of the struct itself (this becomes more relevant the larger your struct gets):
    Code:
    #include <stdio.h>
    struct info
    {
      int a, b, c;
    };
    void f(struct info *info)
    {
      printf("%d %d %d\n", info->a, info->b, info->c);
    }
    int main(void)
    {
      struct info info = { 10, 20, 30 };
      f(&info);
    }
    x->y is the same as (*x).y; that is, dereference a struct (or union) pointer and then access a member.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    245
    void print(struct Info info.name, struct Info info.phone, struct Info info.street, struct Info info.email){


    You're passing in info.name, which is a char array
    it should be
    Code:
    void print(char[] name, .......){
       printf(name......);
       ....
       ...
    }
    you're don't understand how to pass parameters? it's the same as in Java or any other language

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    81
    Quote Originally Posted by dayalsoap View Post
    void print(struct Info info.name, struct Info info.phone, struct Info info.street, struct Info info.email){


    You're passing in info.name, which is a char array
    it should be
    Code:
    void print(char[] name, .......){
       printf(name......);
       ....
       ...
    }
    you're don't understand how to pass parameters? it's the same as in Java or any other language
    you are right. i don' t...C is my first language and that is why i make these mistakes...i don' t know if C is a bit difficult but i don' t know another language yet!

  6. #6
    Registered User jimtuv's Avatar
    Join Date
    May 2010
    Location
    Sylvania, Ohio
    Posts
    94
    Don't feel bad passing function parameters can be a bit tricky. I am new as well and have to really think carefully what is being passed and how. I found this link to be helpful

    Passing Arguments to Function

    Also there is a really good set of videos for beginning C that goes through parameter passing functions and structs (this is where I learned if from) you can find it here:

    CSE 142 TVI - Lectures, Autumn 2000

    If you use the url the slides are synced to the video lecture.

    I hope this will help you out some.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 07:38 PM
  2. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  3. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  4. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  5. Replies: 16
    Last Post: 10-29-2006, 04:04 AM

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