"sizeof" problem

This is a discussion on "sizeof" problem within the C Programming forums, part of the General Programming Boards category; I can run the program but it actually gave warning I can't solve it Code: #include<stdio.h> size_t getSize( float *ptr ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    29

    "sizeof" problem

    I can run the program but it actually gave warning
    I can't solve it

    Code:
    #include<stdio.h>
    
    
    size_t getSize( float *ptr );
    
    
    int main()
    {
    	float array[20];
    
    
    	printf("number of bytes in array is %d "
    		"\nThe number of bytes returned is %d\n",
    		sizeof(array), getSize(array ) );
    
    
    	return 0;
    }
    
    
    size_t getSize( float *ptr )
    {
    	return sizeof(ptr);
    }

    The book answer gave
    " the number of bytes in the array is 80
    the number of bytes returned by getSize is 4"

    However I got
    " the number of bytes in the array is 80
    the number of bytes returned by getSize is 8"

    so what's the problem ??

  2. #2
    Registered User
    Join Date
    Jun 2010
    Location
    In a house
    Posts
    15
    Some types are platform dependent and float is one of them.
    Last edited by tripleA; 12-30-2012 at 04:17 AM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,629
    There is no problem. The result of sizeof() for almost any type (except char types) is implementation (compiler and host system) defined.

    Presumably the author of the book used a 32-bit compiler, for which sizeof() typically yields 4 for any pointer type. You are presumably using a 64-bit compiler, for which sizeof() typically yields 8 for any pointer type.

    Incidentally, a size_t is not necessarily an int, so %d format should not be used as the result is undefined behaviour. If your compiler is recent enough, use %zu. Otherwise, convert the size_t to int, if you must use %d. (for example, "(int)sizeof(array)").
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    29
    ok...
    more or less I get the idea of it ..
    Thank you

  5. #5
    Registered User Raj 89's Avatar
    Join Date
    Nov 2012
    Location
    Chennai, TamilNadu
    Posts
    16
    Quote Originally Posted by loongkee View Post

    Code:
    size_t getSize( float *ptr )
    {
        return sizeof(ptr);
    }
    You just calculated the size of pointer variable in the function getSize. I think u r using a 64 bit machine and a 64 bit compiler. The size varies with reference to compiler and machine we use. The size of a pointer variable in a 64 bit m/c is 8 bytes.

    Since the value you're returning is 8, that is printed using printf....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "sizeof" character strings - output issue
    By bunko in forum C Programming
    Replies: 3
    Last Post: 12-03-2008, 06:10 PM
  2. A funny code snippet of "sizeof()"
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 05-19-2008, 02:09 AM
  3. Replies: 6
    Last Post: 10-15-2007, 09:05 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21