Enrique Rodríguez Enrique Rodríguez - 1 year ago 74
C Question

Function to fill an array of names in c

I have this c piece of code which is working (just a sample of my code with four book names). It works in mingw under eclipse.
I read related posts, but didn't find any passing the argument it in this way. The output is also below.
Is there something wrong in this code (maybe a must related to pointer usage?)

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

void get_newbooks(char book_names[4][16]) {
sprintf(book_names[2], "Help yourself");
int main(void) {
char book_catalog[4][16]={{0}};
sprintf(book_catalog[2], "Help myself");
printf("I need the book: %16s\n", book_catalog[2]);


printf("I got the book : %16s\n", book_catalog[2]);
return 0;


output is:

I need the book: Help myself
I got the book : Help yourself

Answer Source

Is there something wrong in this code

Nothing wrong. Your function expects an argument of type char (*)[16] and you are passing an argument of the same type ( as per the rule that array as a function argument decay to pointer to its first element).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download