Question about pointer arithmetic and types

This is a discussion on Question about pointer arithmetic and types within the C Programming forums, part of the General Programming Boards category; So I would like to know how a pointer acts based on its type. Code: char *ptr; /* char == ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    40

    Question about pointer arithmetic and types

    So I would like to know how a pointer acts based on its type.

    Code:
    char *ptr; /* char == 1 byte */
    So say the memory looks like this; and the pointer points to the byte of memory indicated with x's.

    Code:
    +--------+--------+--------+--------+
    |        |        |xxxxxxxx|        |
    +--------+--------+--------+--------+
    Now if I cast ptr to be of type int16 (2 bytes), will the memory then be pointing to a chunk that looks like this...?

    Code:
    ptr = (int16 *)ptr;
    
    +--------+--------+--------+--------+
    |        |        |xxxxxxxx|xxxxxxxx|
    +--------+--------+--------+--------+
    Also, if a pointer of type larger than one byte points to a chunk of memory, if I dereference that pointer and fill it with a value, will it start filling bits from right to left? Like this:

    Code:
    *ptr = 9;
    
    +--------+--------+--------+--------+
    |        |        |xxxxxxxx|xxxx1001|
    +--------+--------+--------+--------+
    And if I increment the pointer, it will increment by the type that it is? I.e.

    Code:
    ptr += 1;
    
    +--------+--------+--------+--------+
    |        |        |xxxxxxxx|xxxx1001|
    +--------+--------+--------+--------+
    |eeeeeeee|eeeeeeee|        |        |
    +--------+--------+--------+--------+
    
    ptr now points to blocks full of e's and if you were to modify, it starts filling from rightmost e to left...?
    Also, if there are any tutorials on not only pointer arithmetic, but on situations where you cast a pointer to different type and start filling memory, etc... would be helpful. Thanks!
    Last edited by brooksbp; 08-17-2008 at 07:33 PM.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    >> Now if I cast ptr to be of type int16 (2 bytes), will the memory then be pointing to a
    >> chunk that looks like this...?

    You're certainly being clever.

    Pointers do not necessarily fill in data from left to right if you're simulating an array. The index operator can help you access any element, and elements are stored where they are assigned, in the order they are assigned.

    Casting pointer types without a good reason is usually a bad idea, as evidenced by the warning you ought to receive if you do something odd like assign pointers of incompatible types. That said, casting a pointer to a type usually changes it to the type for the sake of that operation alone.

    Code:
    #include <stdio.h>
    
    int main (void)
    {
        char cs[30];
        char * p = cs;
        printf( "p is at &#37;p\n", (void*)p );
    
        p++;
        printf( "p is at %p\n", (void*)p );
    
        p = (long*)p + 16;
        printf( "p is at %p\n", (void*)p );
    
        return 0;
    }
    
    p is at 0012FF40
    p is at 0012FF41
    p is at 0012FF81
    You'll notice that the size of the step is much different because of the type p assumed at the time. p jumped 64 bytes before being printed again.

    Now, storing data of another type, like an integer, in the midst of something else like a string is not smart. The underlying bit representation for char and integer are completely different, for example. You would lose the ability to efficiently read and manipulate either the string or the integer. You risk inadvertently corrupting the storage space during a math or string operation.

    If you do run into a bug it will be nasty. Do not do that with two types.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > xxxxxxxx|xxxx1001
    Look up "big endian" and "little endian"
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    40
    cool. thanks citizen. I really hate having to use nasty pointer arithmetic... sucks when you can't model or express functionality using formal constructs...

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    You had it all right (assuming Big Endian at least) up until the diagram with all the E's. You've actually performed ptr -= 1; in that diagram.
    A longer type extends to occupying more memory in the positive direction, i.e. the direction you go when you increment the pointer. Here's a little endian demo:

    char *ptr;
    Code:
    +--------+--------+--------+--------+
    |xxxxxxxx|        |        |        |
    +--------+--------+--------+--------+
    *ptr = 6;
    Code:
    +--------+--------+--------+--------+
    |00000110|        |        |        |
    +--------+--------+--------+--------+
    short *ptr2 = (short*)ptr;
    Code:
    +--------+--------+--------+--------+
    |00000110|xxxxxxxx|        |        |
    +--------+--------+--------+--------+
    *ptr2 = 9;
    Code:
    +--------+--------+--------+--------+
    |00001001|00000000|        |        |
    +--------+--------+--------+--------+
    ptr2++;
    Code:
    +--------+--------+--------+--------+
    |        |        |xxxxxxxx|xxxxxxxx|
    +--------+--------+--------+--------+
    *ptr2 = 10;
    Code:
    +--------+--------+--------+--------+
    |        |        |00001010|00000000|
    +--------+--------+--------+--------+
    long *ptr3 = (long*)ptr;
    Code:
    +--------+--------+--------+--------+
    |00000110|00000000|00001010|00000000|
    +--------+--------+--------+--------+
    Last edited by iMalc; 08-22-2008 at 01:59 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  2. strings Vs. Char pointers
    By aijazbaig1 in forum C Programming
    Replies: 49
    Last Post: 02-13-2008, 08:51 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. Sending non-(char*) data
    By Nazca in forum Networking/Device Communication
    Replies: 12
    Last Post: 07-31-2005, 03:39 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

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