C struct | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 2

C struct

typedef struct Weapon { int dmg; char name[]; } Weapon; //////////// Weapon knife; knife.dmg = 40; // all good knife.name = "knife" // expression must be a modifiable value Help :/

23rd Jan 2019, 6:44 PM
NoxFly
NoxFly - avatar
5 Respostas
+ 2
knife.name = malloc(sizeof(char) * 6); knife.name[5] = '\0'; strcpy(knife.name, "knife"); Or easier typedef struct Weapon { int dmg; char name[50]; }; Weapon knife; strcpy(knife.name, "knife");
23rd Jan 2019, 7:16 PM
Just4f
Just4f - avatar
+ 3
Thanks Just4f TheWhiteCatšŸ’” even with it didnt work
23rd Jan 2019, 7:44 PM
NoxFly
NoxFly - avatar
+ 1
struct struturename { datamember1, datamember2, ā€” ā€” }variable; structure is a collection of heterogeneous datatypes The variables in structures are datamembers Every structure must have a variable called struture variable if it is normal variable use dot(.) operator to access structure variables if it is pointer use arrow(->) opreator to access structure variables structure with in structure is called nested structure ex: struct student { int sno; char sname[30]; }; void main() { struct student s; struct student *p; clrscr(); printf(ā€œEnter no :ā€); scanf(ā€œ%dā€,&s.sno); printf(ā€œEnter name :ā€); scanf(ā€œ%sā€,s.sname); printf(ā€œNumber = %d\nā€,s.sno); printf(ā€œName = %s\nā€,s.sname); printf(ā€œ\nā€); printf(ā€œEnter no :ā€); scanf(ā€œ%dā€,&p->sno); printf(ā€œEnter name :ā€); scanf(ā€œ%sā€,p->sname); printf(ā€œNumber = %d\nā€,p->sno); printf(ā€œName = %s\nā€,p->sname); getch(); }
24th Jan 2019, 5:53 AM
sree harsha
sree harsha - avatar
+ 1
You could try the following option: typedef struct { int dmg; char *name; } Weapon; ///////////// Weapon knife; knife.dmg = 40; knife.name = "knife";
28th Mar 2020, 1:17 AM
Arnold Ibarra
Arnold Ibarra - avatar
0
I think you should set the size of name char array => for example char name [20].
23rd Jan 2019, 7:11 PM
TheWhĀ”teCat šŸ‡§šŸ‡¬
TheWhĀ”teCat šŸ‡§šŸ‡¬ - avatar