Originally Posted by
Sean McCarthy
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