Basic question on pointers....

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

Basic question on pointers....

跳至解决方案
1,238 次查看
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 解答
1,102 次查看
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 回复数
1,102 次查看
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 项奖励
回复
1,103 次查看
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