pointers

This is a discussion on pointers within the C++ Programming forums, part of the General Programming Boards category; hello guys, here is my problem, pls go through. pls tell me the answer and ***why*** main() { int *p,*q; ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    17

    Thumbs up pointers

    hello guys,
    here is my problem, pls go through.
    pls tell me the answer and ***why***

    main()
    {
    int *p,*q;
    p=(int *)1000;
    q=(int *)2000;
    printf("%d",(q-p));
    }
    srinu

  2. #2
    Unregistered
    Guest
    If you use p = (int *)1000; you tell the compiler that you want to set p to point to memory address 1000. You should use *p = 1000; and *q = 2000;

    Because the memory addresses are of an integer type, the output given will still be 1000, but the value pointed to by p will not be 1000 and the value pointed to by q will not be 2000.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Some kind of forced pointer difference exercise

    The answer is 500 (if sizeof(int)==2)
    The answer is 250 (if sizeof(int)==4)

    There's a 1000 bytes between p and q, but pointer difference (q-p) returns the number of ints between the two pointers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21