Basic question on pointers....

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Basic question on pointers....

ソリューションへジャンプ
788件の閲覧回数
FIDDO
Contributor III

Consider the following example

 

 

void tester(unsigned char *);

 

void main()

{

unsigned int test,y;

tester(&test);

y=test

}

 

void tester( unsigned char *x)

{

*x=36;

 

}

 

What will be the value  of y now?

whether address  of test  will be removed from the subroutine void tester once its exits!!

 

Please clarify!

ラベル(1)
1 解決策
652件の閲覧回数
StenS
Contributor III

Not quite true! y and test are of type unsigned int (16 bits) while the argument to the tester-routine is a pointer to an unsigned char. The compiler will probably give you a warning, but will compile and the program will run. Since the Freescale-controllers are big-endian, the tester-routine will write the number 36 to the most significant byte of the test-integer, giving a result of 0x2400 = 9216.

/Sten


元の投稿で解決策を見る

2 返答(返信)
652件の閲覧回数
ah
Contributor I

Value of y will be 36.

the address of variable test is taken as the value x is pointing to. so the address of variable will hold value 36.

as y is assigned to test, y becomes 36.

I hope it helps.

-AH

0 件の賞賛
返信
653件の閲覧回数
StenS
Contributor III

Not quite true! y and test are of type unsigned int (16 bits) while the argument to the tester-routine is a pointer to an unsigned char. The compiler will probably give you a warning, but will compile and the program will run. Since the Freescale-controllers are big-endian, the tester-routine will write the number 36 to the most significant byte of the test-integer, giving a result of 0x2400 = 9216.

/Sten