Thread: help.. confusion.. passing multi-array to pointer (incompatable pointer type)

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    3

    help.. confusion.. passing multi-array to pointer (incompatable pointer type)

    Hi im new..

    I am trying to create a program that is able to write to a file...

    at first i started like code below

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    void wrecords(FILE *fp);
    void fpfs(FILE *fp, char prom[], char str[], int choice);
    void rrecord(FILE *fp);
    
    
    
    int main(int argc, char *argv[])
    {
    	FILE *fp;
    	int option;
    	fp = fopen("file.cvs", "a+");
    	
    	printf("Write and Read File Experment\n\n");
    	
    	for(;;)	{
    	option = 0;
    	
    	printf("1: Write Records\n");
    	printf("2: Read Records\n");
    	printf("3: Exit Program\n\n");
    	
    		while(option == 0) {
    			printf("Please Choose Option: ");
    			scanf("%d", &option);
    			if(isdigit(option))
    				printf("Error Number Wasn't Entered\n");
    			else if(option < 1 || option > 3)
    				printf("invalid choice\n");
    		}
    		
    	switch(option){
    		case 1:
    			wrecords(fp);
    			break;
    		case 2:
    			rrecord(fp);
    			break;
    		}
    		if(option == 3)
    			break;
    	}
    	
    
    	fclose(fp);
    	printf("File has been closed, end of program\n");
    	getchar();
    }
    
    void wrecords(FILE *fp)
    {
    	int id = 0;
    	char firstname[10][50];
    	char lastname[10][50];
    
    	if(fp == NULL){
    		printf("Error opening file\n");
    		exit(1);
    		}
    		
    	
    		
    	for(id = 0; id < 5; id++)	{
    		printf("Record #%d\n", id+1);
    		fprintf(fp, "%d,", id);
    		printf("Firstname: ");
    		scanf("%s", firstname[id]);
    		fprintf(fp, "%s,", firstname[id]);
    		printf("Lastname: ");
    		scanf("%s", lastname[id]);
    		fprintf(fp, "%s;\n", lastname[id]);
    	}
    
    	
    	for(id = 5; id < 10; id++){
    		printf("Record #%d\n", id+1);
    		fprintf(fp, "%d,", id);
    		fpfs(fp, "Firstname: ", firstname[id], 1);
    		fpfs(fp, "Secondname: ", lastname[id], 2);
    	}
    }
    
    void fpfs(FILE *fp, char prom[], char str[], int choice)
    {
    	printf("%s", prom);
    	scanf("%s", str);
    	
    	fprintf(fp, "%s", str);
    	
    	if(choice == 1)
    		fprintf(fp, ",");
    	else if(choice == 2)
    		fprintf(fp, ";\n");
    }
    
    void rrecord(FILE *fp)
    {
    	if(fp == NULL){
    		printf("Error opening file\n");
    		exit(1);
    		}
    		
    	printf("Function is not yet made\n");
    }
    the above code worked then i decided to put the firstname[][], lastname[][], in the main for making a read file function for the previous records added, and i wanted a pointer to the array for editiing in a function(im crap at explaining but im sure you see what i mean looking at the code)

    but now im getting:
    file.c: In function `main':
    file.c:45: warning: passing arg 2 of `wrecords' from incompatible pointer type
    file.c:45: warning: passing arg 3 of `wrecords' from incompatible pointer type
    file.c: In function `wrecords':
    file.c:89: warning: passing arg 3 of `fpfs' from incompatible pointer type
    file.c:90: warning: passing arg 3 of `fpfs' from incompatible pointer type

    here the new code that generated the errors above:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    void wrecords(FILE *fp, char *firstname[], char *lastname[], int *id);
    void fpfs(FILE *fp, char prom[], char *str[], /*int id, */int choice);
    
    int main(int argc, char *argv[])
    {
    	FILE *fp;
    	int id = 0;
    	int recordn[100];
    	char firstname[100][50];
    	char lastname[100][50];
    	int option;
    	fp = fopen("file.cvs", "a+");
    	
    	printf("Write and Read File Experment\n\n");
    	
    	for(;;)	{
    	option = 0;
    	
    	printf("1: Write Records\n");
    	printf("2: Read Records\n");
    	printf("3: Exit Program\n\n");
    	
    		while(option == 0) {
    			printf("Please Choose Option: ");
    			scanf("%d", &option);
    			if(isdigit(option))
    				printf("Error Number Wasn't Entered\n");
    			else if(option < 1 || option > 3)
    				printf("invalid choice\n");
    		}
    		
    	switch(option){
    		case 1:
    			wrecords(fp, firstname[id], lastname[id], &id);
    			break;
    		case 2:
    			//rrecord(recordn[1], firstname[1], lastname[1]);
    			break;
    		}
    		if(option == 3)
    			break;
    	}
    	
    
    	fclose(fp);
    	printf("File has been closed, end of program\n");
    	getchar();
    }
    
    void wrecords(FILE *fp, char *firstname[], char *lastname[], int *i)
    {
    	int id;
    	id = *i;
    
    
    	if(fp == NULL){
    		printf("Error opening file\n");
    		exit(1);
    		}
    		
    	
    		
    	for(id = 0; id < 5; id++)	{
    		printf("Record #%d\n", id+1);
    		fprintf(fp, "%d,", id);
    		printf("Firstname: ");
    		scanf("%s", firstname[id]);
    		fprintf(fp, "%s,", firstname[id]);
    		printf("Lastname: ");
    		scanf("%s", lastname[id]);
    		fprintf(fp, "%s;\n", lastname[id]);
    	}
    
    	
    	for(id = 5; id < 10; id++){
    		printf("Record #%d\n", id+1);
    		fprintf(fp, "%d,", id);
    		fpfs(fp, "Firstname: ", firstname[id], 1);
    		fpfs(fp, "Secondname: ", lastname[id], 2);
    	}
    	
    	*i = id;
    }
    
    void fpfs(FILE *fp, char prom[], char *str[], /*int id, */int choice)
    {
    	printf("%s", prom);
    	scanf("%s", str);
    	
    	fprintf(fp, "%s", str);
    	
    	if(choice == 1)
    		fprintf(fp, ",");
    	else if(choice == 2)
    		fprintf(fp, ";\n");
    }
    /*
    void rrecord()
    {	
    	printf("Function is not yet made\n");
    }
    */
    its started to confuse me ive tried changing stuff also on how i set up the array like i have to i need to use the & when assigning a pointer to the array or does "the name of the array is a pointer to first element in array" rule apply, can any one help?

    thanks in advance.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    the type of firstname[id]
    is char[50]
    which is converted to

    char*

    if you want to pass array of strings you should drop [id] part in main

    in this case argumant of the function should be

    char names[][50]
    not
    char* names[]

    these types are different
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  3. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 11:01 AM
  4. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  5. Passing a pointer to a 2D array
    By OakRand in forum C Programming
    Replies: 8
    Last Post: 05-18-2006, 12:50 AM