Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Compilation error in snprintf saying incompatible types

I have this program with me. Where I am trying to concatenate a user input to a command, inorder to execute it in a wsl environment.

#include<stdio.h>
#include<stdlib.h>

int main(){
    char cmd[100];
    char usr_in[50];

    fgets(usr_in, sizeof(usr_in), stdin);
    cmd = snprintf(cmd, sizeof(cmd), "ping %s", usr_in);

    system(cmd);
    return 0;
}

But this gives me the following error during compilation.

error: incompatible types in assignment of ‘int’ to ‘char [100]’
cmd = snprintf(cmd, sizeof(cmd), "ping %s", usr_in);

I am not able to figure out which integer assignment it is talking about. The sizeof(cmd) is the only integer there and it is a valid argument to the snprintf.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

snprintf returns int (the number of characters printed) and you are trying to assign the return value to cmd which is char[100].

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading