Pointer

Discussion in 'C Programming' started by Miks, Mar 10, 2006.

  1. Miks

    Miks Guest

    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 ?
     
    Miks, Mar 10, 2006
    #1
    1. Advertising

  2. "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.
     
    Richard G. Riley, Mar 10, 2006
    #2
    1. Advertising

  3. 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.
     
    Vladimir S. Oka, Mar 10, 2006
    #3
  4. Miks

    Miks Guest

    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, Mar 10, 2006
    #4
  5. Miks

    Miks Guest

    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, Mar 10, 2006
    #5
  6. Miks

    Miks Guest

    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, Mar 10, 2006
    #6
  7. 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, Mar 10, 2006
    #7
  8. 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
     
    Vladimir S. Oka, Mar 10, 2006
    #8
  9. Miks

    Miks Guest

    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
     
    Miks, Mar 10, 2006
    #9
  10. On 2006-03-10, 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.


    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://nuclear.demoscene.gr/
     
    John Tsiombikas (Nuclear / Mindlapse), Mar 10, 2006
    #10
  11. Miks

    John Bode Guest

    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.
     
    John Bode, Mar 10, 2006
    #11
  12. 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;
    }
     
    Martin Ambuhl, Mar 10, 2006
    #12
  13. "Miks" <> 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.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 10, 2006
    #13
  14. Miks

    Joe Wright Guest

    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 ---
     
    Joe Wright, Mar 11, 2006
    #14
  15. Joe Wright <> 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) <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.
     
    Keith Thompson, Mar 11, 2006
    #15
  16. Miks

    Joe Estock Guest

    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
     
    Joe Estock, Mar 11, 2006
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    741
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    875
    Jordan Abel
    Mar 28, 2006
  3. Replies:
    4
    Views:
    1,329
    Fred Zwarts
    Jul 2, 2009
  4. A
    Replies:
    7
    Views:
    656
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    496
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page