Why is C compiler stubborn ?

A

alertjean

Or may be I am stubborn or dumb ... of not putting in a * in the
typecast.
This is code I am worrying about

long long b=1;
int *address ;
address=(int)&b;
printf ("%x %x \n",*(address+1)*address);

when I compile the code I get this warning from gcc (although it
allows me to execute and gives me the desired result).

warning: assignment makes pointer from integer without a cast

I get errors for the same in some other compilers.

and when I put address=(int *)&b; I get a clean output.

Why should it give this warning ? It can consider &b as an integer and
proceed..

Any thoughts ?

regards
Jean
 
F

Flash Gordon

Or may be I am stubborn or dumb ... of not putting in a * in the
typecast.

Firstly there is no such thing as a typecast. Stop using whatever book
or online resource tells you there is because the author obviously does
not know the language.
This is code I am worrying about

long long b=1;
int *address ;
address=(int)&b;

Address is a pointer to int, b is a long long, what on earth makes you
think that forcing address to point to b could possibly be a sensible
thing to do?
printf ("%x %x \n",*(address+1)*address);

%x is the wrong format specifier for a pointer. You need to use %p and
also cast the pointer to a void* as that is what %p expects.
Dereferencing a pointer to beyond the end of an object is not allowed,
anything can happen (if you expected it to pick up the value of address
then be aware that I regularly use a machine there this will *not* happen.

Also, this if obviously *not* the code that you compiled since it is
missing a comma. *Always* copy and paste the code, *never* retype it.
Also, always provide a *compile* program, not a small fragment.
when I compile the code I get this warning from gcc (although it
allows me to execute and gives me the desired result).

I've managed to walk around on a busy motorway in the middle of the day,
should I ignore the warnings telling me not to do it just because
nothing happened to me when I did it before?
warning: assignment makes pointer from integer without a cast

I get errors for the same in some other compilers.

and when I put address=(int *)&b; I get a clean output.

Why should it give this warning ? It can consider &b as an integer and
proceed..

Any thoughts ?

First rule of casting, don't do it. If you corrected all the problems so
that you were doing something legal the cast would not be required.

Casting to int obviously creates an int, what makes you think that an
int is the same as a pointer to an int? Do you think the sign post
pointing to the centre of town is actually the centre of town?

I suggest you start reading a good C text book from page 1. I suggest
The C Programming Language 2nd Edition by Kernighan & Ritchie, and also
when you have problems the comp.lang.c FAQ at http://c-faq.com/
 
A

Arthur J. O'Dwyer

Or may be I am stubborn or dumb ... of not putting in a * in the
typecast.
This is code I am worrying about

long long b=1;
int *address ;
address=(int)&b;
printf ("%x %x \n",*(address+1)*address);

You're doing too many things wrong at once. Since you defined
'address' to be of type (int*), it's a pointer. Therefore, you can't
assign an 'int' to it; that's what GCC is warning you about.

But you also can't (portably nor correctly) convert a value of
type (long long *) to 'int', which is what you're doing on the
right-hand side of that assignment.

And then inside the second argument to 'printf', you're trying
to multiply 'address' by *(address+1), which you also can't do.
I think that in that case you simply left out a comma, though.
(Which means you type carelessly, and therefore what you type /here/
may bear no relationship to what you type in your actual program.
Which is why some people may choose not to waste time on it.)

Finally, assuming the presence of the missing comma, you're trying
to treat two values of type 'int' as if they were 'unsigned int',
which is not a good idea.

You should try compiling with 'gcc -O2 -W -Wall -ansi -pedantic',
and fix the mistakes that GCC points out. Don't just ignore them, or
try to cover them up with casts; you won't learn anything that way,
and your code will still be wrong.

HTH,
-Arthur
 
S

santosh

Or may be I am stubborn or dumb ... of not putting in a * in the
typecast.

The correct term is cast, not typecast.
This is code I am worrying about

long long b=1;
int *address ;
address=(int)&b;
printf ("%x %x \n",*(address+1)*address);

Why do want to the that? The correct way would be:

long long b = 1;
long long *address;
address = &b;
printf("%x %x\n", *address+1, *address);
when I compile the code I get this warning from gcc (although it
allows me to execute and gives me the desired result).

warning: assignment makes pointer from integer without a cast

Not only that, you're using the wrong pointer type. You need a pointer
to type long long to point to an object of type long long and
similarly for others. The pointer type void can point to any object,
but it can only be used after an appropriate cast.
I get errors for the same in some other compilers.

Because it invokes undefined behaviour. Your printf statement is also
wrong, as it stands, though I think it's typo on your part. In future,
cut and paste code, don't retype.
and when I put address=(int *)&b; I get a clean output.

It's still leading to undefined behaviour, because you're casting the
pointer value yielded by &b from one type to another. Except for
conversion to and from void and char pointer types, other conversions
invoke implementation defined behaviour. It may work under one
implementation, but may fail under another.
Why should it give this warning ? It can consider &b as an integer and
proceed..

It a value of type pointer to long long.
 
L

Lew Pitcher

Or may be I am stubborn or dumb ... of not putting in a * in the
typecast.
This is code I am worrying about

long long b=1;
int *address ;
address=(int)&b;
printf ("%x %x \n",*(address+1)*address);

when I compile the code I get this warning from gcc (although it
allows me to execute and gives me the desired result).

warning: assignment makes pointer from integer without a cast

I get errors for the same in some other compilers.

and when I put address=(int *)&b; I get a clean output.

Why should it give this warning ? It can consider &b as an integer and
proceed..

No, it can't.

Given
long long b=1;
int *address ;
then
&b is a pointer to a long long, /not/ a pointer to an integer
there's no guarantee that two different pointer types have the same
length, format, or content constraints.

/Then/ you go further and make this
(int)&b
There is no guarantee that a pointer of any type can be mapped into
integer space

And finally, you take the worst step and
address = (int)&b;
which then tries to map an integer (which has already been mapped from
a pointer to long long) back as a pointer to int.

Your compiler is correct in warning you that you are incorrectly
making a pointer from an integer. Too many problems (including
alignment and value range mismappings) can occur when you do this, and
the compiler knows that if you've coded it this way, then you probably
don't realize what the dangers are.

HTH
 
B

bytebro

Or may be I am stubborn or dumb ... of not putting in a * in the
typecast.
This is code I am worrying about

long long b=1;
int *address ;
address=(int)&b;
printf ("%x %x \n",*(address+1)*address);

when I compile the code I get this warning from gcc (although it
allows me to execute and gives me the desired result).

warning: assignment makes pointer from integer without a cast

You are taking the address of a "long long", cast to an "int", which
you are storing in a "pointer to int".

What could conceivably persuade you that this is correct (let alone
portable) code? Stop whinging about the compiler warnings, and write
some sensible code.
 
N

Nick Keighley

Or may be I am stubborn or dumb ... of not putting in a * in the
typecast.
This is code I am worrying about

long long b=1;
int *address ;
address=(int)&b;

eek! why are you trying to shove an int into a pointer?
ITYM
address = (int*)&b;
printf ("%x %x \n",*(address+1)*address);

eek! you told printf you were going to give it two ints and then gave
it <pause>
um, well you only gave it one argument.

*(address + 1) * address
*(<int*> + 1) * <int*>
*(<invalid int*>) * <int*>
((address + 1) does not evaluate to a valid pointer)
<undefined int> * <int*>

so you dereferenced an invalid pointer to yield an int (sort of),
then multiplied an int by an int*. What effect were you expecting?
My DS9000 tried to add the sun and earth together.

when I compile the code I get this warning from gcc (although it
allows me to execute and gives me the desired result).
amazing


warning: assignment makes pointer from integer without a cast

I get errors for the same in some other compilers.

I think they're trying to tell you something...

and when I put address=(int *)&b; I get a clean output.
yes


Why should it give this warning ? It can consider &b as an integer and
proceed..

because (int)&b casts a long long into an <int>
and address is an <int*>. Not an int but a ptr-to-int

they do not have the same type.
 
S

santosh

santosh said:
The correct term is cast, not typecast.


Why do want to the that? The correct way would be:

long long b = 1;
long long *address;
address = &b;
printf("%x %x\n", *address+1, *address);

Sorry about this. The %x format specifier is for unsigned int. For
printing long long values use:

printf("%lld\n", *address);

I left out *(address+1) because in C though you can point a pointer to
one element past an array, (a single object is considered as an array
of one element), you cannot deference it to take the value. Doing so
invokes undefined behaviour.

<snip>
 
O

Old Wolf

(e-mail address removed) wrote, On 13/03/07 06:47:


Firstly there is no such thing as a typecast. Stop using whatever book
or online resource tells you there is because the author obviously does
not know the language.

It is not uncommon to use "typecast" to mean "cast". It can even
help to disambiguate from some of the other meanings of "cast".
 
R

Richard Tobin

Flash Gordon said:
Firstly there is no such thing as a typecast.

I can see an argument - even if I don't always agree with it - for not
using everyday or standard computer science terms here when they
conflict with their use in the C standard, but rejecting the use of a
common computer science term merely because the C standard doesn't
happen to use it seems quite excessive.

-- Richard
 
R

Richard Heathfield

Old Wolf said:
It is not uncommon to use "typecast" to mean "cast".

Right - it is not uncommon for people to misuse words. That doesn't mean
we should encourage the practice.
It can even
help to disambiguate from some of the other meanings of "cast".

C has only one technical meaning for "cast", so the question doesn't
arise.
 
F

Flash Gordon

Richard Tobin wrote, On 13/03/07 22:18:
I can see an argument - even if I don't always agree with it - for not
using everyday or standard computer science terms here when they
conflict with their use in the C standard, but rejecting the use of a
common computer science term merely because the C standard doesn't
happen to use it seems quite excessive.

I consider it important to use the correct terminology for C when
discussing C so that people can then look things up easily in a decent C
text book such as K&R2.
 
C

Chris Dollin

Richard said:
I can see an argument - even if I don't always agree with it - for not
using everyday or standard computer science terms here when they
conflict with their use in the C standard, but rejecting the use of a
common computer science term merely because the C standard doesn't
happen to use it seems quite excessive.

The only place I've come across the term `typecast` is in
postings to CLC from people who don't realise that the C
term is `cast`.

Perhaps I should get out more?
 
S

santosh

Chris said:
The only place I've come across the term `typecast` is in
postings to CLC from people who don't realise that the C
term is `cast`.

I've come across that term in various books and tutorials on
programming languages. Usually it's a book on C, but I'm sure I've
encountered it in material for other languages too, though I can't
remember right now.

It's understandable, though not desirable, that most programming
language books and tutorials are not as precise and rigorous as their
official definition.
 
C

CBFalconer

Chris said:
.... snip ...

The only place I've come across the term `typecast` is in
postings to CLC from people who don't realise that the C
term is `cast`.

Perhaps I should get out more?

I think it has to do with Linotype machines and molten lead. :)
 
