Error E2349 tester.c 29: Nonportable pointer conversion

B

Ben Midgley

Please have a look at the code below

My problem is the error message "Nonportable pointer conversion", so far as
I can tell there is no pointer conversion occuring here so I am assuming it
is something implicit. The error is associated with the assignment {
&MenuItemList }, when delcaring the mit_ZoneSelectMenu structure.

I am using borland bcc32 the command line compiler which seems pretty good,
I just dont understand this issue.



#include <stdio>

#define _LEN_A_ 16 // this is minimum

typedef struct {
u8 ItemName[(_LEN_A_ - 1)];
boolean (*ItemHandler)( u8 Token );
} MenuItemT;

typedef struct {
u8 string_a[_LEN_A_];
MenuItemT * pItemList;
u8 ItemCount;
} MenuT;

static boolean fn_a( u8 token );
static boolean fn_b( u8 token );

static volatile MenuItemT MenuItemList[] = {
/* "Item Name ", Item Handler Function */
{ "Item A" , &fn_a }, // Zone A
{ "Item B" , &fn_b }, // Zone B
{ NULL , NULL }
};

static volatile MenuT mit_ZoneSelectMenu[] = {
{ "Menu Title " },
{ &MenuItemList },
{ 2 },
};


void main( void )
{
printf("hello world");
}

static boolean fn_a( u8 token )
{
printf("fn_a called The token is %c",token);

return true;
}

static boolean fn_b( u8 token )
{
printf("fn_b called The token is %c",token);

return true;
}
 
E

Eric Sosman

Ben said:
Please have a look at the code below

My problem is the error message "Nonportable pointer conversion", so far as
I can tell there is no pointer conversion occuring here so I am assuming it
is something implicit. The error is associated with the assignment {
&MenuItemList }, when delcaring the mit_ZoneSelectMenu structure.

I am using borland bcc32 the command line compiler which seems pretty good,
I just dont understand this issue.

[...]

typedef struct {
u8 string_a[_LEN_A_];
MenuItemT * pItemList;

This element has the type "pointer to MenuItemT."
u8 ItemCount;
} MenuT;

static boolean fn_a( u8 token );
static boolean fn_b( u8 token );

static volatile MenuItemT MenuItemList[] = {
/* "Item Name ", Item Handler Function */
{ "Item A" , &fn_a }, // Zone A
{ "Item B" , &fn_b }, // Zone B
{ NULL , NULL }
};

