[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

puzzling error

Bill Cunningham

5/5/2011 12:44:00 PM

I am not quite sure what is going on here but this is my tested and
compiled code. As written below at compile time I get this warning.

p.c: In function `main':
p.c:9: warning: assignment makes pointer from integer without a cast

I don't see the int in this code.

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

int main(void)
{
char p[] = "hello to all";
char *a;
printf("%s\n", p);
printf("%s\n", a = strfry(p)); //error on this line.
return 0;
}

strfry() is a GNU extension. It takes a char* as is sole argument and
returns a char*. Now if I change that char p[] to a char *p I get a
segmentation fault. What's up with that?

Bill


59 Answers

Ben Bacarisse

5/5/2011 12:56:00 PM

0

"Bill Cunningham" <nospam@nspam.invalid> writes:

> I am not quite sure what is going on here but this is my tested and
> compiled code. As written below at compile time I get this warning.
>
> p.c: In function `main':
> p.c:9: warning: assignment makes pointer from integer without a cast
>
> I don't see the int in this code.
>
> #include <stdio.h>
> #include <string.h>
>
> int main(void)
> {
> char p[] = "hello to all";
> char *a;
> printf("%s\n", p);
> printf("%s\n", a = strfry(p)); //error on this line.
> return 0;
> }
>
> strfry() is a GNU extension. It takes a char* as is sole argument and
> returns a char*.

An undeclared function is assumed to return int. string.h is not
declaring strfry so the compiler assumes it returns an it and complains
when you assign the result to a char * variable.

The man page (or some other documentation) should tell you how to get
strfry declared properly. Adding -D_GNU_SOURCE to the compile line
works for me.

> Now if I change that char p[] to a char *p I get a
> segmentation fault. What's up with that?

p then points to the string rather than being an array initialised by
it. The content of strings can't be modified, so passing p to any
function that tried to alter its contents gives rise to undefined
behaviour. A segmentation fault is a Good Result for undefined
behaviour.

--
Ben.

Ike Naar

5/5/2011 1:02:00 PM

0

On 2011-05-05, Bill Cunningham <nospam@nspam.invalid> wrote:
> I am not quite sure what is going on here but this is my tested and
> compiled code. As written below at compile time I get this warning.
>
> p.c: In function `main':
> p.c:9: warning: assignment makes pointer from integer without a cast
>
> I don't see the int in this code.
>
> #include <stdio.h>
> #include <string.h>
>
> int main(void)
> {
> char p[] = "hello to all";
> char *a;
> printf("%s\n", p);
> printf("%s\n", a = strfry(p)); //error on this line.
> return 0;
> }
>
> strfry() is a GNU extension. It takes a char* as is sole argument and
> returns a char*. Now if I change that char p[] to a char *p I get a
> segmentation fault. What's up with that?

Apparently not all versions of the GNU toolset provide the strfry() function.
If the function is not provided, the strfry(p) call in your program is
a call to an undefined function, and the compiler will assume int for the
return type. You then assign the (presumably int) returnvalue to char *a,
hence the warning about the pointer-from-integer conversion.

Marc Boyer

5/5/2011 1:02:00 PM

0

Le 05-05-2011, Bill Cunningham <nospam@nspam.invalid> a écrit :
> p.c: In function `main':
> p.c:9: warning: assignment makes pointer from integer without a cast
>
> I don't see the int in this code.
>
> #include <stdio.h>
> #include <string.h>
>
> int main(void)
> {
> char p[] = "hello to all";
> char *a;
> printf("%s\n", p);
> printf("%s\n", a = strfry(p)); //error on this line.
> return 0;
> }
>
> strfry() is a GNU extension. It takes a char* as is sole argument and
> returns a char*.

When I compile, I get also warning
warning: implicit declaration of function â??strfryâ??
and when I read the man page, I see that you need to
define macro _GNU_SOURCE to use this function.

So, without _GNU_SOURCE, the function strfry is assumed
to return an int, and the expression
a = strfry(p)
puts an int into a char*.

> Now if I change that char p[] to a char *p I get a
> segmentation fault. What's up with that?

Because
char * p = "hello to all";
defines a pointer p on a *read only* string "hello to all",
and strfry tries to write into a read-only memory?

Marc Boyer
--
En prenant aux 10% des francais les plus riches 12% de leurs revenus,
on pourrait doubler les revenus des 10% les plus pauvres.
http://www.inegalites.fr/spip.php?article1&...

August Karlstrom

5/5/2011 1:59:00 PM

0

On 2011-05-05 14:44, Bill Cunningham wrote:
> I am not quite sure what is going on here but this is my tested and
> compiled code. As written below at compile time I get this warning.
>
> p.c: In function `main':
> p.c:9: warning: assignment makes pointer from integer without a cast
>
> I don't see the int in this code.
>
> #include<stdio.h>
> #include<string.h>
>
> int main(void)
> {
> char p[] = "hello to all";
> char *a;
> printf("%s\n", p);
> printf("%s\n", a = strfry(p)); //error on this line.
> return 0;
> }
>
> strfry() is a GNU extension. It takes a char* as is sole argument and
> returns a char*. Now if I change that char p[] to a char *p I get a
> segmentation fault. What's up with that?

The clang compiler will give you a bit more information (even without
warnings enabled):

$ clang test.c
test.c:9:24: warning: implicit declaration of function 'strfry' is
invalid in
C99 [-Wimplicit-function-declaration]
printf("%s\n", a = strfry(p)); //error on this line.
^
test.c:9:22: warning: incompatible integer to pointer conversion
assigning to
'char *' from 'int'
printf("%s\n", a = strfry(p)); //error on this line.
^ ~~~~~~~~~
2 warnings generated.


/August

--
The competent programmer is fully aware of the limited size of his own
skull. He therefore approaches his task with full humility, and avoids
clever tricks like the plague. --Edsger Dijkstra

osmium

5/5/2011 3:38:00 PM

0

"Bill Cunningham" wrote:

> I am not quite sure what is going on here but this is my tested and
> compiled code. As written below at compile time I get this warning.
>
> p.c: In function `main':
> p.c:9: warning: assignment makes pointer from integer without a cast
>
> I don't see the int in this code.
>
> #include <stdio.h>
> #include <string.h>
>
> int main(void)
> {
> char p[] = "hello to all";
> char *a;
> printf("%s\n", p);
> printf("%s\n", a = strfry(p)); //error on this line.
> return 0;
> }
>
> strfry() is a GNU extension. It takes a char* as is sole argument and
> returns a char*. Now if I change that char p[] to a char *p I get a
> segmentation fault. What's up with that?

Do you think you have now mastered C and want to take on Unix as a
challenge? Or what?

This is the wrong newsgroup for Unix based questions.


Angel

5/5/2011 4:01:00 PM

0

On 2011-05-05, Bill Cunningham <nospam@nspam.invalid> wrote:
>
> strfry() is a GNU extension. It takes a char* as is sole argument and
> returns a char*. Now if I change that char p[] to a char *p I get a
> segmentation fault. What's up with that?

RTFM, RTFF.

--
The perfected state of a spam server is a smoking crater.
- The Crater Corollary to Rule #4

Bill Cunningham

5/5/2011 4:03:00 PM

0

osmium wrote:

> Do you think you have now mastered C and want to take on Unix as a
> challenge? Or what?
>
> This is the wrong newsgroup for Unix based questions.

This is a GNU function. GNUs Not Unix.

Bill


Bill Cunningham

5/5/2011 4:06:00 PM

0

Ike Naar wrote:

> Apparently not all versions of the GNU toolset provide the strfry()
> function. If the function is not provided, the strfry(p) call in your
> program is
> a call to an undefined function, and the compiler will assume int for
> the return type. You then assign the (presumably int) returnvalue to
> char *a, hence the warning about the pointer-from-integer conversion.

Ok but my implementation does support strfry and it does compile and
work. I just get that warning so the compiler must be smart enough to get it
right in the end.

Bill


Keith Thompson

5/5/2011 4:06:00 PM

0

Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> "Bill Cunningham" <nospam@nspam.invalid> writes:
>> I am not quite sure what is going on here but this is my tested and
>> compiled code. As written below at compile time I get this warning.
>>
>> p.c: In function `main':
>> p.c:9: warning: assignment makes pointer from integer without a cast
>>
>> I don't see the int in this code.
>>
>> #include <stdio.h>
>> #include <string.h>
>>
>> int main(void)
>> {
>> char p[] = "hello to all";
>> char *a;
>> printf("%s\n", p);
>> printf("%s\n", a = strfry(p)); //error on this line.
>> return 0;
>> }
>>
>> strfry() is a GNU extension. It takes a char* as is sole argument and
>> returns a char*.
>
> An undeclared function is assumed to return int.

In C90. C99, dropped implicit int, so attempting to call an undeclared
function is a constraint violation.

"gcc -std=c99" gives:
c.c: In function 'main':
c.c:9:5: warning: implicit declaration of function 'strfry'
c.c:9:22: warning: assignment makes pointer from integer without a cast

(Yes, it's still following the C90 implicit int rule, but that's ok as
long as it always warns about constraint violations.)

> string.h is not
> declaring strfry so the compiler assumes it returns an it and complains
> when you assign the result to a char * variable.

Right.

> The man page (or some other documentation) should tell you how to get
> strfry declared properly. Adding -D_GNU_SOURCE to the compile line
> works for me.

In fact, the man page says:

#define _GNU_SOURCE
#include <string.h>

char *strfry(char *string);

where "#define _GNU_SOURCE" in the source is equivalent to
"-D_GNU_SOURCE" on the command line.

*Never* use a function you're not completely familiar with without
first reading the documentation.

>> Now if I change that char p[] to a char *p I get a
>> segmentation fault. What's up with that?
>
> p then points to the string rather than being an array initialised by
> it. The content of strings can't be modified, so passing p to any
> function that tried to alter its contents gives rise to undefined
> behaviour. A segmentation fault is a Good Result for undefined
> behaviour.

Correction: the contents of string *literals* can't be modified.

More precisely, the contents of the array (which exists at run
time) which is associated with a string literal (which exists
only in your program source file) can't be modified. Even more
precisely, "can't be modified" means that attempting to modify them
is undefined behavior; an implementation could permit modifications,
but it wouldn't be doing you any favors.

This is an odd corner case. It would have made more sense for
string literals to be "const", so the compiler would (usually)
warn you about attempts to modify them, but that would have broken
a lot of pre-ANSI C code. C++, with less concern about backward
compatibility, does make string literals "const".

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Bill Cunningham

5/5/2011 4:07:00 PM

0

osmium wrote:

> Do you think you have now mastered C and want to take on Unix as a
> challenge? Or what?
>
> This is the wrong newsgroup for Unix based questions.

My we *must* have something to complain about. I have my POSIX interests
but I'll leave that for comp.unix.programmer.

Bill