PDA

View Full Version : Doubt about char in C


SkyKnight
01-21-2010, 03:08 PM
Hi,


#include<stdio.h>

struct an
{
int num;
char name[256];
}a;

char *match = "This is a test: 10 ok";

int main(void) {
sscanf(match, "This is a test: %d %s", &a.num, &a.name);
printf("%d\n%s\n", a.num, a.name);
return 0;
}


When I run this program I am getting warnings as


t1.c: In function ‘main’:
t1.c:13: warning: format ‘%s’ expects type ‘char *’, but argument 4 has type ‘char (*)[256]’


Why I am getting this error, I tried by removing the variable name length, it causes warning in printf statement, what causing this and how to clear this warning?

Note: I am using gcc


Thanks in advance,
Alagunambi Welkin

abduraooft
01-22-2010, 08:31 AM
sscanf(match, "This is a test: %d %s", &a.num, &a.name); You don't need that & to refer the array a.name.

BrickInTheWall
01-22-2010, 12:29 PM
You don't need that & to refer the array a.name.
My thought too...to elaborate:
the name of an array can be used as a variable and is a pointer to it's first element. If you use the adress of operator on it like so: &arrayName then you will get the adress of the pointer that points to the first element.