sizeof operator

This is a discussion on sizeof operator within the C Programming forums, part of the General Programming Boards category; Why do you need the casting operator to receive the correct size of my_array? And what is it outputting if ...

  1. #1
    Registered User Dave Jay's Avatar
    Join Date
    Mar 2002
    Posts
    33

    sizeof operator

    Why do you need the casting operator to receive the correct size
    of my_array? And what is it outputting if you don't use it?


    Code:
    #include <stdio.h>
    
    char * my_array[] = {"string1", "string2", "string3", ""};
    
    int main()
    {
      printf("size of my_array = %u\n", sizeof( (char*)my_array));
    
      return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you run it and find out? The cast is wrong, by the way. You shouldn't be using a cast there. First off, 'sizeof' isn't really meant for arrays. It will work in the right situation, but it's not really useful for that.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Dave Jay's Avatar
    Join Date
    Mar 2002
    Posts
    33

    i did run it

    But now I realize that addresses can be different sizes on machines, therefore different byte sizes - scratch the casting operator idea.
    What situation will sizeof not work?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void foo( char array[] )
    {
        printf("size of array is: %d\n", sizeof( array ) );
    }
    
    void bar( void )
    {
        char array[ 100 ];
    
        foo( array );
    }
    It won't work in cases where the array is not declared in direct scope of the usage of the sizeof operator.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    When used on an array the sizeof operator will return its size in bytes. If you want the number of elements, you need to divide the size of the array by the size of one of its elements:
    Code:
    printf("size of my_array = %u\n", sizeof(my_array) / sizeof(my_array[0]));
    Some programmers use a macro to make the code more readable:
    Code:
    #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
    
    printf("size of my_array = %u\n", ARRAY_SIZE(my_array));
    The sizeof operator will return the size of an array that is declared in the current scope. When used on a pointer, it will return the size of a pointer, which is 4 bytes on most 32bit platforms.

  6. #6
    Registered User Dave Jay's Avatar
    Join Date
    Mar 2002
    Posts
    33

    thats all i wanted to know

    Thanks for the response anonytmouse, and not the typical condescending criticism. Some people tend to forget that other people aren't programmers and have lives outside of a computer.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Whine whine whine. Where's the "condesending criticism" in any of my posts here? And yes, you were talking about me, because I'm the only one that replied.

    Let's pull your head out of your ass and see why I replied the way I did:
    Quote Originally Posted by Dave Jay
    And what is it outputting if you don't use it?
    Quote Originally Posted by quzah
    Why don't you run it and find out?
    Wow, that sure was "condesending". You post a program which will compile and run, and ask us what the output is. Is it really that hard to just compile and run it? If so, which you then follow up and say you did already do, then why did you ask what the output was?

    Furthermore, my second reply was another exact answer to your question. All you whiners should stop feeling sorry about yourselves for long enoug to actually read what I write once in a while instead of crying about possibly getting your feelings hurt.

    Gee, I'm really sorry if I don't pander and sugar coat things for you little whiners.

    No wait, that's not true at all.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Closed. No reason to degrade into flame war.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  9. #9
    Registered User Dave Jay's Avatar
    Join Date
    Mar 2002
    Posts
    33

    look

    Wow, that sure was "condesending". You post a program which will compile and run, and ask us what the output is. Is it really that hard to just compile and run it? If so, which you then follow up and say you did already do, then why did you ask what the output was?
    I didn't know what the output meant.

    As usual, my intuition about you was right. Do me a favor and ignore my posts.

  10. #10
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Closed. Seriously.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  2. Replies: 6
    Last Post: 10-15-2007, 09:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Replies: 14
    Last Post: 06-28-2006, 02:58 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2003, 12:51 AM

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