Basic question on pointers....

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

Basic question on pointers....

Jump to solution
755 Views
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!

Labels (1)
1 Solution
619 Views
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


View solution in original post

2 Replies
619 Views
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 Kudos
Reply
620 Views
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