0

Cant figure out whats wrong with this function but i think its probably a data type error.
Please help!

void displayName(){
    char name[30];
    int number, x;

    printf("So..what is your name?\n");
    scanf_s("%s",&name);
    printf("Nice to meet you %s, how many times would you like to be cloned?\n",name);
    scanf_s("%i", &number);
    for(x=0; x<number; x++){
        printf("%s",name);
    }
 }
4
Contributors
3
Replies
25
Views
4 Years
Discussion Span
Last Post by cmps
0

Seeing as you haven't posted the actual error message, I'm guessing you're getting an error or a warning about the call to scanf_s at line 6.

Basically you don't need the & operator before name.
scanf_s expects a char * to be passed in as the 2nd parameter. And in C, arrays are implicitly pointers. So passing name will suffice, no need for the & operator. Using the & operator there converts the pointer to an incompatible type! char (*)[30]

EDIT: Not too sure I've explained the above paragraph particularly accurately...Sorry, had a few brewskies this evening! :)

Edited by JasonHippy

0

What is the error ?
Why don't you use scanf() ?
And you don't need to put &name, because name is already a pointer to the address of the first character of the array

void displayName(){
    char name[30];
    int number, x;

    printf("So..what is your name?\n");
    scanf("%s",name);
    printf("Nice to meet you %s, how many times would you like to be cloned?\n",name);
    scanf("%i", &number);
    for(x=0; x<number; x++){
        printf("%s",name);
    }
 }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.