CAST a CHAR array as a structure

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

CAST a CHAR array as a structure

4,659 Views
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;
Labels (1)
0 Kudos
2 Replies

381 Views
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 Kudos

381 Views
WOLF
Contributor I
Decided to do it, using a different aproach.
 
thanks anyway for the help
0 Kudos