Thread: How does ++pointer works?

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    1

    Question How does ++pointer works?

    Suppose I create an array of integers. The name of the array is a pointer to the first element in the array.

    If I add 1 to this pointer, it will jump to the next integer in the array. The address itself will increase by 4, the number of bytes in an integer.

    How does the computer know that when I write:

    ++pointer;

    it must increase the address by 4?

    A pointer contains only an address. It does not contain the size of the pointed-to object.
    Where is the size of the pointed-to object stored?
    Is it stored at all in the computer?
    If it is, I suppose that the information takes up 4 bytes somewhere at a location different from that of the pointer?

    Thanks

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by BlueSalamander
    Suppose I create an array of integers. The name of the array is a pointer to the first element in the array.
    They are not the exact same thing... the name of the array "acts like" or "behaves as if" it were a pointer to the first element.

    Quote Originally Posted by BlueSalamander
    If I add 1 to this pointer, it will jump to the next integer in the array. The address itself will increase by 4, the number of bytes in an integer.

    How does the computer know that when I write:

    ++pointer;

    it must increase the address by 4?

    A pointer contains only an address. It does not contain the size of the pointed-to object.
    Where is the size of the pointed-to object stored?
    Is it stored at all in the computer?
    If it is, I suppose that the information takes up 4 bytes somewhere at a location different from that of the pointer?
    The pointer also has a type that it points to. This type controls how much a pointer to said type will increase or decrease. An integer pointer for example knows that it points to an integer and that (in your case) integers occupy 4 bytes. Therefore when you increment an int pointer, it knows that it should increase the address by 4.

    Likewise, a char pointer knows that it points to a char and that chars are 1 byte. Therefore when you increment a char pointer it knows to increase the address by 1.

    [edit]This concept also extends to types you create such as a struct that may be 20 bytes in size for example. If you had an array of these structs and a pointer to one of the elements in the array, then incrementing or decrementing the pointer would change the size by whatever the size of the type (20 bytes in this case) happened to be.[/edit]
    Last edited by hk_mp5kpdw; 03-03-2005 at 07:18 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    obs.:The variable of type int have 2 bytes(16 bits) man...or im wrong?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by k4z1nh0
    obs.:The variable of type int have 2 bytes(16 bits) man...or im wrong?

    Depends on your system. Most newer systems would probably be 4 bytes; with older systems perhaps 2 bytes was more common. The point is whatever the size of the type the pointer points to is the amount by which the pointer will be incremented when you use the ++ operator on that pointer.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    A pointer contains only an address. It does not contain the size of the pointed-to object.
    You tell the compiler how far to jump when you define the pointer. If you have:
    Code:
    int *ptr;
    The int part is telling the compiler that it should jump sizeof(int) bytes.

    And if you're using a compiler that is using 2-byte ints then it's a safe bet that it's time to throw that compiler away and get a new one.
    If you understand what you're doing, you're not learning anything.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > A pointer contains only an address. It does not contain the size of the pointed-to object.
    The compiler does sizeof(*ptr) for you.
    ptr++ always ends up pointing to the next object of the type being pointed to.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  2. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  3. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM
  4. sending or recieving a[][]to a function
    By arian in forum C++ Programming
    Replies: 12
    Last Post: 05-01-2004, 10:58 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM