By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,971 Members | 1,732 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,971 IT Pros & Developers. It's quick & easy.

fmemopen: assignment makes pointer from integer without a cast

P: n/a
The following if condition

if((stream = fmemopen((void*)str, strlen(str), "r")) == NULL){
// ...
}
gives me a warning: assignment makes pointer from integer without a
cast

But only when I compile it with gcc. With g++ it's fine. Why does the
warning show up and how can I get rid of it (using gcc)?

I tried it with gcc 3.2.2 and 3.3.1 and got the same warnings both
times.

Thanks!
Markus
PS: Here is a mini version of a program I am trying

#include <stdio.h>
#include <string.h>

int main(){
char ch;
FILE *stream;
char *str = "test";
if((stream = fmemopen((void*)str, strlen(str), "r")) == NULL){
fprintf(stderr, "Cannot open stream to string '%s'\n", str);
}
while ((ch = fgetc (stream)) != EOF)
printf ("Got %c\n", ch);
fclose (stream);
return 0;
}
Nov 14 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Markus Dehmann wrote:
The following if condition

if((stream = fmemopen((void*)str, strlen(str), "r")) == NULL){
// ...
}
gives me a warning: assignment makes pointer from integer without a
cast
Since no fmem* function exists as part of standard C or even C++, you
need to ask in a newsgroup, mailing list, or tech support for your
implementation...
But only when I compile it with gcc. With g++ it's fine. Why does the
warning show up and how can I get rid of it (using gcc)?
.... and there are current gcc implementations delivered without any
fmem* functions as part of the supplied libraries.

It appears that you have not included the appropriate prototypes for
your function. If you have invoked gcc as a c89 or c99 compliant
compiler then it is certain that the headers you included
#include <stdio.h>
#include <string.h>


have no prototypes for any fmem* functions visible.
Nov 14 '05 #2

P: n/a
Markus Dehmann wrote:

The following if condition

if((stream = fmemopen((void*)str, strlen(str), "r")) == NULL){
// ...
}
gives me a warning: assignment makes pointer from integer without
a cast .... snip ...
#include <stdio.h>
#include <string.h>

int main(){
char ch;
FILE *stream;
char *str = "test";
if((stream = fmemopen((void*)str, strlen(str), "r")) == NULL){
fprintf(stderr, "Cannot open stream to string '%s'\n", str);
}
while ((ch = fgetc (stream)) != EOF)
printf ("Got %c\n", ch);
fclose (stream);
return 0;
}


There is no such standard function as fmemopen. Even if there
were and you have simply neglected to #include the appropriate
header, and the function took a void* first parameter, you should
not be casting anything. So your code should really read:

#include <stdio.h>
#include <string.h>
#include "fmemopen.h"

int main(void) {
char ch;
FILE *stream;
char *str = "test";

if (!(stream = fmemopen(str, strlen(str), "r"))) {
fprintf(stderr, "Cannot open stream to string '%s'\n",
str);
}
else {
while ((ch = fgetc(stream)) != EOF)
printf("Got %c\n", ch);
fclose(stream);
}
return 0;
}

assuming fmemopen.h declares:

FILE *fmemopen(void *, size_t, char *);

In addition, names begining with "str" are reserved for the
implementation. You should change those.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #3

P: n/a

"CBFalconer" <cb********@yahoo.com> wrote in message news:40***************@yahoo.com...
Markus Dehmann wrote: [..]
assuming fmemopen.h declares:

FILE *fmemopen(void *, size_t, char *);

In addition, names begining with "str" are reserved for the
implementation. You should change those.

IIRC, the restriction is not for the local variables. Please
correct me, if I understood it wrongly.

Vijay
--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #4

P: n/a
In <40***************@yahoo.com> CBFalconer <cb********@yahoo.com> writes:
Markus Dehmann wrote:

The following if condition

if((stream = fmemopen((void*)str, strlen(str), "r")) == NULL){
// ...
}
gives me a warning: assignment makes pointer from integer without
a cast... snip ...

#include <stdio.h>
#include <string.h>

int main(){
char ch;
FILE *stream;
char *str = "test";
if((stream = fmemopen((void*)str, strlen(str), "r")) == NULL){
fprintf(stderr, "Cannot open stream to string '%s'\n", str);
}
while ((ch = fgetc (stream)) != EOF)
printf ("Got %c\n", ch);
fclose (stream);
return 0;
}


