in C, a variable doesn't retain the value i assign it and goes back to 0 [closed]

in C, a variable doesn't retain the value i assign it and goes back to 0 [closed]



I am writing a simple game engine in C and I have come across a very odd problem.


//SANJI
objs[1].animation = 1;
objs[1].pho.h = 52;
objs[1].pho.w = 25;
objs[1].pho.tmp_pos.x = 190;
objs[1].pho.tmp_pos.y = 40;
objs[1].pho.gravity = 1;
objs[1].hp = 10;

printf("%dn", objs[1].hp);//outputs 0???????



It seems no matter what I do the variable (member) hp is refusing to accept the assignment and goes back to 0. I tried searching with Google, but I am afraid I am unable to phrase this problem accurately.


hp


0



EDIT:
this is the definition of the OBJ struct:


struct OBJ
//animation
char animation;//if true apply animation; experimetnal

MOVIE sprites;
PHYSOBJ pho;

//OBJ_STATE state;
OBJ_STATE states[SEQ_MAX][ST_MAX];
int img;//number of image


int hp;

//control; AI or human
char control;//0 human, otherwise AI
;



Also there is no code between the printf and objs[1].hp = 10; i put the printf there for testing



This question appears to be off-topic. The users who voted to close gave this specific reason:





hp and objs[1].hp are two different things. It would help to see a Minimal, Complete, and Verifiable example.
– Retired Ninja
Aug 23 at 0:33


hp


objs[1].hp





At a minimum, please post the code where you define the struct for "objs". That would tell us how hp is defined, and how it relates to objs. But a COMPLETE example would be best: stackoverflow.com/help/mcve
– paulsm4
Aug 23 at 0:48






Need to see how you define "objs", and are you sure the "%d" is correct?
– asdf
Aug 23 at 0:51





This complete example: ideone.com/DqW1j1 works fine, so it would seem there's something in the code we can't see or replicate without assistance from you.
– Retired Ninja
Aug 23 at 1:30





@user3629249: objs[1].animation = 1; is just fine. Furthermore, C has no way to write a one-byte integer literal so you have to let the compiler do the int-to-char conversion..
– rici
Aug 23 at 3:24


objs[1].animation = 1;




1 Answer
1



the following proposed code:



and now the proposed code:


#include <stdio.h>

struct POS

int x;
int y;
;


struct PHO

int h;
int w;
struct POS tmp_pos;
int gravity;
;
typedef struct PHO PHYSOBJ;


struct OBJ

//animation
char animation;//if true apply animation; experimetnal

//MOVIE sprites;
PHYSOBJ pho;

//OBJ_STATE state;
//OBJ_STATE states[SEQ_MAX][ST_MAX];
int img;//number of image


int hp;

//control; AI or human
char control;//0 human, otherwise AI
;


int main( void )

struct OBJ objs[2];

objs[1].animation = 1;
objs[1].pho.h = 52;
objs[1].pho.w = 25;
objs[1].pho.tmp_pos.x = 190;
objs[1].pho.tmp_pos.y = 40;
objs[1].pho.gravity = 1;
objs[1].hp = 10;

printf("%dn", objs[1].hp);//outputs 0???????

return 0;



When compiled, linked, run, the output is as follows:


10



So the problem is in something you have not shown us.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế