How to duplicate a string in C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
    char *dup = NULL;

    const char *s = "c programming";

    dup = strdup(s);

    if (dup == NULL) {
        perror("strdup");
        exit(EXIT_FAILURE);
    }


    printf("%s\n", dup);

    free(dup);

    return 0;
}




/*
run

c programming

*/

 



answered May 2, 2021 by avibootz
...