Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Pointer (http://www.velocityreviews.com/forums/t441728-pointer.html)

Miks 03-10-2006 01:49 PM

Pointer
 

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 ?


Richard G. Riley 03-10-2006 02:03 PM

Re: Pointer
 
"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 ...

--
"A desk is a dangerous place from which to view the world" - LeCarre.

Vladimir S. Oka 03-10-2006 02:12 PM

Re: Pointer
 

Miks wrote:
> 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.


Miks 03-10-2006 02:14 PM

Re: Pointer
 
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




Miks 03-10-2006 02:19 PM

Re: Pointer
 
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


Miks 03-10-2006 02:22 PM

Re: Pointer
 
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


Vladimir S. Oka 03-10-2006 02:27 PM

Re: Pointer
 

Miks wrote:
> 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.

--
BR, Vladimir


Vladimir S. Oka 03-10-2006 02:30 PM

Re: Pointer
 

Miks wrote:
> 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)."

--
BR, Vladimir


Miks 03-10-2006 03:25 PM

Re: Pointer
 

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


John Tsiombikas (Nuclear / Mindlapse) 03-10-2006 04:16 PM

Re: Pointer
 
On 2006-03-10, Miks <akmeera2k4@gmail.com> wrote:
> 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.

--
John Tsiombikas (Nuclear / Mindlapse)
nuclear@siggraph.org
http://nuclear.demoscene.gr/


All times are GMT. The time now is 08:24 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.