static volatile MenuT mit_ZoneSelectMenu[] = {
{ "Menu Title " },
{ &MenuItemList },

This initializer has the type "pointer to array
of MenuItemT," which is not the type of the element it
initializes. Drop the `&', and read Question 6.12 in
the comp.lang.c Frequently Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

(Read Questions 6.12 and 6.8 as well -- oh, what the
heck: read all of Section 6; it's good for you.)
 
M

Mike Wahler

Ben Midgley said:
Please have a look at the code below

My problem is the error message "Nonportable pointer conversion", so far as
I can tell there is no pointer conversion occuring here so I am assuming it
is something implicit. The error is associated with the assignment {
&MenuItemList }, when delcaring the mit_ZoneSelectMenu structure.

I am using borland bcc32 the command line compiler which seems pretty good,
I just dont understand this issue.

I point out your problem with this issue below,
but first, some others (some appear more than
once, I'll only point out the first occurrence
of each):
#include <stdio>

There is no standard header <stdio> in C. Perhaps
you meant said:
#define _LEN_A_ 16 // this is minimum

Global names beginning with an underscore are reserved to the
implementation
typedef struct {
u8 ItemName[(_LEN_A_ - 1)];

Standard C has no such type as 'u8'.
boolean (*ItemHandler)( u8 Token );

Standard C has no such type as 'boolean'.
} MenuItemT;

typedef struct {
u8 string_a[_LEN_A_];
MenuItemT * pItemList;
u8 ItemCount;
} MenuT;

static boolean fn_a( u8 token );
static boolean fn_b( u8 token );

static volatile MenuItemT MenuItemList[] = {
/* "Item Name ", Item Handler Function */
{ "Item A" , &fn_a }, // Zone A
{ "Item B" , &fn_b }, // Zone B
{ NULL , NULL }

There's no declaration of 'NULL' in scope. The C
header which declares it is said:
};

static volatile MenuT mit_ZoneSelectMenu[] = {
{ "Menu Title " },
{ &MenuItemList },

OK here we go:

The name of an array in this context has type
'pointer to element type', in this case 'pointer
to MenuItemT' ('MenuItemT *').

The expression '&MenuItemList' has type 'pointer to
array of four MenuItemT' ( 'MenuItemT(*)[4]' ).
This is not the same as above.

I think you want for your second item:

MenuItemList

not

&MenuItemList
{ 2 },
};


void main( void )

'main()' is required to have return type 'int'.
{
printf("hello world");

No prototype for 'printf()' in scope. THe C header
which declares it is <stdio.h>

You should also terminate your output with a newline
character ('\n') or you're not guranteed the output
will appear.


Since 'main()' returns an 'int', return one:

return 0;
}

static boolean fn_a( u8 token )
{
printf("fn_a called The token is %c",token);

return true;

C has no keyword 'true', and you've not defined any
identifier with that name.
}

static boolean fn_b( u8 token )
{
printf("fn_b called The token is %c",token);

return true;
}

Perhaps you're using some nonstandard bastard version
of C. The above is far from ISO standard C, the topic
here.

-Mike
 
B

Ben Midgley

I am still getting that error.

Thanks for all the pointers, it is standard C I am using however I forgot to
include some details held in an included header file which may help clear up
a number of the points you raise. Sorry it was dumb of me not to spot that
they were missing. All perfectly valid though, I did miss the main return
type (I am used to embedded apps) and the use of underscore in defines was
news to me but I will keep it in mind in the future.

Here is the revised version which includes the below change. I just can't
shift the error message.

{ MenuItemList }, // changed from &MenuItemList, but also tried
&MenuItemList[0]

Thanks for your help

Ben

// -- Start Here

#include <stdio.h>

#define LEN_A_ 16 // this is minimum

typedef unsigned char u8;
typedef signed char s8;

typedef unsigned short u16;
typedef signed short s16;

typedef unsigned long u32;
typedef signed long s32;

typedef float f32;
typedef double f64;

typedef unsigned char boolean;

#define true 1
#define false 0

typedef unsigned long AddressT;

#define NULL 0

typedef struct {
u8 ItemName[(LEN_A_ - 1)];
boolean (*ItemHandler)( u8 Token );
} MenuItemT;

typedef struct {
u8 string_a[LEN_A_];
MenuItemT * pItemList;
u8 ItemCount;
} MenuT;

static boolean fn_a( u8 token );
static boolean fn_b( u8 token );

static volatile MenuItemT MenuItemList[] = {
/* "Item Name ", Item Handler Function */
{ "Item A" , &fn_a }, // Zone A
{ "Item B" , &fn_b }, // Zone B
{ NULL , NULL }
};

static volatile MenuT mit_ZoneSelectMenu[] = {
{ "Menu Title " },
{ MenuItemList }, // changed from &MenuItemList, but also tried
&MenuItemList[0]
{ 2 },
};

int main( void )
{
printf("hello world");

return 0;
}

static boolean fn_a( u8 token )
{
printf("fn_a called The token is %c",token);

return true;
}

static boolean fn_b( u8 token )
{
printf("fn_b called The token is %c",token);

return true;
}
 
B

Ben Midgley

Thanks for the pointer Eric, the FAQ seems to indicate that the use of the &
is the problem in the inititaliser { &MenuItemList }, which I can
modify to get { MenuItemList }, which gets me the same error, or I
can explicity dereference the first item in the array (same as the array
name) { &MenuItemList[0] }, and still I get the same error.

I have included the whole file in my response to Mike and still I cannot get
a build. Have I missed something ?



Eric Sosman said:
Ben said:
Please have a look at the code below

My problem is the error message "Nonportable pointer conversion", so far as
I can tell there is no pointer conversion occuring here so I am assuming it
is something implicit. The error is associated with the assignment {
&MenuItemList }, when delcaring the mit_ZoneSelectMenu structure.

I am using borland bcc32 the command line compiler which seems pretty good,
I just dont understand this issue.

[...]

typedef struct {
u8 string_a[_LEN_A_];
MenuItemT * pItemList;

This element has the type "pointer to MenuItemT."
u8 ItemCount;
} MenuT;

static boolean fn_a( u8 token );
static boolean fn_b( u8 token );

static volatile MenuItemT MenuItemList[] = {
/* "Item Name ", Item Handler Function */
{ "Item A" , &fn_a }, // Zone A
{ "Item B" , &fn_b }, // Zone B
{ NULL , NULL }
};

static volatile MenuT mit_ZoneSelectMenu[] = {
{ "Menu Title " },
{ &MenuItemList },

This initializer has the type "pointer to array
of MenuItemT," which is not the type of the element it
initializes. Drop the `&', and read Question 6.12 in
the comp.lang.c Frequently Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

(Read Questions 6.12 and 6.8 as well -- oh, what the
heck: read all of Section 6; it's good for you.)
 
M

Mike Wahler

Ben Midgley said:
I am still getting that error.

Thanks for all the pointers, it is standard C I am using however I forgot to
include some details held in an included header file which may help clear up
a number of the points you raise. Sorry it was dumb of me not to spot that
they were missing. All perfectly valid though, I did miss the main return
type (I am used to embedded apps) and the use of underscore in defines was
news to me but I will keep it in mind in the future.

Here is the revised version which includes the below change. I just can't
shift the error message.

{ MenuItemList }, // changed from &MenuItemList, but also tried
&MenuItemList[0]

You also have a 'number of initializers' problem with
your array of structs, which I missed before.

More below.
Thanks for your help

Ben

// -- Start Here

#include <stdio.h>

#define LEN_A_ 16 // this is minimum

typedef unsigned char u8;
typedef signed char s8;

typedef unsigned short u16;
typedef signed short s16;

typedef unsigned long u32;
typedef signed long s32;

typedef float f32;
typedef double f64;

typedef unsigned char boolean;

#define true 1
#define false 0

typedef unsigned long AddressT;

#define NULL 0

DON'T DO THIS!!! The name "NULL" is defined by the library,
typedef struct {
u8 ItemName[(LEN_A_ - 1)];
boolean (*ItemHandler)( u8 Token );
} MenuItemT;

typedef struct {
u8 string_a[LEN_A_];
MenuItemT * pItemList;
u8 ItemCount;
} MenuT;

static boolean fn_a( u8 token );
static boolean fn_b( u8 token );

static volatile MenuItemT MenuItemList[] = {

I'm not sure you understand the meanings of
'static' and 'volatile'. (Note that 'static'
has different meanings depending upon context)

I've created a compilable verson of your code
below, and removed the cases where specifying
'static' is redundant. I've left in the 'volatile',
(and added it in one place where it's needed if you
continue to use it) but I seriously doubt you need it
at all.

Corrected code at the end of this message.
/* "Item Name ", Item Handler Function */
{ "Item A" , &fn_a }, // Zone A
{ "Item B" , &fn_b }, // Zone B
{ NULL , NULL }
};

static volatile MenuT mit_ZoneSelectMenu[] = {
{ "Menu Title " },
{ MenuItemList }, // changed from &MenuItemList, but also tried
&MenuItemList[0]
{ 2 },
};

int main( void )
{
printf("hello world");

return 0;
}

static boolean fn_a( u8 token )
{
printf("fn_a called The token is %c",token);

return true;
}

static boolean fn_b( u8 token )
{
printf("fn_b called The token is %c",token);

return true;
}


The following will compile. Compare it very carefully
with yours, I've made many changes, some rather subtle.

Note that this code is compilable, but I did not try to
run it. It may or may not do exactly what you intend.


#include <stdio.h>

#define LEN_A_ 16 // this is minimum

typedef unsigned char u8;
typedef signed char s8;

typedef unsigned short u16;
typedef signed short s16;

typedef unsigned long u32;
typedef signed long s32;

typedef float f32;
typedef double f64;

typedef unsigned char boolean;

#define true 1
#define false 0

typedef unsigned long AddressT;

typedef struct {
u8 ItemName[LEN_A_ - 1];
boolean (*ItemHandler)(u8 Token);
} MenuItemT;

typedef struct {
u8 string_a[LEN_A_];
volatile MenuItemT * pItemList;
u8 ItemCount;
} MenuT;

static boolean fn_a( u8 token );
static boolean fn_b( u8 token );

volatile MenuItemT MenuItemList[] = {
/* "Item Name", Item Handler Function */
{ "Item A", fn_a}, // Zone A
{ "Item B", fn_b}, // Zone B
{ "", NULL}
};

volatile MenuT mit_ZoneSelectMenu[] = {
{ "Menu Title ", MenuItemList, 2},
};

int main(void)
{
printf("hello world\n");
return 0;
}

static boolean fn_a(u8 token)
{
printf("fn_a called The token is %c\n", token);
return true;
}

static boolean fn_b(u8 token)
{
printf("fn_b called The token is %c\n",token);
return true;
}

-Mike
 
B

Ben Midgley

Thanks mike, thats seen it - overzelous ( ..read wrong ) use of parenthesis
in the intitialiser right ?

There is no intention for this code, its a sample which reproduces an error
on a much more convaluted peice of code which I can now compile and now
works correctly (cheers). There was no point posting the whole thing.... no
one would have read 12,000 lines, right ? .

My use of static in all the instances below is just to limit scope. My use
of volatile - habit from working to misra, but it does no harm ....other
than maybe cost a little performace on a missed optimisation.

Thanks a million,

Ben

Mike Wahler said:
Ben Midgley said:
I am still getting that error.

Thanks for all the pointers, it is standard C I am using however I
forgot
to
include some details held in an included header file which may help
clear
up
a number of the points you raise. Sorry it was dumb of me not to spot that
they were missing. All perfectly valid though, I did miss the main return
type (I am used to embedded apps) and the use of underscore in defines was
news to me but I will keep it in mind in the future.

Here is the revised version which includes the below change. I just can't
shift the error message.

{ MenuItemList }, // changed from &MenuItemList, but also tried
&MenuItemList[0]

You also have a 'number of initializers' problem with
your array of structs, which I missed before.

More below.
Thanks for your help

Ben

// -- Start Here

#include <stdio.h>

#define LEN_A_ 16 // this is minimum

typedef unsigned char u8;
typedef signed char s8;

typedef unsigned short u16;
typedef signed short s16;

typedef unsigned long u32;
typedef signed long s32;

typedef float f32;
typedef double f64;

typedef unsigned char boolean;

#define true 1
#define false 0

typedef unsigned long AddressT;

#define NULL 0

DON'T DO THIS!!! The name "NULL" is defined by the library,
typedef struct {
u8 ItemName[(LEN_A_ - 1)];
boolean (*ItemHandler)( u8 Token );
} MenuItemT;

typedef struct {
u8 string_a[LEN_A_];
MenuItemT * pItemList;
u8 ItemCount;
} MenuT;

static boolean fn_a( u8 token );
static boolean fn_b( u8 token );

static volatile MenuItemT MenuItemList[] = {

I'm not sure you understand the meanings of
'static' and 'volatile'. (Note that 'static'
has different meanings depending upon context)

I've created a compilable verson of your code
below, and removed the cases where specifying
'static' is redundant. I've left in the 'volatile',
(and added it in one place where it's needed if you
continue to use it) but I seriously doubt you need it
at all.

Corrected code at the end of this message.
/* "Item Name ", Item Handler Function */
{ "Item A" , &fn_a }, // Zone A
{ "Item B" , &fn_b }, // Zone B
{ NULL , NULL }
};

static volatile MenuT mit_ZoneSelectMenu[] = {
{ "Menu Title " },
{ MenuItemList }, // changed from &MenuItemList, but also tried
&MenuItemList[0]
{ 2 },
};

int main( void )
{
printf("hello world");

return 0;
}

static boolean fn_a( u8 token )
{
printf("fn_a called The token is %c",token);

return true;
}

static boolean fn_b( u8 token )
{
printf("fn_b called The token is %c",token);

return true;
}


The following will compile. Compare it very carefully
with yours, I've made many changes, some rather subtle.

Note that this code is compilable, but I did not try to
run it. It may or may not do exactly what you intend.


#include <stdio.h>

#define LEN_A_ 16 // this is minimum

typedef unsigned char u8;
typedef signed char s8;

typedef unsigned short u16;
typedef signed short s16;

typedef unsigned long u32;
typedef signed long s32;

typedef float f32;
typedef double f64;

typedef unsigned char boolean;

#define true 1
#define false 0

typedef unsigned long AddressT;

typedef struct {
u8 ItemName[LEN_A_ - 1];
boolean (*ItemHandler)(u8 Token);
} MenuItemT;

typedef struct {
u8 string_a[LEN_A_];
volatile MenuItemT * pItemList;
u8 ItemCount;
} MenuT;

static boolean fn_a( u8 token );
static boolean fn_b( u8 token );

volatile MenuItemT MenuItemList[] = {
/* "Item Name", Item Handler Function */
{ "Item A", fn_a}, // Zone A
{ "Item B", fn_b}, // Zone B
{ "", NULL}
};

volatile MenuT mit_ZoneSelectMenu[] = {
{ "Menu Title ", MenuItemList, 2},
};

int main(void)
{
printf("hello world\n");
return 0;
}

static boolean fn_a(u8 token)
{
printf("fn_a called The token is %c\n", token);
return true;
}

static boolean fn_b(u8 token)
{
printf("fn_b called The token is %c\n",token);
return true;
}

-Mike
 
M

Mark L Pappin

Ben Midgley said:
Thanks mike, thats seen it - overzelous ( ..read wrong ) use of
parenthesis in the intitialiser right ?

Minor terminology nitpick:
() are usually called 'parentheses' (just one of them would be 'a
parenthesis')
{} are usually called 'braces' or 'curly brackets'.

But yes, arrays of derived types care very much about how many levels
of braces are wrapped around their initializers.
There is no intention for this code, its a sample which reproduces
an error on a much more convaluted peice of code which I can now
compile and now works correctly (cheers). There was no point posting
the whole thing.... no one would have read 12,000 lines, right ? .

If only all c.l.c posters would be as considerate. Another thing you
might try in future: don't top-post.
My use of volatile - habit from working to misra,

The only places 'volatile' can help, as far as I can see, are if

- the object designates a memory-mapped device or something which
acts like one, or

- the object can be read or written asynchronously by some other
entity (often: interrupt service routines).

Are there any uses for 'volatile' beyond these two?

BTW, that's the second time in the last couple months I've seen
someone quote MISRA as justification for questionable coding. Yours
is less severe - as you say:
but it does no harm ....other
than maybe cost a little performace on a missed optimisation.

The other person (a customer) wanted a way to convert an integer to a
pointer without casting an integer to a pointer (which is the method
one should use if one's compiler defines its results, surely?), as
MISRA says that's verboten. Apparently MISRA has no such objection to
inline assembly code that does exactly the same thing. I'm not sure
how this was conducive to Software Reliability.

mlp
 
E

Eric Sosman

Mark said:
[...]
The only places 'volatile' can help, as far as I can see, are if

- the object designates a memory-mapped device or something which
acts like one, or

- the object can be read or written asynchronously by some other
entity (often: interrupt service routines).

Are there any uses for 'volatile' beyond these two?

Yes: see the description of setjmp().
 
O

Old Wolf

Ben Midgley wrote:

[I have corrected a typo,included a typedef, and formatted]
#include <stdio.h>

#define _LEN_A_ 16 // this is minimum
typedef unsigned char u8, boolean;

typedef struct {
u8 ItemName [_LEN_A_ - 1];
boolean (*ItemHandler)( u8 Token );
} MenuItemT;

typedef struct {
u8 string_a [_LEN_A_];
MenuItemT * pItemList;
u8 ItemCount;
} MenuT;

static boolean fn_a( u8 token );
static boolean fn_b( u8 token );

static volatile MenuItemT MenuItemList[] = {
{ "Item A" , &fn_a }, // Zone A
{ "Item B" , &fn_b }, // Zone B
{ NULL , NULL }
};

NULL may be defined as (void *)0, which is not a
valid initializer for an array of chars. You should
either use 0, or "".

Also, I'm not entirely sure that (void *)0 is a valid
initializer for a function pointer (but it probably is).
};

static volatile MenuT mit_ZoneSelectMenu[] = {
{ "Menu Title " },

That was the initializer for mit_ZoneSelectMenu[0]
{ &MenuItemList },

That was the initializer for mit_ZoneSelectMenu[1].
&MenuListItem can't be converted to a char array.

That was the initializer for mit_ZoneSelectMenu[2].
2 can't be converted to a char array.

You had too many braces, I think you meant to write:

static volatile MenuT mit_ZoneSelectMenu[] = {
{ "Menu Title ", &MenuItemList, 2 }
};

Now the problem is that mit_ZoneSelectMenu[0].pItemList
has type (MenuItemT *), but &MenuItemList has type
(MenuItemT volatile (*) [3]). You need to drop the
ampersand, and modify the definition of MenuT
to have: MenuItemT volatile *pItemList;
void main( void )

int main(void), if you want to be portable.
 
L

Lawrence Kirby

On Mon, 17 Jan 2005 13:23:14 -0800, Old Wolf wrote:

....
Also, I'm not entirely sure that (void *)0 is a valid
initializer for a function pointer (but it probably is).

Normally you cannot convert implicitly between void * and function pointer
types, but any null pointer constant can be converted implicitly to any
pointer type. So (void *)0 is a valid initialiser for a function pointer
where, for example, (void *)(void *)0 isn't.

Lawrence
 
D

Dave Thompson

There is no standard header <stdio> in C. Perhaps
you meant <stdio.h>


There's no declaration of 'NULL' in scope. The C
header which declares it is <stdlib.h>
And several others, including stdio.h, which you correctly diagnosed
he meant to #include and the OP's code downthread confirms.

C has no keyword 'true', and you've not defined any
identifier with that name.
C99 with <stdbool.h> does -- well, not a keyword, a predefined macro,
which is close enough -- but from the OP's code downthread he is in
fact using his own, as many people have done for a long time in C89.

- David.Thompson1 at worldnet.att.net
 

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,773
Messages
2,569,594
Members
45,126
Latest member
FastBurnketoIngredients
Top