Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pointer addition with structs

Reply
Thread Tools

pointer addition with structs

 
 
andreyvul
Guest
Posts: n/a
 
      01-02-2008
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));
}

 
Reply With Quote
 
 
 
 
cr88192
Guest
Posts: n/a
 
      01-02-2008

"andreyvul" <> wrote in message
news:c1751112-7eb1-4b81-a3e2-...
> 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));




 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      01-02-2008
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,0xa6 7f6aaa,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 1utchar(a[i&15]);break;}}}
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      01-02-2008
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.
 
Reply With Quote
 
andreyvul
Guest
Posts: n/a
 
      01-02-2008
On Jan 1, 10:04 pm, Martin Ambuhl <mamb...@earthlink.net> 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?
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      01-02-2008
andreyvul wrote:
> On Jan 1, 10:04 pm, Martin Ambuhl <mamb...@earthlink.net> 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.
 
Reply With Quote
 
andreyvul
Guest
Posts: n/a
 
      01-02-2008
On Jan 1, 10:18 pm, Martin Ambuhl <mamb...@earthlink.net> wrote:
> andreyvul wrote:
> > On Jan 1, 10:04 pm, Martin Ambuhl <mamb...@earthlink.net> 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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Packed structs vs. unpacked structs: what's the difference? Daniel Rudy C Programming 15 04-10-2006 08:10 AM
Array of structs instead of an array with pointers to structs? Paminu C Programming 5 10-11-2005 07:18 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
const structs in other structs Chris Hauxwell C Programming 6 04-27-2004 07:03 PM
structs with fields that are structs Patricia Van Hise C Programming 5 04-05-2004 01:37 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57