Question:
c programming error,structures.?
jsz
2011-12-16 10:47:56 UTC
hi i need the working code for accepting string input from the console into an array variable which is in a structure.i tried to write it in ubuntu but i got an error.1111.c: In function ‘main’:
1111.c:11: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[10]’

#include
main()
{
struct profile
{
char name[10];
int age;
};
struct profile p;
printf("enter name");
scanf("%s",&p.name);
printf("enter age");
scanf("%d",&p.age);
}

my compiler version is: gcc (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5

however the same program works well in turbo c compiler.PLZ help me work with my(Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5 compiler,
thanks
Three answers:
jplatt39
2011-12-16 12:50:53 UTC
compiles and runs with no problem with my gcc 4.5.3 on Slackware. I'd say first thing to do is open a terminal and try sudo apt-get update then sudo apt-get upgrade build-essential.
warpjedi
2011-12-16 19:05:56 UTC
Try this:



#include

main()

{

struct profile

{

char *name;

int age;

};

struct profile p;

p.name = new char[10];

printf("enter name");

scanf("%s",&p.name);

printf("enter age");

scanf("%d",&p.age);

}
anonymous
2011-12-16 18:55:35 UTC
All you should have to do is use p.name (do not use the &).


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...