....In addition, names begining with "str" are reserved for the
implementation. You should change those.


A little knowledge is a dangerous thing. If you can't be bothered to
learn the full rule (or it exceeds your learning capabilities) it is OK
if you use the simplified version above for *your own* purposes. It is,
however, sheer stupidity to try to impose it to others, who may be capable
of learning the full rule and, therefore, have no use for your simplified
version.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #5

P: n/a
"Vijay Kumar R Zanvar" <vi*****@globaledgesoft.com> wrote in message
news:2k*************@uni-berlin.de...
"CBFalconer" <cb********@yahoo.com> wrote in message news:40***************@yahoo.com...

In addition, names begining with "str" are reserved for the
implementation. You should change those.


IIRC, the restriction is not for the local variables. Please
correct me, if I understood it wrongly.


See 7.1.3, 7.1.4 & 7.26

Identifiers beginning with 'str' followed by at least one lower case letter are reserved
for use as identifiers with external linkage. [In C90, such linkage is possibly case
insensitive.] If <string.h> or <stdlib.h> is included, then they are reserved as macro
names and file scope identifiers too.

But it's still possible to declare a local (auto) variable and have problems...

#include <string.h>

void bar(int);

void foo(void)
{
void (*strobe)(int) = bar; /* ok */
strobe(42); /* UB */
}

--
Peter
Nov 14 '05 #6

P: n/a
On Sat, 26 Jun 2004 12:38:41 +1000, "Peter Nilsson"
<ai***@acay.com.au> wrote in comp.lang.c:
"Vijay Kumar R Zanvar" <vi*****@globaledgesoft.com> wrote in message
news:2k*************@uni-berlin.de...
"CBFalconer" <cb********@yahoo.com> wrote in message news:40***************@yahoo.com...

In addition, names begining with "str" are reserved for the
implementation. You should change those.


IIRC, the restriction is not for the local variables. Please
correct me, if I understood it wrongly.


See 7.1.3, 7.1.4 & 7.26

Identifiers beginning with 'str' followed by at least one lower case letter are reserved
for use as identifiers with external linkage. [In C90, such linkage is possibly case
insensitive.] If <string.h> or <stdlib.h> is included, then they are reserved as macro
names and file scope identifiers too.

But it's still possible to declare a local (auto) variable and have problems...

#include <string.h>

void bar(int);

void foo(void)
{
void (*strobe)(int) = bar; /* ok */
strobe(42); /* UB */
}


I disagree with your assessment. The fact that a block scoped pointer
object holds the name of something (function or object) with external
linkage does not violate the standard requirement above. The linkage
and scope that count are only that of the identifier itself, 'strobe'
in this case.

There is absolutely no undefined behavior in your example.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #7

P: n/a
"Jack Klein" <ja*******@spamcop.net> wrote in message
news:cm********************************@4ax.com...

On Sat, 26 Jun 2004 12:38:41 +1000, "Peter Nilsson"
<ai***@acay.com.au> wrote in comp.lang.c:
"Vijay Kumar R Zanvar" <vi*****@globaledgesoft.com> wrote in message
news:2k*************@uni-berlin.de...

"CBFalconer" <cb********@yahoo.com> wrote in message
news:40***************@yahoo.com...
>
> In addition, names begining with "str" are reserved for the
> implementation. You should change those.
>

IIRC, the restriction is not for the local variables. Please
correct me, if I understood it wrongly.


See 7.1.3, 7.1.4 & 7.26

Identifiers beginning with 'str' followed by at least one lower
case letter are reserved for use as identifiers with external
linkage. [In C90, such linkage is possibly case insensitive.]
If <string.h> or <stdlib.h> is included, then they are reserved ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^ as macro names and file scope identifiers too. ^^^^^^^^^^^^^^
But it's still possible to declare a local (auto) variable and have
problems...

#include <string.h>

void bar(int);

void foo(void)
{
void (*strobe)(int) = bar; /* ok */
strobe(42); /* UB */
This is a function macro like application of a reserved identifier.
}


I disagree with your assessment. The fact that a block scoped pointer
object holds the name of something (function or object) with external
linkage does not violate the standard requirement above. ...


