Thread: Basic Pointer Problem

  1. #1
    Registered User
    Join Date
    Jan 2013
    Location
    Kolkata
    Posts
    20

    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?

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    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. #3
    Registered User
    Join Date
    Jan 2013
    Location
    Kolkata
    Posts
    20
    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. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.

    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2013
    Location
    Kolkata
    Posts
    20
    Quote Originally Posted by laserlight View Post
    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. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2013
    Location
    Kolkata
    Posts
    20
    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. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.

    Quote 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.

    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jan 2013
    Location
    Kolkata
    Posts
    20
    Thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very basic pointer problem
    By DeliriumCordia in forum C Programming
    Replies: 6
    Last Post: 09-10-2012, 11:00 AM
  2. Basic Pointer Help
    By Neti in forum C++ Programming
    Replies: 2
    Last Post: 07-28-2012, 02:02 PM
  3. very basic pointer questions--pls help
    By tkm in forum C Programming
    Replies: 7
    Last Post: 02-04-2011, 02:54 PM
  4. Basic pointer question
    By james123 in forum C Programming
    Replies: 4
    Last Post: 12-29-2009, 09:53 AM
  5. Basic pointer q
    By Jasper in forum C Programming
    Replies: 1
    Last Post: 11-28-2009, 02:44 AM