F

Flash Gordon

santosh wrote, On 14/03/07 09:13:
I've come across that term in various books and tutorials on
programming languages. Usually it's a book on C, but I'm sure I've
encountered it in material for other languages too, though I can't
remember right now.

It's understandable, though not desirable, that most programming
language books and tutorials are not as precise and rigorous as their
official definition.

I've yet to see a good reason why one should use the term typecast
instead of cast. A beginner at programming won't know either term so
learning the correct terminology for C is no hardship in this case, and
someone more experienced who has come across the term typecast is highly
unlikely to be confused by C books using the term cast.

So far I have only seen the term typecast used by people who do not know
the language and in resources which are bad for other reasons. So I
think the only reason some beginners use it is that they are learning
from poor resources, and the use of the word typecast is a good
indicator that the person using it has a poor knowledge of C even if the
person using it is the author of a book on C.
 
C

Chris Dollin

Bob said:
It seems that none of you have ever read a Pascal or Delphi manual !!

I've read several Pascal manuals, but that was in the days when
Pascal didn't have a "typecast" (whatever that is).
 
B

Bob Martin

in 724333 20070314 132750 Flash Gordon said:
santosh wrote, On 14/03/07 09:13:

I've yet to see a good reason why one should use the term typecast
instead of cast. A beginner at programming won't know either term so
learning the correct terminology for C is no hardship in this case, and
someone more experienced who has come across the term typecast is highly
unlikely to be confused by C books using the term cast.

