how do you put struct pointers into functions?

This is a discussion on how do you put struct pointers into functions? within the C Programming forums, part of the General Programming Boards category; This is my struct Code: typedef struct{ char first[20]; char last[20]; int nums[6]; }RECORD; this is my function prototype Code: ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    19

    how do you put struct pointers into functions?

    This is my struct

    Code:
    typedef struct{
    char first[20];
    char last[20];
    int nums[6];
    }RECORD;
    this is my function prototype

    Code:
    void writeRecord (RECORD* , FILE*, int);
    this is my functions

    Code:
    void writeRecord (RECORD* people, FILE* fp, int num_people){
    ...................
    }
    in my main i put
    Code:
    RECORD* people;
    
    people = (RECORD*) malloc(num_people*sizeof (RECORD));
    
    writeRecord(people, fp, num_people);
    can you guys tell me where i'm going wrong.

    these are the errors i get in microsoft visual c++ 2008

    these all point to the prototype
    syntax error: ')'
    syntax error: ','
    syntax error: missing ')' before '*'
    syntax error: missing '{' before '*'

    these point to the function
    writeRecord: redifinition; different basic types

  2. #2
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Cool

    Quote Originally Posted by SORAKH2756 View Post
    This is my struct

    Code:
    typedef struct{
    char first[20];
    char last[20];
    int nums[6];
    }RECORD;
    this is my function prototype

    Code:
    void writeRecord (RECORD* , FILE*, int);
    this is my functions

    Code:
    void writeRecord (RECORD* people, FILE* fp, int num_people){
    ...................
    }
    in my main i put
    Code:
    RECORD* people;
    
    people = (RECORD*) malloc(num_people*sizeof (RECORD));
    
    writeRecord(people, fp, num_people);
    can you guys tell me where i'm going wrong.

    these are the errors i get in microsoft visual c++ 2008

    these all point to the prototype
    syntax error: ')'
    syntax error: ','
    syntax error: missing ')' before '*'
    syntax error: missing '{' before '*'

    these point to the function
    writeRecord: redifinition; different basic types
    Did you define fp (FILE *fp) somewhere in main()?? It's hard to tell when your program
    is in snips with the errors you are getting. Can you post everything?

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    19
    sure can
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    //function prototype
    void writeRecord (RECORD* , FILE*, int)
    
    //Enumarated winnings
    
    enum Scenario{
    
    	three = 10,
    	four = 100,
    	five = 10000,
    	six = 1000000
    };
    
    //Record struct
    typedef struct{
    	char first[20];
    	char last[20];
    	int nums[6];
    }RECORD;
    
    main(){
    	FILE* fp;
    	int num_people,i,WinNums[6] = {0};
    	char file_name[20];
    	RECORD* people;
    
    	//asks and opens the file the user puts in
    
    	printf("Enter the name of the file with the ticket data.\n");
    	scanf("%s",file_name);
    
    	fp = fopen(file_name,"r");
    
    	if(fp == NULL){
    		printf("Can't find the file you have entered");
    		system("pause");
    		exit(-1);
    	}
    
    	// Reads the amount of people in lottery and allocates memory
    
    	fscanf(fp,"%i",&num_people);
    
    	people = (RECORD*) malloc(num_people*sizeof (RECORD));
    
    
    	// Start to put information about people in the Record array people
    
    	writeRecord(people, fp, num_people);
    
    
    	//Asks user for the winning numbers and error check
    
    	printf("Enter the winning Lottery numbers\n");
    	scanf("%i %i %i %i %i %i",&WinNums[0],&WinNums[1],&WinNums[2],&WinNums[3],&WinNums[4],&WinNums[5]);
    
    	while(WinNums[0] > WinNums[1] || WinNums[1] > WinNums[2]|| WinNums[2] > WinNums[3] || WinNums[3] > WinNums[4]||WinNums[4] > WinNums[5]){
    		printf("you entered numbers in non ascending order, try again.");
    		printf("Enter the winning Lottery numbers\n");
    		scanf("%i %i %i %i %i %i",&WinNums[0],&WinNums[1],&WinNums[2],&WinNums[3],&WinNums[4],&WinNums[5]);
    	}
    
    	//
    	fclose(fp);
    	system("pause");
    }
    
    
    void writeRecord ( RECORD* people,FILE* fp,int num_people){
    	int i;
    	for(i=0; i<num_people;i++){
    		fscanf(fp,"%s %s %i %i %i %i %i %i",people[i].last,people[i].first,people[i].nums[0],people[i].nums[1],people[i].nums[2],people[i].nums[3],people[i].nums[4],people[i].nums[5]);
    	}
    }

  4. #4
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Code:
    void writeRecord (RECORD* , FILE*, int);
    you forgot the ";" after your function prototype.

    Happens to me all the time. :-)

    I'll check for more erors...

    Edit:
    #include <stdlib.h>
    #include <stdio.h>

    //function prototype
    void writeRecord (RECORD* , FILE*, int)
    Edit#2: but I see the ";" in your first code snips... I am assuming that you entire code is what you
    actually have.
    Last edited by Char*Pntr; 09-03-2010 at 08:04 PM.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    19
    wow you're right. I also figured out that I needed to put the struct above the prototype.

    Now what i'm having trouble with is this part
    Code:
    void writeRecord ( RECORD* people,FILE* fp,int num_people){
    	int i;
    	for(i=0; i<num_people;i++){
    		fscanf(fp,"%s %s %i %i %i %i %i %i",people[i].last,people[i].first,people[i].nums[0],people[i].nums[1],people[i].nums[2],people[i].nums[3],people[i].nums[4],people[i].nums[5]);
    	}
    }
    it's reading the name from the file, but it won't read the integers :[

  6. #6
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Wink

    wow you're right. I also figured out that I needed to put the struct above the prototype.
    lmao, I don't get that very often on here! I'm sure there will be *someone* that
    will argue with the merits of my previous suggestion, putting a ";" after your
    function prototype.

    ok, easy stuff done... will take some time to look at the rest.

  7. #7
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Smile

    //function prototype
    void writeRecord (RECORD* , FILE*, int)

    You're way ahead of me.. but I think you need the keyword "STRUCT) inside
    your function arguments, like this....

    Code:
    void writeRecord (struct RECORD* , FILE*, int);
    Let me know if I'm wrong.
    Last edited by Char*Pntr; 09-03-2010 at 08:29 PM. Reason: Deleted something that was wrong.

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    19
    lol yea i think you are wrong, created a lot of errors

  9. #9
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by SORAKH2756 View Post
    lol yea i think you are wrong, created a lot of errors
    I did some editing since my last post, read the last one again. I only put "struct" in the
    function prototype, but not in the function's arguments.

    Anyway I got this puppy to compile & run, I've created a .txt file....

    Can you post your winning.txt so that I can test the program?

  10. #10
    Registered User
    Join Date
    Sep 2010
    Posts
    19
    5
    Llewellyn Mark
    1 15 19 26 33 46
    Young Brian
    17 19 33 34 46 47
    Cazalas Jonathan
    1 4 9 16 25 36
    Siu Max
    17 19 34 46 47 48
    Balci Murat
    5 10 17 19 34 47

    save that as a input.txt

  11. #11
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    int WinNums[6] = {0};


    Can I ask what you're doing here? I see a 6 element of type int declared,
    but I get lost on the initialize part. Shouldn't it be like:

    Code:
    int WinNums[6] = {0,0,0,0,0,0};
    and then placed above main()?

  12. #12
    Registered User
    Join Date
    Sep 2010
    Posts
    19
    just by initializing it once, it automatically gives it to all of them.
    btw i got passed a lot of the coding. now i'm comparing values from the user input and the txt files =P

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I don't want to correct your homework .... but here's one hint you might want to pick up on...

    Always write your code so that your Main or WinMain functions are at the bottom of the page, not the top. That way you have an organization that will ensure things are known about before they are called... and it makes for much tidier code too. You won't need function prototypes in this structure because everything is defined before it's called. And... most importantly, you will find your code is a lot easier to debug when working from a bottom up structure.

  14. #14
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    OK, I'm exhausted. One final note, is you use scanf() i.e. reading the file name...
    I would change that to gets()... before gets() I wrote another function called "void clear_kb(void)
    that should be used before each scanf() if you choose to use scanf().

    Also, I'm gravely concerned with my suggestion to add "struct" in the function prototype. I thought function prototypes & function definitions, the arguments should match perfectly.

    Also, today I just finished the chapter on file operations... so I'm not strong there. Haven't covered "enum" types yet.

  15. #15
    Registered User
    Join Date
    Sep 2010
    Posts
    19
    i see, well this is the code i got now

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    //Record Struct
    typedef struct{
    	char first[20];
    	char last[20];
    	int numbers[6];
    }RECORD;
    
    //function prototypes
    void writeRecord ( RECORD* , FILE*, int);
    void Winners (RECORD*,int,int);
    
    //Enumarated winnings
    enum Scenario{
    
    	three = 10,
    	four = 100,
    	five = 10000,
    	six = 1000000
    };
    
    
    main(){
    	FILE* fp;
    	int num_people,i,WinNums[6] = {0};
    	char file_name[20];
    	RECORD* people;
    
    	//asks and opens the file the user puts in
    
    	printf("Enter the name of the file with the ticket data.\n");
    	scanf("%s",file_name);
    
    	fp = fopen(file_name,"r");
    
    	if(fp == NULL){
    		printf("Can't find the file you have entered");
    		system("pause");
    		exit(-1);
    	}
    
    	// Reads the amount of people in lottery and allocates memory
    
    	fscanf(fp,"%i",&num_people);
    
    	people = (RECORD*) malloc(num_people*sizeof (RECORD));
    
    
    	// Start to put information about people in the Record array people
    
    	writeRecord(people, fp, num_people);
    
    
    	//Asks user for the winning numbers and error check
    
    	printf("Enter the winning Lottery numbers\n");
    	scanf("%i %i %i %i %i %i",&WinNums[0],&WinNums[1],&WinNums[2],&WinNums[3],&WinNums[4],&WinNums[5]);
    
    	while(WinNums[0] > WinNums[1] || WinNums[1] > WinNums[2]|| WinNums[2] > WinNums[3] || WinNums[3] > WinNums[4]||WinNums[4] > WinNums[5]){
    		printf("you entered numbers in non ascending order, try again.\n");
    		printf("Enter the winning Lottery numbers\n");
    		scanf("%i %i %i %i %i %i",&WinNums[0],&WinNums[1],&WinNums[2],&WinNums[3],&WinNums[4],&WinNums[5]);
    	}
    
    	//Checks amount of numbers matched and outputs winners
    	Winners(people,num_people,WinNums);
    
    	//closes file and free's up the memory allocated
    	fclose(fp);
    	free(people);
    
    	system("pause");
    }
    
    
    void writeRecord (RECORD* people,FILE* fp,int num_people){
    	int i;
    
    	//scanning the file and encoding the data into the struct
    
    	for(i=0; i<num_people;i++){
    		fscanf(fp,"%s %s",people[i].last,people[i].first);
    		fscanf(fp,"%i",people[i].numbers);
    		fscanf(fp,"%i",people[i].numbers+1);
    		fscanf(fp,"%i",people[i].numbers+2);
    		fscanf(fp,"%i",people[i].numbers+3);
    		fscanf(fp,"%i",people[i].numbers+4);
    		fscanf(fp,"%i",people[i].numbers+5);
    	}
    }
    
    void Winners(RECORD* people,int num_people,int WinNums[]){
    	int i,j,k,num_matched;
    
    	for(i=0; i < num_people; i++){
    		num_matched = 0;
    		for(j=0; j<6; j++){
    			for(k=0; k<6;k++){
    				if(people[i].numbers[j] == WinNums[k]){
    					num_matched++;	
    				}
    
    			}
    			if(num_matched == three){
    
    		}
    	}
    
    }
    as you can see i'm trying to get all the matches for one profile. I know there is an easier code for it but i forgot.
    I also have to use the enum thing which idk how do use :P

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of functions
    By frktons in forum C Programming
    Replies: 29
    Last Post: 06-30-2010, 10:51 AM
  2. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  3. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM

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