473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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 ?

Mar 10 '06 #1
14 6688

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.

Mar 10 '06 #2
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

¦

Mar 10 '06 #3
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

Mar 10 '06 #4
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

Mar 10 '06 #5

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

Mar 10 '06 #6

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

Mar 10 '06 #7

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

Mar 10 '06 #8
On 2006-03-10, Miks <ak********@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)
nu*****@siggraph.org
http://nuclear.demoscene.gr/
Mar 10 '06 #9
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.

Mar 10 '06 #10
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;
}

Mar 10 '06 #11
"Miks" <ak********@gmail.com> 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) ks***@mib.org <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.
Mar 10 '06 #12
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 ---
Mar 11 '06 #13
Joe Wright <jo********@comcast.net> 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) ks***@mib.org <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.
Mar 11 '06 #14
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
Mar 11 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
16
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
16
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
8
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.