It's the reserved use as a function macro which is the problem.

--
Peter
Nov 14 '05 #8

P: n/a
"Peter Nilsson" <ai***@acay.com.au> wrote:
"Jack Klein" <ja*******@spamcop.net> wrote in message
news:cm********************************@4ax.com.. .

On Sat, 26 Jun 2004 12:38:41 +1000, "Peter Nilsson"
<ai***@acay.com.au> wrote in comp.lang.c:
> "Vijay Kumar R Zanvar" <vi*****@globaledgesoft.com> wrote in message
> news:2k*************@uni-berlin.de...
> >
> > "CBFalconer" <cb********@yahoo.com> wrote in message
> > news:40***************@yahoo.com...
> > >
> > > In addition, names begining with "str" are reserved for the
> > > implementation. You should change those.
> >
> > IIRC, the restriction is not for the local variables. Please
> > correct me, if I understood it wrongly.
>
> See 7.1.3, 7.1.4 & 7.26
>
> Identifiers beginning with 'str' followed by at least one lower
> case letter are reserved for use as identifiers with external
> linkage. [In C90, such linkage is possibly case insensitive.]
> If <string.h> or <stdlib.h> is included, then they are reserved ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^ > as macro names and file scope identifiers too. ^^^^^^^^^^^^^^ >
> But it's still possible to declare a local (auto) variable and have
> problems...
>
> #include <string.h>
>
> void bar(int);
>
> void foo(void)
> {
> void (*strobe)(int) = bar; /* ok */
> strobe(42); /* UB */
This is a function macro like application of a reserved identifier.


No. I can't find *any* macro defined in your code, for that matter.
What's disallowed is to write something like this:

#include <string.h>
#define strobe0 42 /* UB, str[a-z]* used as macro name */
static int strobe; /* UB, str[a-z]* used as file scope identifier */

whereas e.g. the following TU is valid:

/* Look Ma, no string.h included. */
#define strobe0 42
static int strobe;

as well as this one:

#include <string.h>
int foo( void )
{
int strstr = 666; /* Look Ma, block scope identifier. */
return strstr;
}
I disagree with your assessment. The fact that a block scoped pointer
object holds the name of something (function or object) with external
linkage does not violate the standard requirement above. ...


It's the reserved use as a function macro which is the problem.


No. It's used as a simple block scope identifier, which is perfectly
fine (modulo it may shadow a 'strobe' identifier defined in some
future version of string.h, but that's the programmer's problem then,
and doesn't result in undefined behaviour a priori).

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #9

P: n/a
"Irrwahn Grausewitz" <ir*******@freenet.de> wrote in message
news:eq********************************@4ax.com...
"Peter Nilsson" <ai***@acay.com.au> wrote:
"Jack Klein" <ja*******@spamcop.net> wrote in message
news:cm********************************@4ax.com.. .

On Sat, 26 Jun 2004 12:38:41 +1000, "Peter Nilsson"
<ai***@acay.com.au> wrote in comp.lang.c:

> "Vijay Kumar R Zanvar" <vi*****@globaledgesoft.com> wrote in message
> news:2k*************@uni-berlin.de...
> >
> > "CBFalconer" <cb********@yahoo.com> wrote in message
> > news:40***************@yahoo.com...
> > >
> > > In addition, names begining with "str" are reserved for the
> > > implementation. You should change those.
> >
> > IIRC, the restriction is not for the local variables. Please
> > correct me, if I understood it wrongly.
>
> See 7.1.3, 7.1.4 & 7.26
>
> Identifiers beginning with 'str' followed by at least one lower
> case letter are reserved for use as identifiers with external
> linkage. [In C90, such linkage is possibly case insensitive.]
> If <string.h> or <stdlib.h> is included, then they are reserved ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^
> as macro names and file scope identifiers too.

^^^^^^^^^^^^^^
>
> But it's still possible to declare a local (auto) variable and have
> problems...
>
> #include <string.h>
>
> void bar(int);
>
> void foo(void)
> {
> void (*strobe)(int) = bar; /* ok */
> strobe(42); /* UB */


This is a function macro like application of a reserved identifier.


No.


Huh? You don't think its a reserved identifier, or you don't think its a 'function macro
like' application?
I can't find *any* macro defined in your code, for that matter.


It's right there in the line...

#include <string.h>

Translation phases 1 through 4 occur long before any block scopes are identified by the
implementation.

My argument is quite simple: The identifier strobe is reserved as an external identifier,
therefore it is reserved as a macro whenever <string.h> or <stdlib.h> is included,
therefore a token sequence like...

strobe(42)

....is potentially a macro invocation, therefore it is potentially UB since no programmer
cannot possibly know the number of arguments the identifier will be defined with (until it
makes the standard as a prototyped function).

What am I missing?

--
Peter
Nov 14 '05 #10

P: n/a
"Peter Nilsson" <ai***@acay.com.au> wrote:
"Irrwahn Grausewitz" <ir*******@freenet.de> wrote in message
news:eq********************************@4ax.com...
"Peter Nilsson" <ai***@acay.com.au> wrote:
"Jack Klein" <ja*******@spamcop.net> wrote in message
news:cm********************************@4ax.com.. .
>
> On Sat, 26 Jun 2004 12:38:41 +1000, "Peter Nilsson"
> <ai***@acay.com.au> wrote in comp.lang.c:
>
> > Identifiers beginning with 'str' followed by at least one lower
> > case letter are reserved for use as identifiers with external
> > linkage. [In C90, such linkage is possibly case insensitive.]
> > If <string.h> or <stdlib.h> is included, then they are reserved
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^
> > as macro names and file scope identifiers too.
^^^^^^^^^^^^^^
> >
> > But it's still possible to declare a local (auto) variable and have
> > problems...
> >
> > #include <string.h>
> >
> > void bar(int);
> >
> > void foo(void)
> > {
> > void (*strobe)(int) = bar; /* ok */
> > strobe(42); /* UB */

This is a function macro like application of a reserved identifier.
I can't find *any* macro defined in your code, for that matter.
My argument is quite simple: The identifier strobe is reserved as an external identifier,
therefore it is reserved as a macro whenever <string.h> or <stdlib.h> is included,
therefore a token sequence like...

strobe(42)

...is potentially a macro invocation, therefore it is potentially UB since no programmer
cannot possibly know the number of arguments the identifier will be defined with (until it
makes the standard as a prototyped function).

What am I missing?


The fact that, if it _were_ a function-like macro, the first line would
in all probability have been an error.

Richard
Nov 14 '05 #11

P: n/a
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:40****************@news.individual.net...
"Peter Nilsson" <ai***@acay.com.au> wrote:

>> >
>> > #include <string.h>
>> >
>> > void bar(int);
[!! I have no idea why I put in this decleration!]
>> > void foo(void)
>> > {
>> > void (*strobe)(int) = bar; /* ok */
>> > strobe(42); /* UB */
>
>This is a function macro like application of a reserved identifier. I can't find *any* macro defined in your code, for that matter.

My argument is quite simple: The identifier strobe is reserved as an
external identifier, therefore it is reserved as a macro whenever
<string.h> or <stdlib.h> is included, therefore a token sequence
like...

strobe(42)

...is potentially a macro invocation, therefore it is potentially UB
since no programmer cannot possibly know the number of arguments the
identifier will be defined with (until it makes the standard as a
prototyped function).

What am I missing?


The fact that, if it _were_ a function-like macro, the first line would
in all probability have been an error.


Which 'first line'?

I don't think you mean...

void (*strobe)(int) = bar;

....since I'm sure you've seen code like...

#define foo(x) ((x) + 42)

int (foo)(int x) /* avoid macro invocation */
{
return x + 42;
}

But I can't see a problem with any other line, other than the one I highlight as UB.

--
Peter
Nov 14 '05 #12

P: n/a
In <cm********************************@4ax.com> Jack Klein <ja*******@spamcop.net> writes:
On Sat, 26 Jun 2004 12:38:41 +1000, "Peter Nilsson"
<ai***@acay.com.au> wrote in comp.lang.c:
But it's still possible to declare a local (auto) variable and have problems...

#include <string.h>

void bar(int);

void foo(void)
{
void (*strobe)(int) = bar; /* ok */
strobe(42); /* UB */
}


I disagree with your assessment. The fact that a block scoped pointer
object holds the name of something (function or object) with external
linkage does not violate the standard requirement above. The linkage
and scope that count are only that of the identifier itself, 'strobe'
in this case.

There is absolutely no undefined behavior in your example.


Wrong. There are two key factors in this simple example:

1. <string.h> is included. As a result, a function-like macro named
strobe may be in scope until the end of the translation unit or until
explicitly undefined.

2. strobe(42) would invoke the macro described above. The behaviour, is,
of course, undefined.

And the programmer got exactly what he deserved, by dereferencing a
function pointer as if it were a real function designator.
(*strobe)(42) is perfectly immune to the problem illustrated above.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #13

P: n/a
In <40****************@news.individual.net> rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"Peter Nilsson" <ai***@acay.com.au> wrote:
"Irrwahn Grausewitz" <ir*******@freenet.de> wrote in message
news:eq********************************@4ax.com...
> "Peter Nilsson" <ai***@acay.com.au> wrote:
> >"Jack Klein" <ja*******@spamcop.net> wrote in message
> >news:cm********************************@4ax.com.. .
> >>
> >> On Sat, 26 Jun 2004 12:38:41 +1000, "Peter Nilsson"
> >> <ai***@acay.com.au> wrote in comp.lang.c:
> >>
> >> > Identifiers beginning with 'str' followed by at least one lower
> >> > case letter are reserved for use as identifiers with external
> >> > linkage. [In C90, such linkage is possibly case insensitive.]
> >> > If <string.h> or <stdlib.h> is included, then they are reserved
> > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^
> >> > as macro names and file scope identifiers too.
> > ^^^^^^^^^^^^^^
> >> >
> >> > But it's still possible to declare a local (auto) variable and have
> >> > problems...
> >> >
> >> > #include <string.h>
> >> >
> >> > void bar(int);
> >> >
> >> > void foo(void)
> >> > {
> >> > void (*strobe)(int) = bar; /* ok */
> >> > strobe(42); /* UB */
> >
> >This is a function macro like application of a reserved identifier. > I can't find *any* macro defined in your code, for that matter.

My argument is quite simple: The identifier strobe is reserved as an external identifier,
therefore it is reserved as a macro whenever <string.h> or <stdlib.h> is included,
therefore a token sequence like...

strobe(42)

...is potentially a macro invocation, therefore it is potentially UB since no programmer
cannot possibly know the number of arguments the identifier will be defined with (until it
makes the standard as a prototyped function).

What am I missing?


The fact that, if it _were_ a function-like macro, the first line would
in all probability have been an error.


Nonsense! If it were a function-like macro, the first line wouldn't have
referred to it, due to purely syntactical issues. See 7.1.4 for
further enlightenment.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #14

P: n/a
"Peter Nilsson" <ai***@acay.com.au> wrote:
"Irrwahn Grausewitz" <ir*******@freenet.de> wrote:
"Peter Nilsson" <ai***@acay.com.au> wrote:
>> "Peter Nilsson" wrote: <snippage> >> > Identifiers beginning with 'str' followed by at least one lower
>> > case letter are reserved for use as identifiers with external
>> > linkage. [In C90, such linkage is possibly case insensitive.]
>> > If <string.h> or <stdlib.h> is included, then they are reserved
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^
>> > as macro names and file scope identifiers too.
> ^^^^^^^^^^^^^^
>> >
>> > But it's still possible to declare a local (auto) variable and have
>> > problems...
>> >
>> > #include <string.h>
>> >
>> > void bar(int);
>> >
>> > void foo(void)
>> > {
>> > void (*strobe)(int) = bar; /* ok */
>> > strobe(42); /* UB */
>
>This is a function macro like application of a reserved identifier.
No.


Huh? You don't think its a reserved identifier, or you don't think its a 'function macro
like' application?


The latter, alas I screwed up.

<snip>My argument is quite simple: The identifier strobe is reserved as an external identifier,
therefore it is reserved as a macro whenever <string.h> or <stdlib.h> is included,
therefore a token sequence like...

strobe(42)
Though (strobe)(42) would still be OK, right?
...is potentially a macro invocation, therefore it is potentially UB since no programmer
cannot possibly know the number of arguments the identifier will be defined with (until it
makes the standard as a prototyped function). s/cannot/can/
What am I missing?


The fact that I had things kind of backwards in my mind - sorry for
the confusion... :o}

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.