0
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>

int main()
{
    string Name[2];

    {
        printf("Enter your name: \n");
        scanf("%s%s", &Name[0], &Name[1]);
    }
    int i = strlen(Name[0]);

    int j = strlen(Name[1]);


    printf("%c%c\n", toupper(Name[i][0]), toupper(Name[j][0]));
}    

this is my code to print the initials of user's name in capital letters, but the compiler keeps outputting this error.

~/workspace/pset2/ $ make initials
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wshadow    initials.c  -lcs50 -lm -o initials
initials.c:12:23: error: format specifies type 'char *' but the argument has type 'string *' (aka 'char **') [-Werror,-Wformat]
        scanf("%s%s", &Name[0], &Name[1]);
               ~~     ^~~~~~~~
initials.c:12:33: error: format specifies type 'char *' but the argument has type 'string *' (aka 'char **') [-Werror,-Wformat]
        scanf("%s%s", &Name[0], &Name[1]);
                 ~~             ^~~~~~~~
2 errors generated.
make: *** [initials] Error 1

1 Answer 1

1

First, a warning about your approach. Even though you say "practising", how will your code handle this example "robert thomas bowden" from the spec? What about "john jacob jinglehammer jones"?

The compile error is very explicit. The type of the format "%s" does not match the type of it's argument "&Name[0]". "%s" is a string pointer, or char*. "&Name[0]" is a pointer to a pointer or char**. Removing the "&" is not going to work either (it will compile, it will segfault when run), because Name[0] has no memory allocated. If you don't want to use the check50 function GetString, you're going to have to study future lessons/lectures on pointers and memory allocation.

Looks like it's back to the drawing board!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .