Thread: short to int

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    201

    short to int

    I have the following code:

    Code:
    typedef struct X
    {
      const short x;
    } X;
    
    int func(void)
    {
      X x = { 3 };
    
      return x.x;
    }
    So you see that the x member is of short type but func returns an int, not a short. Would there be a problem with this code?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Well putting const inside your struct makes it a tad inconvenient to use.
    From a type point of view, nothing particular stands out.

  3. #3
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    There's never an issue from going from short to int because all values of short are guaranteed to fit inside an int.

  4. #4
    Registered User Afrinux's Avatar
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    46
    Some compilers might give you a warning, but I dont think it will affect the results.
    I am a little puzzled here, why do use const in your data structure?

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by Afrinux
    Some compilers might give you a warning, but I dont think it will affect the results.
    I am a little puzzled here, why do use const in your data structure?
    The original code is written by somebody who uses const a lot, just to express that vars cant be changed. My own opinion would be to mostly use const for pointers really.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  2. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM