Pointer

M

Miks

float a = 2.0;
int *p;
p = a;
printf(""%u",p);

It will say suspicious pointer conversion but still will work and print
address of a something 66345 like this right ?
 
R

Richard G. Riley

"Miks"posted the following on 2006-03-10:
float a = 2.0;
int *p;
p = a;
printf(""%u",p);

It will say suspicious pointer conversion but still will work and print
address of a something 66345 like this right ?

Try it and see. You have a compiler & an editor?

There is more room in C than any other language for mistyping and
falling foul of dangerous between type conversions. Rubbish in,
rubbish out as they say.

Almost for sure the above is not what you want to do and certainly
will not compile.

Key points for you to look at:

1) p is a pointer to an integer. You have declared no integer for it
to point to.

2) a is a float.

You are assigning a float value of 2 to p. That is some nasty casting
even if you force it.

At the risk of incurring the wrath of some of the regulars who see a
debugger as only for lightweights, a debugger would clarify all this
as you step through it. After you compile it and link it that is ...
 
V

Vladimir S. Oka

Miks said:
float a = 2.0;
int *p;
p = a;
printf(""%u",p);

You have an extra double quote here. Even if you post what's clearly a
snippet, try to make each line syntactically correct.

A good compiler also warns that you're trying to print a pointer value
using unsigned int format specifier. You should've used %p.
It will say suspicious pointer conversion but still will work and print
address of a something 66345 like this right ?

Wrong. It actually should say something like "incompatible types in
assignment" at line `p = a;`. You should turn up your compiler
sensitivity dial a few notches, and revisit your C textbook as well.
 
M

Miks

Hi Thanks for reply,

I'm using Turbo c 2.01, Borland International

#include <stdio.h>
main()
{
float a = 2.0;
int *p;
p = a;
printf("%u",p);
getch();
}

Error: Illegal Use of Floating point

I'm sorry possibly I cannot reproduce what I intended. I remeber
"Suspicious Pointer" Conversion warning but program still worked.

Thanks,
Miks

¦
 
M

Miks

Tried the below snippet

#include <stdio.h>

main()
{
int p;
float *a;
p=2;
a = p;
printf("%u",a);
getch();
}

Output is 26501, It works here, Assigning Float Pointer to Integer.
I'm using Turbo c 2.01, Borland International
 
M

Miks

Tried The below snippet also

#include <stdio.h>

main()
{
int a =2;
float *p;
p = a;
printf("%u",p);
getch();
}

This also Works!!
Thank you very much for sharing your ideas and correcting my
assumptions...
- Miks
 
V

Vladimir S. Oka

Miks said:
Tried the below snippet

#include <stdio.h>

main()

It's:

int main(void)
{
int p;
float *a;
p=2;
a = p;
printf("%u",a);
getch();

This (`getch`) is not a standard C function. Also, you did not #include
the Borland header that does declare it.
}

Output is 26501, It works here, Assigning Float Pointer to Integer.
I'm using Turbo c 2.01, Borland International

Output is 2 for me (once I corrected the errors), but that's not the
point. It might as well have been 42.

What happens in the code is entirely implementation defined, and hence
off-topic (and a Bad Thing) around here.

Good compilers warn you that you're making a pointer out of integer
without a cast (`p=2;`), and using unsigned int format specifier to
print out a pointer value.

All this is "legal" in terms that an implementation is permitted to
allow it, but it by no means make it a Good Thing to do.
 
V

Vladimir S. Oka

Miks said:
Tried The below snippet also

#include <stdio.h>

main()
{
int a =2;
float *p;
p = a;
printf("%u",p);
getch();
}

This also Works!!

To this, I must quote Dijkstra:

"[Poor programmers] derive their intellectual excitement from not
quite knowing what they are doing and prefer to be thrilled by the
marvel of the human mind (in particular their own ones)."
 
M

Miks

I accept all your comments, It's not my intention to prove my skills.
It was an interview question, It's no my intention to prove it works or
it doesn't work.

I answered interviewer reassigning pointers will work as stated but
it's not my personal programming style.

Hope I clarify what I'm.

Miks
 
J

John Tsiombikas (Nuclear / Mindlapse)

I accept all your comments, It's not my intention to prove my skills.
It was an interview question, It's no my intention to prove it works or
it doesn't work.

I answered interviewer reassigning pointers will work as stated but
it's not my personal programming style.

Hope I clarify what I'm.

Please provide some context when you reply, by quoting the relevant
parts of the article. Otherwise people will not be able to understand
what you are talking about.
 
J

John Bode

Miks said:
float a = 2.0;
int *p;
p = a;

This line should cause the compiler to yak outright: a float value
cannot be converted to a pointer type.

Changing that line to

p = &a;

will allow compilation to proceeed with warning about assigning between
incompatible pointer types; a pointer to int and a pointer to float may
have different representations. I'm not sure if this invokes undefined
behavior or not, but
printf(""%u",p);

definitely does; the %u conversion specifier expects an unsigned int as
its argument, and pointers are not unsigned ints. Use the %p
conversion specifier instead.
It will say suspicious pointer conversion but still will work and print
address of a something 66345 like this right ?

Depends on what you mean by "work." You've invoked at least one
instance of undefined behavior, so *any* result is suspect.
 
M

Martin Ambuhl

Miks said:
float a = 2.0;
int *p;
p = a;
printf(""%u",p);

It will say suspicious pointer conversion but still will work and print
address of a something 66345 like this right ?
#include <stdio.h>

int main(void)
{
float a = 2.0;
int *p;
int q; /* added to avoid allocation code below
*/
#if 0
p = a; /* p is a pointer, a is a float. These
are incompatible. Attempting to
interpret a float as a pointer is
meaningless. */

printf("%u", p); /* extra leading '"' removed. "%u" is
the specifier for an unsigned int.
p is a pointer, for which the
specifier if "%p", not "%u". And *p
is a signed int, for which "%u" is
also wrong. */
#endif

p = &q; /* this makes p actually point to some
memory which is of the correct
type */
printf("%p\n", (void *) p); /* the correct way to print the value
of p */
*p = a; /* copying the value of the float to
the memory to which p points. note
the '*' */
printf("%d\n", *p); /* printing the int. note both the
"%d" specifier and the dereferencing
'*' */
return 0;
}
 
K

Keith Thompson

Miks said:
I'm using Turbo c 2.01, Borland International

#include <stdio.h>
main()
{
float a = 2.0;
int *p;
p = a;
printf("%u",p);
getch();
}

"main()" should be "int main(void)".

The assignment
p = a;
attempts to assign a float value to a pointer. This is a constraint
violation, requiring a diagnostic.

If it were changed to
p = &a;
it would still be a constraint violation; you can convert one pointer
type to another, but not without a cast.

In the statement
printf("%u", p);
the "%u" format causes printf to expect an unsinged int; passing an
int* instead invokes undefined behavior. It also fails to terminate
the program's output with a new-line. The correct statement would be
printf("%p\n", (void*)p);

There is no getch() function in standard C, and you've failed to
#include any header that declares it. It's probably not necessary
anyway.

The program should end with a "return 0;".

I'm not going to try to come up with a correct version of the program;
there's not enough there to figure out what it's supposed to do.

(Somebody suggested that a debugger would be useful in understanding
this code. I don't see how. The code is incorrect; even if you could
compile it, your time would be better spent correcting it than
figuring out exactly how it behaves incorrectly. Undefined behavior
is undefined behavior.)
 
J

Joe Wright

Miks said:
Tried The below snippet also

#include <stdio.h>

main()
{
int a =2;
float *p;
p = a;
printf("%u",p);
getch();
}

This also Works!!
Thank you very much for sharing your ideas and correcting my
assumptions...
- Miks
Miks, you are running too fast. There are many errors here. It doesn't
'work' no matter what you 'think'. I suppose it prints '2' and you think
that's great, but consider..

int main(void)
{
return 0;
}
... is the minimal C program.

int a = 2;
float *p;
p = a;

Here you assign an int value to a float pointer. That's a constraint
violation, I think, and requires a diagnostic.

printf("%u",p);

I suppose this one is Undefined Behavior. A float pointer is not
compatible with an unsigned integer.

getch();

Not C. Get a book. Read the book.
 
K

Keith Thompson

Joe Wright said:
getch();

Not C. Get a book. Read the book.

I agree with your basic point, but I don't think you expressed it well.

"getch();" is C. It's an identifier followed by empty parentheses and
a semicolon, which makes it a statement that calls a function. If
there happens to be a function called "getch" that takes no arguments,
it's a valid function call.

In the context of the program, there is no declaration for getch()
(presumably some system-specific header should have been #included).
In C90, it's still potentially valid if getch() takes no arguments and
returns int.

The real point is that there is no getch() function in standard C (and
in fact there are at least two incompatible system-specific functions
by that name) -- and there's no real need to call it here anyway.

We sometimes forget, I think, that non-portable C is still C, and the
ability to write useful non-portable code is one of the greatest
strengths of the language, even though the details are off-topic here.
(The ability to write useful portable code is another of the greatest
strengths of the language.)
 
J

Joe Estock

Miks said:
I accept all your comments, It's not my intention to prove my skills.
It was an interview question, It's no my intention to prove it works or
it doesn't work.

I answered interviewer reassigning pointers will work as stated but
it's not my personal programming style.

Hope I clarify what I'm.

Miks

I do not mean this to be disrepectful in any way, however I hope that
whoever you were being interviewed by did not hire you on (or if they
did they know that you will require a fair amount of training before you
are able to begin working).

In order for you to be even concidered for a [decent] position in a
company that does programming in the C language you must first
understand the language as well as be able to read code (be it good code
or bad code) and understand what is going on (and in many cases what is
going on but more importantly what *should* be going on).

To help point you in the right direction, UB means *Undefined*
*Behavior*. This means exactly as it reads: the *behavior* is
*undefined*. To put that more clearly, the output may be one thing on
system A but then may be something *completely* different on system B.
The results are not reproduceable (is this even a word?), meaning that
you cannot produce the same exact result on every system out there under
the same conditions.

Joe
 

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,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top