-5

i got this warning " assignment makes pointer from integer without a cast " (MARKED IN CODE PIECE) the code works fine, what i'm doing wrong and how can i fix this warning? thanx

 void Read_Keys(char *keys[MAX_LEN])  //Read the keys from input file 
{
    char c,Fname[MAX_LEN];

    gets(Fname);
    FILE *fptr = (fopen(Fname, "r")); // Input stream pointer assume

    if(fptr == NULL)
    {
        printf("No Such File...");
        exit(EXIT_FAILURE);
    }
    if(fptr) //if not empty get in
    {
        int i = 0;
        while((c = getc(fptr)) != EOF) //while loop copies each char from file
        {                             //to keys array
**          keys[i] = c; //          **                      WARNING IS HERE
            i++;
        }
        keys[i+1] = END; //ending point assume
    }
    fclose(fptr); //Close file for security issues
} ```
Alwaysblue
  • 9,948
  • 38
  • 121
  • 210
AB1
  • 69
  • 7
  • Never ***ever*** use `gets`. It's a [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) function that have even been removed from the C standard. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude Oct 31 '19 at 11:26
  • Like the warning says, you are trying to write a single character to a pointer. The code cannot possibly work fine. `c` shouldn't even be `char` but `int`. – Lundin Oct 31 '19 at 11:26
  • Also note that [`getc`](https://en.cppreference.com/w/c/io/fgetc) return an **`int`** value, which is very crucial for that comparison to the `int` value `EOF`. – Some programmer dude Oct 31 '19 at 11:26
  • 1
    There is absolutely no way that this code works fine. You have just been lucky. – klutt Oct 31 '19 at 11:27
  • 1
    Lastly about your problem, is `keys` really supposed to be an *array of pointers* to `char` (i.e. an array of strings)? How do you use this function? What is it supposed to do? What is `END`? Please try to create a [mcve] to show us how this function works in context. – Some programmer dude Oct 31 '19 at 11:27
  • How do you use/call this function, passing what and how is the latter defined. – alk Oct 31 '19 at 11:56
  • Welcome to Stackoverflow, If the problem is answered/solved mark the answer if necessary. Please, Don't change the tile of the question to **solved, thanks for answering**. For people having the same problem, it's impossible for them to find this question by reading such headings. I have reverted the question back to what it originally was – Alwaysblue Oct 31 '19 at 12:57

2 Answers2

1

The parameter keys is declared like

char *keys[MAX_LEN]

the compiler adjusts it to the following declaration

char **keys

So in this statement

keys[i] = c;

the left operand has the type char * that is it is a pointer while the right operand has the type char.

So the compiler issues a warning because this assignment does not make sense.

I suspect that in any case the parameter is declared incorrectly. It seems you mean the following declaration

void Read_Keys(char ( *keys )[MAX_LEN]);

that is you are trying to pass a two dimensional array to the function. But in any case this code snippet

    int i = 0;
    while((c = getc(fptr)) != EOF) //while loop copies each char from file
    {                             //to keys array
        keys[i] = c; //          **                      WARNING IS HERE
        i++;
    }
    keys[i+1] = END; //ending point assume
}

is invalid because it trues to write all the file in one record instead of an array of records.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

You keys parameter is an array of MAX_LEN pointers to char. So, if you want to assign a value inside this array, it should be a pointer to a character type. Here, getc() returns a character, not a pointer.

I think what you expect is void Read_Keys(char *keys), with the following calling code:

char keys[MAX_LEN];

Read_Keys(keys);

Thus, your keys array is decayed into a char * pointer when Read_Keys is called. Inside Read_Keys, you can access all your array elements using an index, like keys[2].

Obviously, you also need to pass you array length to Read_Keys, so the function knows which array index is valid and which one is not.

Quentin
  • 724
  • 7
  • 16