How can I get rid of this C4047 Indirection warning?

P

pkirk25

I like to keep warnings to a minimum and am find this one cropping up a
lot.

struct auction_bargains bargainList[LIST_SIZE];
struct auction_bargains *pBargainList = &bargainList;

I am creating an array of a fixed size and pBargainList allows me to
access it from any function.

C:\Documents and Settings\patrick\My
Documents\projects\DayTrader\FindBargains.c(13) : warning C4047:
'initializing' : 'struct auction_bargains *' differs in levels of
indirection from 'struct auction_bargains (*)[5]'

Should I worry?
 
J

Jean-Marc Bourguet

pkirk25 said:
I like to keep warnings to a minimum and am find this one cropping up a
lot.

struct auction_bargains bargainList[LIST_SIZE];
struct auction_bargains *pBargainList = &bargainList;

I am creating an array of a fixed size and pBargainList allows me to
access it from any function.

C:\Documents and Settings\patrick\My
Documents\projects\DayTrader\FindBargains.c(13) : warning C4047:
'initializing' : 'struct auction_bargains *' differs in levels of
indirection from 'struct auction_bargains (*)[5]'

Should I worry?

Either

struct auction_bargains *pBargainList = &bargainList[0];

or

struct auction_bargains *pBargainList = bargainList;

gives you a pointer to the first element of the table. What &bargainList
gives you a pointer to the whole table, and the compiler rightly complain.

Yours,
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

pkirk25 said:
I like to keep warnings to a minimum and am find this one cropping up a
lot.

struct auction_bargains bargainList[LIST_SIZE];
struct auction_bargains *pBargainList = &bargainList;

I am creating an array of a fixed size and pBargainList allows me to
access it from any function.

C:\Documents and Settings\patrick\My
Documents\projects\DayTrader\FindBargains.c(13) : warning C4047:
'initializing' : 'struct auction_bargains *' differs in levels of
indirection from 'struct auction_bargains (*)[5]'

Should I worry?
Yes.

Perhaps you want to point it at the first element in the bargainList
array. That would be the address of its first element(which is at
index 0)

struct auction_bargains *pBargainList = &bargainList[0];

This is the same as
struct auction_bargains *pBargainList = bargainList;
 
S

shaanxxx

pkirk25 said:
I like to keep warnings to a minimum and am find this one cropping up a
lot.

struct auction_bargains bargainList[LIST_SIZE];
struct auction_bargains *pBargainList = &bargainList;

change above code to following code .

struct auction_bargains bargainList[LIST_SIZE];
struct auction_bargains *pBargainList = bargainList;
 
R

Richard Heathfield

pkirk25 said:
I like to keep warnings to a minimum and am find this one cropping up a
lot.

struct auction_bargains bargainList[LIST_SIZE];
struct auction_bargains *pBargainList = &bargainList;

Remove the &, or change pBargainList's type to struct (auction_bargains
*)[LIST_SIZE]. One or the other, but not both. Which you choose depends on
factors which you have not told us about.
I am creating an array of a fixed size and pBargainList allows me to
access it from any function.

C:\Documents and Settings\patrick\My
Documents\projects\DayTrader\FindBargains.c(13) : warning C4047:
'initializing' : 'struct auction_bargains *' differs in levels of
indirection from 'struct auction_bargains (*)[5]'

Should I worry?

Yes. Alternatively, remove the &, or change the type as I indicated.
 
C

Chris Dollin

pkirk25 said:
I like to keep warnings to a minimum and am find this one cropping up a
lot.

struct auction_bargains bargainList[LIST_SIZE];
struct auction_bargains *pBargainList = &bargainList;

I am creating an array of a fixed size and pBargainList allows me to
access it from any function.

In this snipped, `pBargainList` and `bargainList` have equally global
(which is to say, horribly excessively exposed) access.

[Style query: why camelCase the variable names but not the struct
names?]
C:\Documents and Settings\patrick\My
Documents\projects\DayTrader\FindBargains.c(13) : warning C4047:
'initializing' : 'struct auction_bargains *' differs in levels of
indirection from 'struct auction_bargains (*)[5]'

Should I worry?

No, worrying is pointless - easier to fix the code. `pBargainList`
should be initialised with a pointer-to-struct-auction_bargains,
not a pointer-to-an-array-of-LIST_SIZE-struct-auction_bargains.

If you want to keep the type of `pBargainList`, write whichever
of

struct auction_bargains *pBargainList = bargainList;
struct auction_bargains *pBargainList = &bargainList[0];

fits your stylistic preferences.

Arrays are not pointers. Pointers are not arrays. The address
of an array isn't the same thing as the address of its first
element. See the FAQ for a more detailed discussion.
 

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

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top