Thread: Pointer arithmetic

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    5

    Pointer arithmetic

    What's the correct way to do pointer arithmetic in this example ?

    Say I store a struct in a buffer at some offset and then want to get a pointer to it. e.g.

    Code:
    int offset;
    sometype mystruct, *ptr;
    char buff[32768];
    
    memcpy(buff[offset], mystruct, sizeof(mystruct));
    Is the pointer

    ptr = (mystruct *)buffer[offset];

    or

    ptr = (mystruct *)buffer + offset;

    or

    ptr = (mystruct *)((&buffer[0]) + offset);

    or something else ? I'm getting confused - and coredumps ...

    thks

  2. #2
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    Code:
    int offset;
    sometype mystruct, *ptr;
    char buff[32768];
    
    memcpy(&buff[offset], &mystruct, sizeof(sometype ));
    or

    Code:
    int offset;
    sometype mystruct, *ptr;
    char buff[32768];
    
    memcpy(bufd + offset, &mystruct, sizeof(sometype ));

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    5
    Sorry - didn't make myself clear enough.

    Assume you can't change the code I posted, but I need a pointer to the place in the buffer where the stored struct starts.

    Is this legal ??

    ptr = (sometype *)buffer[offset];

    Should I then be able to write something like

    printf("%d\n", ptr->some_int_member);

    ??

    TIA

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Assume you can't change the code I posted
    Code:
    memcpy(buff[offset], mystruct, sizeof(mystruct));
    But it's got bugs.
    Code:
    memcpy(&buff[offset], &mystruct, sizeof(mystruct));
    You see, buff[offset] is a char, not a pointer; and mystruct is a struct, not a pointer. Since memcpy takes pointers for its first two arguments, if we leave it the way you posted, we'd likely to be writing to wherever from la-la land.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct mytype
    {
        char   name[20];
        int    i;
        double d;
    };
    
    int main(void)
    {
        char buffer[128];
        struct mytype myvar = { "name", -1, 3.14 }, *ptr;
        int offset = 24;
    
        ptr = (struct mytype*)&buffer[offset];
        memcpy(ptr, &myvar, sizeof(myvar));
    
        printf("ptr->name = \"%s\"\n", ptr->name);
        printf("ptr->i    = %d\n",     ptr->i);
        printf("ptr->d    = %lf\n",    ptr->d);
    
        return 0;
    }
    
    /* my output
    ptr->name = "name"
    ptr->i    = -1
    ptr->d    = 3.140000
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    5
    Thanks to all for the suggestions and apologies for posting c**p code. I didn't want to post hundreds of lines of real code and came up with a buggy example :-(

    The alignment problem looks to be the most probable cause of my core dumps, so I'll have to find some other way of doing this.

    D
    #

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  3. Pointers, arithmetic, and order of operation.
    By Aerie in forum C Programming
    Replies: 4
    Last Post: 04-19-2005, 07:35 AM
  4. Replies: 41
    Last Post: 07-04-2004, 03:23 PM