-4

So to describe my code, my scanCode function reads in from a text file to produce an array called "array" in the main function which is 2D holding strings one, two, three etc until nine. My problem is in the converter function because I want to convert the words corresponding to its actual digits so one to 1, two to 2 in an int array. But there is something wrong with my code which I just cannot find out because the run cmd keeps freezing ! Could debug and tell me what is wrong with my converter function?

 #include <stdio.h>

 char** scanCode()
 {
     FILE *in_file;
     int i = 0;
    static char scan[9054][6];
     in_file = fopen("message.txt", "r");
    while (!feof(in_file))
{

    fscanf(in_file,"%s", scan[i]);
    //printf("%s", scan[i]);
    i++;

}
return scan;
fclose(in_file);
}

int* converter(char** array)
{
int j = 0;
static int his[9053];
int i = 9053;
for (j=0;j<i;j++)
{
    if ((strcmp(array[j], "one")) == 0)

{
    his[j] = 1;
}
    else if ((strcmp(array[j], "two")) == 0) 
{
    his[j] = 2;
}
    else if ((strcmp(array[j], "three")) == 0) 
{
    his[j] = 3;
}
    else if ((strcmp(array[j], "four")) == 0) 
{
    his[j] = 4;
}
    else if ((strcmp(array[j], "five")) == 0) 
{
    his[j] = 5;
}
    else if ((strcmp(array[j], "six")) == 0) 
{
    his[j] = 6;
}
    else if ((strcmp(array[j], "seven")) == 0) 
{
    his[j] = 7;
}
    else if ((strcmp(array[j], "eight")) == 0) 
{
    his[j] = 8;
}
    else if ((strcmp(array[j], "nine")) == 0) 
{
    his[j] = 9;
}
else{
his[j] = 0;
}
printf("%d",his[j]);   
}  
return his;
}

int main(void)
{
int i = 9054;
int d = 0;
int j = 0;
int b = 0;
int a  = 0;
int c = 0;
//int hi[9053];
int final[3018];
int veryfinal[1509];

char ( *array )[6] = scanCode();

int *hi = converter(array);
  while(1);
return 0;
}

Basically it freezes when I run it, I think it has definitely to do something with the converter function but I just don't know what but I get these little warnings

17  1   [Warning] return from incompatible pointer type
85  25  [Warning] initialization from incompatible pointer type
87  24  [Warning] passing argument 1 of 'converter' from incompatible pointer type
21  6   [Note] expected 'char **' but argument is of type 'char (*)[6]'

but obviously it runs despite these warnings and notes

anony
  • 79
  • 1
  • 2
  • 10

1 Answers1

1

In C people often say that pointers and arrays are the same, and in many ways they are, but this is not always the case.

int* converter(char** array)

You are passing this function something that is not actually a char** type. What you are passing is a char *[6] type. Inside the converter function the value passed in is treated as the wrong type of pointer and the math that is done to find the data is therefore wrong. It will interpret the characters {'o','n','e',0} as a pointer rather than an array (well, on a 32 bit machine, on a 64 bit it would also use bytes from the next entry).

nategoose
  • 12,054
  • 27
  • 42