Incompatible format types

Posted by nebffa on Stack Overflow See other posts from Stack Overflow or by nebffa
Published on 2012-11-12T04:56:28Z Indexed on 2012/11/12 4:59 UTC
Read the original article Hit count: 169

Filed under:

I'm playing around with strncpy in C and am having some trouble.

The code is as follows:

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    char src[] = "Benjamin Franklin";
    char dest[5];

    strncpy(src, dest, sizeof(dest) / sizeof(char));
    dest[5] = '\0';

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

    return 0;
}

which compiles with no errors using:

gcc -Wall -g -Werror    test.c   -o test

and prints out gibberish like

p4??

I cannot really understand what I'm doing wrong especially since I have played around with it a lot and been looking online for answers. Perhaps since I am using arrays I am passing the address to printf without realising it?

© Stack Overflow or respective owner

Related posts about c