Thread: C Pointer question

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    8

    C Pointer question

    Hi to all.

    I have a small question regarding pointers concept is C. The code is below

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int main(void);      //Prototype for main() function. Ofcourse not required
    
    int main(void)
    {
    
       int i=2,*j;
       j=&i;
       clrscr();
    
         printf("Value of i is %d\n",i);
         printf("Address of i is %u", j);
    
       getch();
       return 0;
    
    }
    Output:

    Value of i is 2
    Address of is is 65524

    Here my question is, we declared *j as int. The int data type range is -32768 to 0 to +32767. It is in 2 Bytes size. But how it able to print the address value which is beyond the range 32767. Actually we need to declare *j as

    unsigned int *j;

    Because unsigned int range is 0 to 65535. But instead of this, if we declare *j as

    int *j;

    it is able to print the value beyond specified range. How this is possible. Please let me know.

    Thanks to all.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by girish1026
    Here my question is, we declared *j as int. The int data type range is -32768 to 0 to +32767. It is in 2 Bytes size. But how it able to print the address value which is beyond the range 32767.
    In the second printf, you did not print *j. You printed j. Normally, we would use %p instead of %u and cast j to void*.
    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

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    74
    'j' is not an int, its a pointer to int.
    So that range doesn't apply to pointers.

    The value of 'j' is an address of memory, in your case, the address of the variable 'i'.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    8
    Quote Originally Posted by laserlight View Post
    In the second printf, you did not print *j. You printed j. Normally, we would use %p instead of %u and cast j to void*.
    I am sorry laserlight. I did not understand your answer in clear way. Please let me know it in detail. Thanks.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Actually, I think noobiept got your question better: you are confusing the pointer with what it points to. That is why you asked about *j when you printed j.
    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

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    8
    Quote Originally Posted by laserlight View Post
    Actually, I think noobiept got your question better: you are confusing the pointer with what it points to. That is why you asked about *j when you printed j.
    Ya now i understood. Actually j points to the address of i and not actually it stores. So that we can print the address directly. Thanks for your reply.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What? A pointer stores the address of whatever it points to, so of course you can print the address directly.
    Also note that you declare j to be an int*. There is no variable named *j if that's what you believe.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    To answer your question, girish1026, which everyone is dancing around...

    Declaring a variable int vs unsigned int makes no difference in how its values are represented internally. For your compiler/machine it may be a 2 byte integer for example. Therefore formatting for printf is not affected by the storage type either. It's because you said "%u" that the value is interpreted to be unsigned. Or you could specify "%d" if you want to see it as a signed number. "%p" is available for pointers which are usually interpreted as unsigned.

    The only difference signed vs unsigned makes is during calculations, conversions during assignment, etc. where overflow conditions may be handled differently for either type.
    Last edited by nonoob; 07-14-2010 at 02:26 PM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nonoob
    To answer your question, girish1026, which everyone is dancing around...

    Declaring a variable int vs unsigned int makes no difference in how its values are represented internally. For your compiler/machine it may be a 2 byte integer for example. Therefore formatting for printf is not affected by the storage type either. It's because you said "%u" that the value is interpreted to be unsigned. Or you could specify "%d" or "%p" if you want to see it as a signed number.
    That does not actually answer the question correctly. The problem, as noobiept, Elysia and I have stated is that this:
    Code:
    printf("Address of i is %u", j);
    Prints the pointer j, whose value is the address of i. Therefore, that i is of type int or unsigned int does not matter. What matters is that j is of a pointer type, and it turns out that printing the value of this pointer prints 65524. However, girish1026 expected that the type of j must have the same range as the type of i (i.e., *j), but this is not the case.
    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

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do not print a non-pointer type with %p. You will easily invoke undefined behavior.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 10:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 02:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM