Thread: Trouble with string arrays

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    3

    Trouble with string arrays

    I am an amateur programmer, I am trying to take words out of a file, then store them in one array and check for any duplicate words Please Help

    I am currently getting a segmentation fault and have no idea why

    Code:
    
    
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    void function1(char **array[][17]);
    void function2(char **array[][17], int*);
    void function3(char **array[][17], int);
    
    int main( void )
    { 
    
    
    char str_array[20][17];
    int count = 0;
    
    
    function1(str_array);
    
    
    function2(str_array, &count);
    
    
    function3(str_array, count);
    
    
        return ( 0 ) ;
    
    
    }    
    
    
    
    
    function1(char str_array[][17]){
    int i;
    FILE *fp;
    fp = fopen("words.dat","r");
            if(fp==NULL)
                    printf("\tNull\n");
            else
                for(i = 0;i<19;i++){
                fscanf(fp,"%s", &str_array[i][17]);
                    }           
                fclose(fp);
    }
    function2(char str_array[][17], int *count){
    int i;
        for(i = 0;i<18;i++){
            if(strcmp(str_array[i][17],str_array[i+1][17])==0){
                ++*count;
            }
    
    
    }
    }
    function3(char str_array[][17],int count){
    int i;
        for(i = 0;i<19;i++){
            printf("#%s",str_array[i]);
                printf("%d",count);
        }
    }
    Last edited by Sean McCarthy; 11-12-2013 at 01:50 AM. Reason: updated

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Sean McCarthy View Post
    I am currently getting a segmentation fault and have no idea why
    I have an idea - your warning level is too low or you ignore them
    You should not run code that has warnings

    Here is code with fixed indentation only
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void function1(char **array[20][17]);
    void function2(char **array[20][17], int*);
    void function3(char **array[20][17], int);
    
    int main( void )
    { 
        char *str_array[20][17];
        int count = 0;
    
        function1(str_array);
    
        function2(str_array, &count);
    
        function3(str_array, count);
    
        return ( 0 ) ;
    } 
    
    function1(char str_array[20][17]){
        int i;
        FILE *fp;
        fp = fopen("words.dat","r");
        for(i = 0;i<19;i++){
            fscanf(fp,"%s", &str_array[i][17]);
            fclose(fp); 
        }
    }
    function2(char str_array[20][17], int *count){
        int i;
        for(i = 0;i<18;i++){
            if(strcmp(str_array[i][17],str_array[i+1][17])==0){
                ++*count;
            }
    
        }
    }
    function3(char str_array[20][17],int count){
        int i;
        for(i = 0;i<19;i++){
            printf("%s",str_array[i][17]);
            printf("%d",count);
        }
    }
    Here is the VS errors you need to fix before running program
    Code:
    test.c(13): warning C4047: 'function' : 'char **(*)[17]' differs in levels of indirection from 'char *[20][17]'
    test.c(13): warning C4024: 'function1' : different types for formal and actual parameter 1
    test.c(15): warning C4047: 'function' : 'char **(*)[17]' differs in levels of indirection from 'char *[20][17]'
    test.c(15): warning C4024: 'function2' : different types for formal and actual parameter 1
    test.c(17): warning C4047: 'function' : 'char **(*)[17]' differs in levels of indirection from 'char *[20][17]'
    test.c(17): warning C4024: 'function3' : different types for formal and actual parameter 1
    test.c(22): warning C4431: missing type specifier - int assumed. Note: C no longer supports default-int
    test.c(22): error C2371: 'function1' : redefinition; different basic types
       test.c(4) : see declaration of 'function1'
    test.c(31): warning C4431: missing type specifier - int assumed. Note: C no longer supports default-int
    test.c(31): error C2371: 'function2' : redefinition; different basic types
       test.c(5) : see declaration of 'function2'
    test.c(34): warning C4047: 'function' : 'const char *' differs in levels of indirection from 'char'
    test.c(34): warning C4024: 'strcmp' : different types for formal and actual parameter 1
    test.c(34): warning C4024: 'strcmp' : different types for formal and actual parameter 2
    test.c(40): warning C4431: missing type specifier - int assumed. Note: C no longer supports default-int
    test.c(40): error C2371: 'function3' : redefinition; different basic types
       test.c(6) : see declaration of 'function3'
    And same for gcc
    Code:
    gcc -c -o obj/test.o src/test.c -Wall -pedantic -march=core2 -Iinclude -std=c99
    src/test.c: In function ‘main’:
    src/test.c:13:5: warning: passing argument 1 of ‘function1’ from incompatible pointer type [enabled by default]
    src/test.c:4:6: note: expected ‘char ** (*)[17]’ but argument is of type ‘char * (*)[17]’
    src/test.c:15:5: warning: passing argument 1 of ‘function2’ from incompatible pointer type [enabled by default]
    src/test.c:5:6: note: expected ‘char ** (*)[17]’ but argument is of type ‘char * (*)[17]’
    src/test.c:17:5: warning: passing argument 1 of ‘function3’ from incompatible pointer type [enabled by default]
    src/test.c:6:6: note: expected ‘char ** (*)[17]’ but argument is of type ‘char * (*)[17]’
    src/test.c: At top level:
    src/test.c:22:1: warning: return type defaults to ‘int’ [enabled by default]
    src/test.c:22:1: warning: conflicting types for ‘function1’ [enabled by default]
    src/test.c:4:6: note: previous declaration of ‘function1’ was here
    src/test.c:31:1: warning: return type defaults to ‘int’ [enabled by default]
    src/test.c:31:1: warning: conflicting types for ‘function2’ [enabled by default]
    src/test.c:5:6: note: previous declaration of ‘function2’ was here
    src/test.c: In function ‘function2’:
    src/test.c:34:9: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast [enabled by default]
    /usr/include/string.h:143:12: note: expected ‘const char *’ but argument is of type ‘char’
    src/test.c:34:9: warning: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast [enabled by default]
    /usr/include/string.h:143:12: note: expected ‘const char *’ but argument is of type ‘char’
    src/test.c: At top level:
    src/test.c:40:1: warning: return type defaults to ‘int’ [enabled by default]
    src/test.c:40:1: warning: conflicting types for ‘function3’ [enabled by default]
    src/test.c:6:6: note: previous declaration of ‘function3’ was here
    src/test.c: In function ‘function3’:
    src/test.c:43:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
    gcc -o test obj/test.o -Wall -pedantic -march=core2 -Iinclude -std=c99 -lm
    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

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    3
    Thank you, I don't have a good compiler.
    I need to get one it would seem

  4. #4
    Registered User
    Join Date
    Nov 2013
    Posts
    3
    I tried to address all the errors the best I could, if you could give me a tip from this point it would be very appreciated!

    Code:
    
     #include <stdio.h>
     #include <string.h>
    
    
     char function1(char str_array[][17]);
     int function2(char str_array[][17], int*);
     void function3(char str_array[][17], int);
    
    
     int main( void )
     {  
    
    
    
    
        char str_array[20][17];
        int count = 0;
    
    
    
    
        function1(str_array);
    
    
        function2(str_array, &count);
    
    
        function3(str_array, count);
    
    
            return ( 0 ) ;
    
    
     }    
    
    
     char function1(char str_array[][17]){
        int i;
         FILE *fp;
         fp = fopen("words.dat","r");
            if(fp==NULL)
                    printf("\tNull\n");
            else
                    for(i = 0;i<19;i++){
                    fscanf(fp,"%s", &str_array[i]);
                     }           
                fclose(fp);
    }
     int function2(char str_array[][17], int *count){
         int i,f;
            for(f = 0;f<19;f++){
                for(i = 0;i<19;i++){
                if(strcmp(str_array[i],str_array[f])==0){
                ++*count;
                    }
                }
    
    
            }
    }
     void function3(char str_array[][17],int count){
         int i;
         for(i = 0;i<20;i++){
            printf("Word #%d is %s \n", i + 1, str_array[i]);
                printf("%d",count);
        }
    }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    fscanf(fp,"%s", &str_array[i]);
    str_array[i] is already the address of the starting character of your 17-character (inner) array, so no need for an ampersand.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Sean McCarthy View Post
    Thank you, I don't have a good compiler.
    I need to get one it would seem
    It is possible that you have one descent compiler, but you do not how to enable what Vart enabled.
    If you see carefully his post, he compiled the code with
    Code:
    gcc -c -o obj/test.o src/test.c -Wall -pedantic -march=core2 -Iinclude -std=c99
    Notice the -Wall and -pedantic flags. These two can provide you with a variety of useful warnings.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trouble with simple arrays
    By time4f5 in forum C Programming
    Replies: 10
    Last Post: 07-11-2011, 07:09 AM
  2. Trouble with two two dimensional arrays
    By scmurphy64 in forum C Programming
    Replies: 5
    Last Post: 12-06-2009, 06:57 PM
  3. Trouble with Arrays
    By blurx in forum C Programming
    Replies: 4
    Last Post: 10-08-2009, 08:25 PM
  4. trouble assigning arrays
    By shintaro in forum C++ Programming
    Replies: 8
    Last Post: 11-21-2008, 08:31 AM
  5. More trouble passing arrays
    By Rad_Turnip in forum C Programming
    Replies: 2
    Last Post: 04-04-2006, 08:11 PM