CAST a CHAR array as a structure

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

CAST a CHAR array as a structure

5,060 次查看
WOLF
Contributor I
i have the following struct code
 
struct record
{
 char Name[21];
 char date[3];
};
 
 char array[24];
 char *ptr=array;
 
 struct record *read_record;
 
 array[0]=1;
 array[1]=2;

 read_record = (struct record*) ptr;
 
printf(" %c %c\n",read_record->Name[0],read_record->Name[1] );
 
this should display 1 and 2, but display nothing.   
 
can anyone expalin what i'm doing wrong?
thanks
 
 return 0;
标签 (1)
标记 (1)
0 项奖励
回复
2 回复数

782 次查看
CompilerGuru
NXP Employee
NXP Employee
First let me mention that normally such casts and similar things should not be done at all.
In your case, I guess the problem is that you print the (non printable) character with value 1, and not '1'.
E.g.:
 array[0]='1';
 array[1]='2';

But again, I have to have a good reason before I do such casts.

(or did you intend:
>printf(" %i %i\n",read_record->Name[0],read_record->Name[1] );
?)

Daniel
0 项奖励
回复

782 次查看
WOLF
Contributor I
Decided to do it, using a different aproach.
 
thanks anyway for the help
0 项奖励
回复