pointer addition with structs

A

andreyvul

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));
}
 
C

cr88192

andreyvul said:
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?

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));
 
B

Ben Pfaff

andreyvul said:
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?
 
M

Martin Ambuhl

andreyvul said:
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.
 
A

andreyvul

andreyvul said:
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?
 
M

Martin Ambuhl

andreyvul said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top