Alex Nichols
Alex Nichols

Reputation: 896

Printing a Char *

I apologize in advance for the dumb question!

Here is my struct def:

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

I am trying to print a representation of this struct with the function vcard_show(vcard *c), but the compiler is throwing back an 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);
}

When compiled: "warning: format ‘%c’ expects type ‘int’, but argument 2 has type ‘char *’"

Isn't %c the symbol for char*?

Upvotes: 18

Views: 110607

Answers (3)

Crash Magnet
Crash Magnet

Reputation: 31

Unless you have some typedef you are not telling us about, you should probably declare vcard_show() like this:

void vcard_show(struct vcard *c)

Upvotes: 3

Jonathon Simister
Jonathon Simister

Reputation: 286

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

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

Upvotes: 27

DanZimm
DanZimm

Reputation: 2568

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

Upvotes: 5

Related Questions