So far I have only seen the term typecast used by people who do not know
the language and in resources which are bad for other reasons. So I
think the only reason some beginners use it is that they are learning
from poor resources, and the use of the word typecast is a good
indicator that the person using it has a poor knowledge of C even if the
person using it is the author of a book on C.


It seems that none of you have ever read a Pascal or Delphi manual !!
 
D

Dave Vandervies

(e-mail address removed) wrote, On 13/03/07 06:47:

%x is the wrong format specifier for a pointer. You need to use %p and
also cast the pointer to a void* as that is what %p expects.

Nitpick: It's not strictly necessary to cast the pointer. You do need
to make sure that it fgets converted to a void* through some mechanism,
but if you already have an object of the correct type (void *) floating
around with the suitably converted pointer value you want to print,
you can just give printf the value of that object without casting it:
--------
/*A bit verbose if you're not using vp anywhere else, but perfectly valid*/
void *vp=my_pointer_value;
printf("%p\n",vp);

/*leaks memory but otherwise correct*/
printf("%p\n",malloc(42));

/*Not quite incorrect, but gratuitiously wrong in several ways*/
printf("%p\n",(void *)(int *)malloc(sizeof(int)));
--------

Giving non-void pointers to printf is, however, one of the few situations
where pointer casts are all of correct, useful, and CLC-conforming.


dave
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top