Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > fmemopen: assignment makes pointer from integer without a cast

Reply
Thread Tools

fmemopen: assignment makes pointer from integer without a cast

 
 
Markus Dehmann
Guest
Posts: n/a
 
      06-25-2004
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;
}
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      06-25-2004
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.
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      06-25-2004
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 ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
Vijay Kumar R Zanvar
Guest
Posts: n/a
 
      06-25-2004

"CBFalconer" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> 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 ((E-Mail Removed)) ((E-Mail Removed))
> Available for consulting/temporary embedded and systems.
> <http://cbfalconer.home.att.net> USE worldnet address!
>
>



 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      06-25-2004
In <(E-Mail Removed)> CBFalconer <(E-Mail Removed)> 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: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      06-26-2004
"Vijay Kumar R Zanvar" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "CBFalconer" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> >
> > 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


 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      06-26-2004
On Sat, 26 Jun 2004 12:38:41 +1000, "Peter Nilsson"
<(E-Mail Removed)> wrote in comp.lang.c:

> "Vijay Kumar R Zanvar" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > "CBFalconer" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> > >
> > > 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
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      06-27-2004
"Jack Klein" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> On Sat, 26 Jun 2004 12:38:41 +1000, "Peter Nilsson"
> <(E-Mail Removed)> wrote in comp.lang.c:
>
> > "Vijay Kumar R Zanvar" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > >
> > > "CBFalconer" <(E-Mail Removed)> wrote in message
> > > news:(E-Mail Removed)...
> > > >
> > > > 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


 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      06-27-2004
"Peter Nilsson" <(E-Mail Removed)> wrote:
>"Jack Klein" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed).. .
>>
>> On Sat, 26 Jun 2004 12:38:41 +1000, "Peter Nilsson"
>> <(E-Mail Removed)> wrote in comp.lang.c:
>>
>> > "Vijay Kumar R Zanvar" <(E-Mail Removed)> wrote in message
>> > news:(E-Mail Removed)...
>> > >
>> > > "CBFalconer" <(E-Mail Removed)> wrote in message
>> > > news:(E-Mail Removed)...
>> > > >
>> > > > 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 ((E-Mail Removed))
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
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      06-28-2004
"Irrwahn Grausewitz" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Peter Nilsson" <(E-Mail Removed)> wrote:
> >"Jack Klein" <(E-Mail Removed)> wrote in message
> >news:(E-Mail Removed).. .
> >>
> >> On Sat, 26 Jun 2004 12:38:41 +1000, "Peter Nilsson"
> >> <(E-Mail Removed)> wrote in comp.lang.c:
> >>
> >> > "Vijay Kumar R Zanvar" <(E-Mail Removed)> wrote in message
> >> > news:(E-Mail Removed)...
> >> > >
> >> > > "CBFalconer" <(E-Mail Removed)> wrote in message
> >> > > news:(E-Mail Removed)...
> >> > > >
> >> > > > 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


 
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
assignment makes pointer from integer without a cast Zach C Programming 3 05-05-2009 09:42 PM
Help: "Assignment makes integer from pointer without a cast" Juggernaut C Programming 10 04-09-2005 08:00 AM
warning: passing arg 1 of `atoi' makes pointer from integer without a cast francescomoi@europe.com C Programming 2 04-01-2005 05:30 PM
warning: assignment makes integer from pointer without a cast Dawn Minnis C Programming 4 02-20-2005 07:02 PM
assignment makes pointer from integer without a cast...help please. Rocke Robertson C Programming 14 10-02-2003 01:35 PM



Advertisments