# Thread: Basic Pointer Problem

1. ## Basic Pointer Problem

Code:
`#include<stdio.h>  int main() {     float a=3.14;     char *j;     j = (char*)&a;     printf("%d\n", *j);     return 0; }`
Forgive my ignorance, but what is the significance of the statement
j= (char*)&a; .

How is it different from j=&a? Why does it output the ASCII values of the first bye of the float variable rather than the value of a?

2. No difference from j = &a, except that the casting will satisfy the compiler's warning about incompatible pointer types. Plus it indicates that you as a programmer is forcing a pointer conversion. But in the end, the address that's assigned is the same number.
Why it outputs the first byte as ASCII value - because that's what you asked it to do. The pointer j points to an array of chars (as far as the pointer type suggests).

3. I am clear with the first part. But correct me please, the *j points to the starting address of the array which holds a floating value 3.14. Why does it print the ASCII value?
Eg. int *p, a = 10;
p= &a;
Now when I pass *p as an argument while calling printf function, the value 10 would get printed instead of the ASCII value. Why not the same over here? I am sorry if I am coming across completely novice, since I am exactly that. Thanks.

4. Originally Posted by Prateek Sarkar
the *j points to the starting address of the array which holds a floating value 3.14
j is the pointer to char; *j is a char. So, j points to the first element of an array of char. This array of char is what you "re-interpret" the float variable to be. So, the array does not contain a float value of 3.14. Rather, it contains a series of char values, that when interpreted as a float, is roughly 3.14.

Originally Posted by Prateek Sarkar
Now when I pass *p as an argument while calling printf function, the value 10 would get printed instead of the ASCII value. Why not the same over here?
p is a pointer to int, hence *p is an int, thus when you print *p, you print an int. No surprise there. j is a pointer to char, hence *j is an int, thus when you print *j, you print a char as an int (since %d is used). As such, the ASCII (or otherwise) value is printed.

5. Originally Posted by laserlight
j So, the array does not contain a float value of 3.14. Rather, it contains a series of char values, that when interpreted as a float, is roughly 3.14.
Can you kindly elaborate a little? As to how exactly does this happen?

p is a pointer to int, hence *p is an int, thus when you print *p, you print an int. No surprise there. j is a pointer to char, hence *j is an int, thus when you print *j, you print a char as an int (since %d is used). As such, the ASCII (or otherwise) value is printed.
What if I had used %c instead?

6. Originally Posted by Prateek Sarkar
Can you kindly elaborate a little? As to how exactly does this happen?
Imagine that you have four boxes of marbles. Each box contains a certain number of marbles, and collectively there are 314 marbles. What you are doing now is akin to looking at each box one by one and counting the number of marbles inside of it, then you are printing that number per box.

(Note that this analogy is not very good because floating point numbers are not simple summations of the values of the underlying bytes, but I think it would be useful to have a mental model of the object, i.e., the float variable, and the bytes that it occupies.)

7. Thanks have got a pretty good picture of what you have explained so far. But what I still don't get is why do you say j is pointing to an 'ARRAY' of characters? j= (char*)&a typecasts the pointer to be of character type right? Although the variable it points to is a floating variable. Why do this at all?

8. Originally Posted by Prateek Sarkar
But what I still don't get is why do you say j is pointing to an 'ARRAY' of characters?
Notice that my analogy had 4 boxes. An array of 4 boxes.

Originally Posted by Prateek Sarkar
j= (char*)&a typecasts the pointer to be of character type right?
It type casts the pointer that is the result of &a to be of pointer to character type.

Originally Posted by Prateek Sarkar
Although the variable it points to is a floating variable. Why do this at all?
To access the individual bytes of the float variable.

9. Thanks a lot

Popular pages Recent additions