Thread: newbie: pointers to array question

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    61

    Wink newbie: pointers to array question

    hi im not really sure about this, hope u guys can help me understand.

    lets say we have a array of characters and a pointer:

    char string[] = "my string";
    char *p

    p = string +1;

    does this means dat p is now pointing to character 'y'?? if i dereference the pointer using *(p+2) it will point to a null value which is illegal cos p is only pointing to chracter 'y' and not the string itself?

    thanks for the help.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    p is pointint to the character y in the string
    so p+2 is pointing to string+3 to the character s and dereferencing it you will get the same char as string[3]
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    61
    thanks vart., i understand it now. i have another question though. if i reference the array using &string[3], do i get character 's' as well?

  4. #4
    Banned
    Join Date
    Nov 2007
    Posts
    678
    no. you get the address of string, past three locations.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    61
    thanks manav for making this clear.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Note that pointers are only storing a memory location. So when you take the address of the string and add 1, you jump to the address one byte past the starting point. For strings, each character takes 1 byte, hence you jump to the second character, which is 'y', when you dereference the pointer.

    Now p is one byte past the start of the array. Add 2 more and you're 3 bytes past the start of the array. Dereference it and you get the 4th char, 's'. If you use the & operator, you take the address of something, so &string[3] gets the address where the 4th character is in the array (3 bytes past the beginning).

    Note that operator [] always deferences a pointer automatically, so string[3] gives 'y' and then & takes the address.

    For more information, an informative article might be,
    http://cpwiki.sf.net/A_pointer_on_pointers
    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. pointer to an array question..
    By transgalactic2 in forum C Programming
    Replies: 41
    Last Post: 10-20-2008, 12:22 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Newbie question: pointers, other program's memory
    By xxxxme in forum C++ Programming
    Replies: 23
    Last Post: 11-25-2006, 01:00 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Replies: 5
    Last Post: 11-20-2001, 12:48 PM