0
#include<stdio.h>
#include<curses.h>
#include<string.h>
void main()
{   const char *str1[20];
    const char *str2[20];
    int comp;
    printf("Enter the first string:\n");
    scanf("%s",&str1);
    printf("Enter the second string:\n");
    scanf("%s",&str2);
    comp=strcmp(str1,str2);
}

This was compiled in gcc compiler 4.8 . A detailed explanation will be appreciated.

Error message:

strcmp.c: In function ‘main’:
strcmp.c:10:10: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘const char * (*)[20]’ [-Wformat=]
  scanf("%s",&str1);
          ^
strcmp.c:12:10: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘const char * (*)[20]’ [-Wformat=]
  scanf("%s",&str2);
          ^
strcmp.c:13:14: warning: passing argument 1 of ‘strcmp’ from incompatible pointer type [-Wincompatible-pointer-types]
  comp=strcmp(str1,str2);
              ^~~~
In file included from strcmp.c:3:0:
/usr/include/string.h:140:12: note: expected ‘const char *’ but argument is of type ‘const char **’
 extern int strcmp (const char *__s1, const char *__s2)
            ^~~~~~
strcmp.c:13:19: warning: passing argument 2 of ‘strcmp’ from incompatible pointer type [-Wincompatible-pointer-types]
  comp=strcmp(str1,str2);
                   ^~~~
In file included from strcmp.c:3:0:
/usr/include/string.h:140:12: note: expected ‘const char *’ but argument is of type ‘const char **’
 extern int strcmp (const char *__s1, const char *__s2)
2
  • 1
    Change const char *str1[20]; to char str1[20];, and scanf("%s",&str1); to scanf("%s",&str[0]);. Same with second string. That's a good start. Also, you're not printing any value, so hard to know the result of the comparaison. Commented Apr 10, 2017 at 17:40
  • What is str1? What is e.g. str1[0]? What is&str1? When you can answer those questions you will know the answer to your problem. Also think about the meaning of the const keyword. And if you haven't got one, then find a good beginners book and reread the chapters on arrays and pointers. Commented Apr 10, 2017 at 17:42

4 Answers 4

3
const char *str1[20];

Should be

char str1[20];

You want a mutable array of characters, not an array of pointers.

scanf("%s",&str1);

Should be:

scanf("%s",str1);

The scanf function needs the address to store the input in. That's equivalent to &str1[0] which is what str1 decays into.

Sign up to request clarification or add additional context in comments.

1 Comment

This change alone won't help much, considering that e.g. str1 isn't an array of characters.
2

Your declared arrays are of the wrong type and qualifier.

The first thing is the incorrect declarations of the string buffers. You want an array of characters, but const char* str[20] declares an array of const character pointers (that is why the compiler is talking about argument 2 with type const char * (*)[20] being the wrong type). You do not want const here either since you want the characters to be changeable. char str[20] declares a string of characters which is what you really want.

Another point in C coding is that the address of an array is the same as the name of an array, so &str1 and str1 mean the same thing.

Comments

2

Change const char *str1[20]; to char str1[20];,

and scanf("%s",&str1); to scanf("%s",&str1[0]);

Same with second string.

As it currently is, you're declaring, not an array of characters, but an array of pointers to characters.

You're not printing any value, so hard to know the result of the comparison.

Comments

1

You did not explain exactly what your code is supposed to do, but I guess that

  const char *str1[20];

(the above declares an array of 20 pointers to constant strings, or zones of several char-s)

should probably be

  char str1[256];

this declares a single string -which should be writable by the computer, since you read it from thje user- of at most 256 bytes, and you want it to be null-terminated

(I'm using 256, because 80 bytes is really not a lot - and 20 bytes is not enough -; you want UTF8 everywhere, and 80 bytes can be much less than 80 characters).

BTW

    scanf("%s",&str1);

is poor taste (may you wanted scanf("%19s", str1);). I suggest to use fgets and code:

 if (!fgets(str1, sizeof(str1)-1, stdin)) {
    perror("fgets str1");
    exit(EXIT_FAILURE);
 }

(Hint, you should always test for successful input).

BTW, always improve your code to get no warnings, and yes you need to compile with gcc -Wall -g (all warnings and debug info) to be able to use the gdb debugger (which you need to learn).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.