Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Pointer

Reply
Thread Tools

Pointer

 
 
John Bode
Guest
Posts: n/a
 
      03-10-2006
Miks wrote:
> 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.

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



 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      03-10-2006
"Miks" <(E-Mail Removed)> writes:
> 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.)

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      03-11-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!!
> 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.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-11-2006
Joe Wright <(E-Mail Removed)> writes:
[...]
> 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.)

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Joe Estock
Guest
Posts: n/a
 
      03-11-2006
Miks 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.
>
> 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
 
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