Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Pointer

Reply
Thread Tools

Pointer

 
 
Miks
Guest
Posts: n/a
 
      03-10-2006

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 ?

 
Reply With Quote
 
 
 
 
Richard G. Riley
Guest
Posts: n/a
 
      03-10-2006
"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.
 
Reply With Quote
 
 
 
 
Vladimir S. Oka
Guest
Posts: n/a
 
      03-10-2006

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.

 
Reply With Quote
 
Miks
Guest
Posts: n/a
 
      03-10-2006
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



 
Reply With Quote
 
Miks
Guest
Posts: n/a
 
      03-10-2006
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

 
Reply With Quote
 
Miks
Guest
Posts: n/a
 
      03-10-2006
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

 
Reply With Quote
 
Vladimir S. Oka
Guest
Posts: n/a
 
      03-10-2006

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

 
Reply With Quote
 
Vladimir S. Oka
Guest
Posts: n/a
 
      03-10-2006

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

 
Reply With Quote
 
Miks
Guest
Posts: n/a
 
      03-10-2006

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

 
Reply With Quote
 
John Tsiombikas (Nuclear / Mindlapse)
Guest
Posts: n/a
 
      03-10-2006
On 2006-03-10, Miks <(E-Mail Removed)> 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)
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://nuclear.demoscene.gr/
 
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
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments