Thread: pointer problem

  1. #1
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587

    pointer problem

    Hi all,
    I have trouble understanding the following code.
    Code:
    #include <stdio.h>
    int main(void){
    	int *p,*q,i;
    	p=(int *)100;
    	q=(int *)700;
    	i=q-p;
    	printf("\n%d\n",i);
    	return 0;
    }
    The output comes as
    300
    can somebody please explain this.
    Any help would be appreciated.
    -------
    stevesmithx
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    to me, it looks like your assigning to p and q an address to a location in memory of size 'int', rather than signing integer values '100' and '700' (in which case youd expect the output to be 600). when you do q-p, its subtracting the hexadecimal addresses corresponding to q and p, respectively. this difference happens to be 300 in base 10 (decimal).

  3. #3
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Thanks for replying, nadroj.
    I traced the addresses assigned to p and q using debugger.
    For p->0064(corresponds to 100 in decimal)
    q->02BC(corresponds to 700 in decimal)
    So the difference between the two should be 600(in decimal).
    I am confused why 300 should come as output.
    Can you explain that part,please.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It actually makes sense, because pointers are not integers.
    If you subtract from a pointer, you will subtract n - sizeof(*p).
    So when you subtract 100 from the integer pointer, you actually subtract a total of 100 * sizeof(int), which is 400, and 700 - 400 = 300.
    This is actually a good thing since we can increment pointers by doing n++, which would then point to the next element in an array or n-- which would do the reverse.
    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.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    My opinion is that the behaviour here is undefined, but my guess is that because pointer difference gives the number of elements between the two pointers, the result of 300 means that there are 300 ints between p and q. Since the difference in raw address values is 600, the implication is that sizeof(int) is 600/300=2 on your system.

    So when you subtract 100 from the integer pointer, you actually subtract a total of 100 * sizeof(int), which is 400, and 700 - 400 = 300.
    It is not subtracting 100 from the int*, but another int* from the int* to return a ptrdiff_t.
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It might also be worth noting that simply because one pointer is located at address 100 and one at 700, does not mean there are 600 integers between there [there would only be space for 600 / sizeof(int)].
    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.

  7. #7
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Now I can understand that.
    Thank you very much to all of you.
    One more question,
    Is it right to assume the sizeof(int) always as 2?
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, far from it.
    On a x86/x64 machine running Windows, sizeof(int) = 4.
    I believe, on Linux or some other platform (with x64), sizeof(int) = 8
    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.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    stevesmithx, just to confirm my reasoning, have you checked the value of sizeof(int) on your system by printing it?
    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
    Oh and no type except char (sizeof(char) is always 1) is guaranteed to have the same size of all platforms.
    They have different sizes on different platforms, which is actually where there exists a couple of typedefs that when used is guaranteed to be the size you look for.
    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.

  11. #11
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    No, far from it.
    On a x86/x64 machine running Windows, sizeof(int) = 4.
    I believe, on Linux or some other platform (with x64), sizeof(int) = 8
    Thank you Elysia,
    I asked this question because this was asked as an interview question.
    So now i am assured that none of their choices were correct.
    stevesmithx, just to confirm my reasoning, have you checked the value of sizeof(int) on your system by printing it?
    I checked it.You are right.It was 2.
    Thank you all.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    83
    Quote Originally Posted by laserlight View Post
    My opinion is that the behaviour here is undefined, but my guess is that because pointer difference gives the number of elements between the two pointers, the result of 300 means that there are 300 ints between p and q. Since the difference in raw address values is 600, the implication is that sizeof(int) is 600/300=2 on your system.


    It is not subtracting 100 from the int*, but another int* from the int* to return a ptrdiff_t.
    Hai Dear
    I am getting output of same program as 150 what may be the reason?
    i am not able to follow this:-

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, if you follow laserlight's reasoning, then if sizeof(int) = 4, then you would get 150.
    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.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    No, far from it.
    On a x86/x64 machine running Windows, sizeof(int) = 4.
    I believe, on Linux or some other platform (with x64), sizeof(int) = 8
    Not correct, sizeof(int) is 4 on x86_64 as well as on 32-bit x86 (but long is 8 in x86_64). That's not to say that machines where sizeof(int) == 8 or sizeof(int) == 1 may exist. 4 is certainly the common value in modern compilers/OS's, but it's absolutely not guaranteed. The only provisions in the standard is that:
    Code:
    sizeof(char) == 1;
    sizeof(short) >= sizeof(char)
    sizeof(int) >= sizeof(short)
    sizeof(long) >= sizoef(int)
    sizeof(long long) >= sizeof(long)
    Theoretically, it's valid to build a compiler where all those sizes are 1. It would just either make all integers pretty small (char = 8 bits) or strings take up quite a bit of space (char = 32/64 bits).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    While all data types could be the same size, there are minimum sizes in C89 -- 2.2.4.2, C99 -- 5.2.4.2.1, for integral types. So an int could not take up only 8 binary bits.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 07:37 PM
  3. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM