*void -> *short ????? - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 26 of 26

Thread: *void -> *short ?????

  1. #16
    Join Date
    Dec 2001
    Location
    Greece, Athens
    Posts
    1,015
    thanx a lot guys. anyway, scanf("%hd",(short*)a) works pretty fine, and I aggre that it's a matter of scanf NOT printf(). printf displays the content of a even when using %d.
    Theodore
    Personal Web Page (some audio segmentation tools): www.di.uoa.gr/~tyiannak

  2. #17
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    Thinking about it, the original printf("%d") statement is entirely correct. There is no need for %hd in there nor for a conversion to int, since the compiler will upgrade the dereferenced value of var to an int automatically. So it's just the scanf that's wrong.
    The compiler upgrades all arguments to multiples of the machine word size (32 bits on the usual 32-bit PC), which means that you're quite lucky it works this way. Because the compiler doesn't know that you want an int. E.g. if you do this:
    Code:
    int i = 23;
    printf("%i64", i);
    you're in trouble. i64 is an MS-specific sign for a 64-bit integer, but you only provide 32 bits.
    A pointer as passed to scanf is already 32 bits, that's why no enlargening is done.


    On a side note, this line in the original code:
    Code:
    var = (short*)malloc(sizeof(short));
    doesn't need the short* cast. malloc returns a void* and var is a void*, everyone's satisfied. The cast has no runtime effect anyway, it's only a sign for the compiler.
    All the buzzt
    CornedBee

  3. #18
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    Originally posted by CornedBee
    The compiler upgrades all arguments to multiples of the machine word size (32 bits on the usual 32-bit PC), which means that you're quite lucky it works this way.
    So Athlon64s would convert shorts to 64-bit integers?
    And why do floats get converted to doubles then? According to your argument, there should be no need to promote a double.
    Honestly, if this was the intended behavior, then code that ran on a Pentium would have completely different behavior than that compiled for an Athlon64 -- even though they run the same OS.

    The reason the short is promoted to an int is because of the rules of the C language, not because of the OS. Usually, int's contain a single machine word -- but not always. The previous version of National Instruments' C compiler assumed an int was 16 bits, not 32 bits -- even though it compiled code for 32-bit Windows. For this compiler, shorts didn't need to be promoted, because a short was equivalent to an int -- despite the fact that the machine word size was 32-bits.

  4. #19
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    Not sure about the 64-bit processors, only about 16- and 32-bit processors.


    floats get converted to double? Are you sure?
    All the buzzt
    CornedBee

  5. #20
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    Absolutely, 100% positive. It's part of the C language standard.

  6. #21
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    Ok then.
    All the buzzt
    CornedBee

  7. #22
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245

    Don't just take my word for it...

    I've seen many people be "100% positive" about a subject before and be 100% wrong. Don't just take my word for it. Do some research yourself. Do a search on Google. Pick up a copy of "The C Programming Language 2nd edition" and look for promotions in variable argument length functions. If I remember correctly, there is a section on printf() and they may cover such things there. Read some other books on C. Or just go get a copy of the standard and read it. Or even write a program to test my statements.

    Anyway, my point is: do the reasearch -- don't just take the word of one person.

    - Kevin

  8. #23
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    I don't have a copy of the C standard (know any legal way of getting a cheap or free one?), but if you say it's there, I trust you.
    All the buzzt
    CornedBee

  9. #24
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    If you're that trusting, then mail me $1000 and I will psychically command all bugs to leave your programs.

    Seriously, do the research! Read the fourth paragraph of the following page:

    http://216.239.41.104/search?q=cache...hl=en&ie=UTF-8

    It says that variable argument lists convert arguments to int, unsigned int, pointer, or double. Of coarse, I am a little skeptical about this page too, since it seems to imply that long integers cannot be passed as a variable-length argument.

  10. #25
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    I trust other's knowledge if they before have shown it (and I've seen you answer other questions), I don't trust other's benevolence, or magical abilities for that matter


    What I need is the C standard, but I don't really want to buy it...
    All the buzzt
    CornedBee

  11. #26
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
       Thank you for the kind words!

       Darn!

    and,

       I understand!

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center