#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    20
    Rep Power
    0

    Two-dimensional array and functions


    So I am having this issue trying to move a list to have it organized. I'm sure there are errors in this but I'm not much worried about it because I'm still working to debug this. however I'm trying to comprehend an error I'm receiving. I've been googling and searching for some time now and I've found no conclusion or explanation. If anyone can help I'd greatly appreciate it.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    void insertSort(char **list, int len); //ERROR HERE
    int findNextLowest(int beg, int len, char **list);
    void switchChar(int indextoswitch, int currentindex, char **list);
    
    
    int main(){
    	printf("Enter file to alphabetize: \n");
    	//char loc[100];
    	//This is turned off for now location
    	//will be /tester/words.txt
    	//scanf("%s", loc);
    	FILE *fp = fopen("/tester/words.txt", "r"); //replace words.txt with loc
    	char info[50][100];
    	char temp[100];
    	int index = 0;
    	
    	while((fscanf(fp, "%s", temp)) != EOF){
    		//CODE
    		strcpy(info[index], temp);
    		index++;
    	}
    	int ndx = index;
    	int len = index;
    	index = 0;
    	//still prints a blank char
    	while(ndx >=0){
    		printf("Here is the %d th word from the list: %s\n", index, info[index]);
    		index++;
    		ndx--;
    	}
    	
    	insertSort(info, len); //ERROR HERE
    	//end all
    	return 0;
    }
    
    void insertSort(char **list, int len){
    	int index = 0;
    	int lowest;
    	while(index < len){
    		lowest = findNextLowest(index, len, list);
    		switchChar(lowest, index, list);
    		index++;
    	}
    }
    
    int findNextLowest(int beg, int len, char **list){
    	//CODE
    	char temp[100];
    	strcpy(temp, list[beg]);
    	int com;
    	int remember;
    	//int index = beg;
    	while(beg <= len){
    		//CODE
    		com = strcmp(temp, list[beg]);
    		if(com <= 0){
    			strcpy(temp, list[beg]);
    			remember = beg;
    		}
    		beg++;
    	}
    	return remember;
    }
    
    void switchChar(int indextoswitch, int currentindex, char **list){
    	//CODE
    	char temp[100];
    	strcpy(temp, list[currentindex]);
    	strcpy(list[currentindex], list[indextoswitch]);
    	strcpy(list[indextoswitch], temp);
    }
    First error is line 5 (insertSort) second is line 36 but both are due to line 36.

    ERROR 36:
    warning: passing argument 1 of 'insertSort' from incompatable pointer type

    ERROR 5:
    note: expected 'char **' but argument is of type 'char (*)[100]'

    Im guessing i've passed it wrong to the function but ive tried multiple ways.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    The "array becomes a pointer" is NOT recursive. That is, [][] does not become **.

    So given char info[50][100]; the function prototype could be written as

    void insertSort(char (*list)[100], int len);
    void insertSort(char list[][100], int len);
    void insertSort(char list[50][100], int len);

    Notice the last one can simply be a copy/paste of the variable you want to pass into the function.
    Notice the first one is in the syntax of the error message
    note: expected 'char **' but argument is of type 'char (*)[100]'

    All three forms are equivalent - just pick one which you feel comfortable with. Satisfy yourself that they are equivalent by trying the different forms in this printarray function.

    Inside the function, normal array syntax to access the passes array works.
    Code:
    void printarray ( char list[50][100], int len ) {
      for ( i = 0 ; i < len ; i++ ) {
        printf("%s\n", list[i] );
      }
    }
    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

IMN logo majestic logo threadwatch logo seochat tools logo