-4

I'm trying to debug my program but I keep getting this error from the compiler any ideas? I searched online and I guess I'm using & in the wrong way

Just a side note (I'm only allowed to use <stdio.h> on this program cuz that's what my professors instructions says)

main.c: In function ‘main’:
main.c:18:16: warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[8]’ [-Wformat=]
   18 |       scanf ("%c", &letters);
      |               ~^   ~~~~~~~~
      |                |   |
      |                |   char (*)[8]
      |                char *
main.c:20:28: error: expected expression before ‘]’ token
   20 |       if (letters == words[][7])
      |                            ^

My program should compare the elements of the first array of characters to each element of the string and if the user typed the word correctly he gets (valid value) if not corrent word he gets (Not valid value.

#include<stdio.h>
int
main ()
{
  char letters[] = { "kamneri" };
    words[][7] =
  {
  "anemik", "kameri", "marine", "minare", "makine", "anemi", "mekan",
      "krema", "kerim", "inmek", "imkan", "imren", "imran", "erkan",
      "ekran", "mera", "krem", "mine", "mira", "name", "ekim", "erik",
      "kim", "nem", "nam", "ani", "kin", "kir"};
    printf("This is the game\n Your letters are (k,a,n,m,e,r,i)");

    for(i = 0 ; i < 7 ; i++)
        {scanf("%s", &letters)
            if(letters == words[][7])           
                printf("valid value");
                else 
                    print("Not valid value");
        }
    return 0;
}
AKRU
  • 1
  • 1
  • `"%c"` scans a single character but it looks like you are passing a pointer to an array of characters. Please add your code to the question so we can see what's going on. You should also read [scanf() leaves the newline character in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-newline-character-in-the-buffer) and probably use `" %c"` as your format string to consume leading whitespace. – Retired Ninja Dec 10 '22 at 19:45
  • Please post your program as well – Sourabh Burse Dec 10 '22 at 19:45
  • `scanf("%s", &letters)` should technically be `scanf("%s", letters)` as `letters` is already a pointer. Taking it's address changes the type from `char *` to `char (*)[8]` (a *pointer-to-array-of* `char[8]`. See [C11 Standard - 6.3.2.1 Other Operands - Lvalues, arrays, and function designators(p3)](http://port70.net/~nsz/c/c11/n1570.html#6.3.2.1p3) for how `letters` is converted to a pointer. Current [C18 Standard - 6.3.2.1 Other Operands - Lvalues, arrays, and function designators(p3)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2596.pdf#subsection.6.3.2) – David C. Rankin Dec 10 '22 at 20:08

1 Answers1

0

As some comment already pointed, the compiler warning tells you that you are not passing the right type to the function call.

Unlike you think, the type of what you pass is char **. Let me explain. The letter is of type char *, that is okay. However, when you &letter, the type becomes char **: the address of a pointer.

In order to stay consistent with the function prototype, we rather want to use letter as is. Or &letter[0].

Regarding the second warning, you do not specify index of word to compare the value of letter to. But that is a detail because you are comparing a pointer to an array char * to a char.

Apart from that, your code has many mistakes:

  • missing ;.
  • invalid comparison of strings.
  • invalid assignments.

The proper code should look like this:

/* `letter` is an array which can handle 7 character + NULL terminating. */
char letters[8];
/* `words` is a list of string which are at most 7 chars longs. */
char words[][7] = {
"anemik", "kameri", "marine", "minare", "makine", "anemi", "mekan",
  "krema", "kerim", "inmek", "imkan", "imren", "imran", "erkan",
  "ekran", "mera", "krem", "mine", "mira", "name", "ekim", "erik",
  "kim", "nem", "nam", "ani", "kin", "kir"
};

printf("This is the game\n Your letters are (k,a,n,m,e,r,i)");

int i;

/* Ask the user to input 7 letters. Each will be stored in `letters`
* successively at position 0, 1, 2, ... 6. */
for(i = 0 ; i < 7 ; i++) {
  scanf("%c", &letters[i]);
}
letters[7] = 0;

/* Look for the user input in the array of words. Note the use of strcmp
* to compare to string of chars. */
for(i = 0 ; i < (sizeof(words) / sizeof(words[0])) ; i++) {
  if(!strcmp(letters, words[i]))
    printf("valid value\n");
  else
    print("Not valid value\n");
}

return 0;
Jib
  • 1,334
  • 1
  • 2
  • 12