Char seal *

I apologize in advance for the stupid question!

Here is my def structure:

struct vcard { char *cnet; char *email; char *fname; char *lname; char *tel; }; 

I am trying to print a representation of this structure using the vcard_show (vcard * c) function, but the compiler throws a warning:

 void vcard_show(struct vcard *c) { printf("First Name: %c\n", c->fname); printf("Last Name: %c\n", c->lname); printf("CNet ID: %c\n", c->cnet); printf("Email: %c\n", c->email); printf("Phone Number: %c\n", c->tel); } 

At compilation: "warning: format"% c expects type "int", but argument 2 is of type "char *"

Isn't that the% character for the char * character?

+7
source share
3 answers

You want to use %s , which is for strings (char *). %c for single characters (char).

An asterisk * after a type makes it a pointer to a type. So char* is actually a pointer to a character. In C, strings are passed by reference, passing a pointer to the first character of the string. The end of a line is determined by setting the byte after the last character of the line to NULL (0).

+13
source

The property type encoding for char * is %s . %c for char (not just a single char pointer)

+5
source

If you do not have any typedef that you are not telling us about, you should probably declare vcard_show() as follows:

 void vcard_show(struct vcard *c) 
+2
source

All Articles