2 Questions about POINTER and MEMORY.

This is a discussion on 2 Questions about POINTER and MEMORY. within the C++ Programming forums, part of the General Programming Boards category; int *p= 0; p++ ; // why each ++, will increase the address by 2 ???? output: 0x12345670 0x12345672 0x12345674 ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    88

    2 Questions about POINTER and MEMORY.

    int *p= 0;

    p++ ; // why each ++, will increase the address by 2 ????

    output:
    0x12345670
    0x12345672
    0x12345674
    0x12345676
    .
    .
    ...

    //---------------------------------------------------------------------------//

    int *p = 0 ; // That 's OK

    int *p = 1 ; // Why ERROR?????

    thx~!

  2. #2
    btq
    btq is offline
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    p++ ; // why each ++, will increase the address by 2 ????
    if you work with 16-bit adresses when you increase the adress you move it forward by 16-bits,which is 2 bytes..therefor it increases by the value of 2.

    int *p=0;
    this means that p points to nothing.
    more apropriate might be:
    int *p=NULL; but it's practically the same.
    I guess your compiler doesnt like this:
    int *p=1;
    and it shouldn't. You'd want to initialize a pointer to an adress, not a number..

    /btqq
    ...viewlexx - julie lexx

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    88
    Originally posted by btq

    and it shouldn't. You'd want to initialize a pointer to an adress, not a number..

    /btqq [/B]
    how to initialize it to an address ?

    p = 1 ; /// why can't i use 1 as an address??

  4. #4
    btq
    btq is offline
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    the most common way to do it is to let the pointer point to a variable...

    int i=2;
    int *ptr=NULL; //it's a good thing to always initialize pointers to NULL....
    ptr=&i;

    & is an adress operator that is used to assign the adress of i to ptr..so now ptr points to the variable i, that is, ptr contains the adress in which the contents of i is stored.

    there are LOTS of text on the net about this, you might wanna check it out..

    /btq
    ...viewlexx - julie lexx

  5. #5
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Hi,
    As far as I know you cannot say:
    Code:
    int *p=1
    because if you initialize the pointer you should initialized it to as address since the pointer stores the address only,

    but I think you can do this:
    Code:
    int x;
    int *p;
    
    p=&x;
    
    *p=1;
    so with *p you can access the content of the variable which address is stored in the pointer *p.

  6. #6
    btq
    btq is offline
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    so with *p you can access the content of the variable which address is stored in the pointer *p.
    yup, the * here is called a dereferencer..

    /btq
    ...viewlexx - julie lexx

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    88
    Originally posted by ammar
    Hi,
    As far as I know you cannot say:
    Code:
    int *p=1
    because if you initialize the pointer you should initialized it to as address since the pointer stores the address only,

    but I think you can do this:
    Code:
    int x;
    int *p;
    
    p=&x;
    
    *p=1;
    so with *p you can access the content of the variable which address is stored in the pointer *p.

    HMM , i understood , thx.

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, 05:23 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 10:37 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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