pointer addition with structs

Discussion in 'C Programming' started by andreyvul, Jan 2, 2008.

  1. andreyvul

    andreyvul Guest

    gcc 3.4 (mingw) says error: invalid operands to binary + when I try to
    to pointer arithmetic to calculate offset of a certain element.
    It compiles without errors or warnings in MSVC.

    Any clue why?

    code (fully compilable snippet):

    /* Disable MSVC W C4996 */
    #ifdef _MSC_VER
    #define _CRT_SECURE_NO_DEPRECATE
    #endif

    #include <stdio.h>
    #ifndef _MSC_VER
    #include <stdint.h>
    #else /* _MSC_VER */
    typedef unsigned __int32 uint32_t;
    #endif /* _MSC_VER */

    /*
    * Sub-cheat (#)
    */
    typedef struct sub_s {
    /* Sub-cheat title */
    char *label;
    /* An array of 32-bit numbers containing an address or a value */
    uint32_t *data;
    /* The number of addresses and values */
    size_t n_data;
    } sub_t;

    /*
    * Main cheat (@)
    */
    typedef struct cheat_s {
    /* Cheat title */
    char *label;
    /* Array of sub-cheat(s) */
    sub_t *sub;
    /* The number of sub-cheat(s) */
    size_t n_subs;
    } cheat_t;

    /* Global pointer to (main) cheats */
    cheat_t *cheat;
    /* The number of (main) cheats */
    size_t n_cheats;

    void foo() {
    printf("debug: cheat[n_cheats - 1] @ %p\n", cheat + (cheat_t *)
    (n_cheats - 1));
    }
    andreyvul, Jan 2, 2008
    #1
    1. Advertising

  2. andreyvul

    cr88192 Guest

    "andreyvul" <> wrote in message
    news:...
    > gcc 3.4 (mingw) says error: invalid operands to binary + when I try to
    > to pointer arithmetic to calculate offset of a certain element.
    > It compiles without errors or warnings in MSVC.
    >
    > Any clue why?
    >


    <snip>

    > typedef struct cheat_s {
    > /* Cheat title */
    > char *label;
    > /* Array of sub-cheat(s) */
    > sub_t *sub;
    > /* The number of sub-cheat(s) */
    > size_t n_subs;
    > } cheat_t;
    >
    > /* Global pointer to (main) cheats */
    > cheat_t *cheat;
    > /* The number of (main) cheats */
    > size_t n_cheats;
    >
    > void foo() {
    > printf("debug: cheat[n_cheats - 1] @ %p\n", cheat + (cheat_t *)
    > (n_cheats - 1));
    > }
    >


    note that it is not valid to add two pointers together like this.
    you can add a pointer and an integer, but not two pointers.

    so, altering:
    > printf("debug: cheat[n_cheats - 1] @ %p\n", cheat + (n_cheats -
    > 1));
    cr88192, Jan 2, 2008
    #2
    1. Advertising

  3. andreyvul

    Ben Pfaff Guest

    andreyvul <> writes:

    > gcc 3.4 (mingw) says error: invalid operands to binary + when I try to
    > to pointer arithmetic to calculate offset of a certain element.


    [...]

    > cheat_t *cheat;

    [...]
    > printf("debug: cheat[n_cheats - 1] @ %p\n", cheat + (cheat_t *)
    > (n_cheats - 1));


    You can subtract two pointers, or add an integer to a pointer,
    but adding two pointers makes no sense. What are you really
    trying to do?
    --
    char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
    ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
    2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
    Ben Pfaff, Jan 2, 2008
    #3
  4. andreyvul wrote:
    > gcc 3.4 (mingw) says error: invalid operands to binary + when I try to
    > to pointer arithmetic to calculate offset of a certain element.
    > It compiles without errors or warnings in MSVC.
    >
    > Any clue why?


    Why MSVC fails to diagnose your error is something to take up with them.
    The offending lines
    > printf("debug: cheat[n_cheats - 1] @ %p\n", cheat + (cheat_t *)
    > (n_cheats - 1));

    attempts to add a pointer to a pointer, which makes no sense, even if
    your cast of an int to a pointer had any portably defined meaning.
    Replace it with the legal
    printf("debug: cheat[n_cheats - 1] @ %p\n",
    (void *) (cheat + n_cheats - 1));

    Note that %p expects a pointer-to-void.
    Martin Ambuhl, Jan 2, 2008
    #4
  5. andreyvul

    andreyvul Guest

    On Jan 1, 10:04 pm, Martin Ambuhl <> wrote:
    > andreyvul wrote:
    > > gcc 3.4 (mingw) says error: invalid operands to binary + when I try to
    > > to pointer arithmetic to calculate offset of a certain element.
    > > It compiles without errors or warnings in MSVC.

    >
    > > Any clue why?

    >
    > Why MSVC fails to diagnose your error is something to take up with them.
    > The offending lines> printf("debug: cheat[n_cheats - 1] @ %p\n", cheat + (cheat_t *)
    > > (n_cheats - 1));

    >
    > attempts to add a pointer to a pointer, which makes no sense, even if
    > your cast of an int to a pointer had any portably defined meaning.
    > Replace it with the legal
    > printf("debug: cheat[n_cheats - 1] @ %p\n",
    > (void *) (cheat + n_cheats - 1));
    >
    > Note that %p expects a pointer-to-void.


    My 3 A.M. C is kinda rusty. Shouldn't the integer be multiplied by
    sizeof before addition?
    andreyvul, Jan 2, 2008
    #5
  6. andreyvul wrote:
    > On Jan 1, 10:04 pm, Martin Ambuhl <> wrote:


    >> Replace it with the legal
    >> printf("debug: cheat[n_cheats - 1] @ %p\n",
    >> (void *) (cheat + n_cheats - 1));
    >>
    >> Note that %p expects a pointer-to-void.

    >
    > My 3 A.M. C is kinda rusty. Shouldn't the integer be multiplied by
    > sizeof before addition?


    No.
    Martin Ambuhl, Jan 2, 2008
    #6
  7. andreyvul

    andreyvul Guest

    On Jan 1, 10:18 pm, Martin Ambuhl <> wrote:
    > andreyvul wrote:
    > > On Jan 1, 10:04 pm, Martin Ambuhl <> wrote:
    > >> Replace it with the legal
    > >> printf("debug: cheat[n_cheats - 1] @ %p\n",
    > >> (void *) (cheat + n_cheats - 1));

    >
    > >> Note that %p expects a pointer-to-void.

    >
    > > My 3 A.M. C is kinda rusty. Shouldn't the integer be multiplied by
    > > sizeof before addition?

    >
    > No.


    So the original code was redundant AND illegal?
    andreyvul, Jan 2, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    635
    Al Bowers
    Apr 5, 2004
  2. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    556
    Chris Hauxwell
    Apr 27, 2004
  3. Paminu
    Replies:
    5
    Views:
    639
    Eric Sosman
    Oct 11, 2005
  4. Daniel Rudy
    Replies:
    15
    Views:
    1,390
    Keith Thompson
    Apr 10, 2006
  5. Tuan  Bui
    Replies:
    14
    Views:
    470
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page