Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. the question of "sizeof"!

the question of "sizeof"!

Scheduled Pinned Locked Moved C / C++ / MFC
question
11 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W wbgxx

    #include <iostream.h>

    I want to print the "helloworld" but it print "hell" ,why??

    void show( char str[])
    {
    for (int i=0; i < sizeof(str)/sizeof(str[0]); ++i)
    cout << str[i];
    }

    int main()
    {

    char str[]="helloworld!";
    show(str);
    return 0;
    }

    D Offline
    D Offline
    David Crow
    wrote on last edited by
    #2

    wbgxx wrote:

    I want to print the "helloworld" but it print "hell" ,why??

    Because the size of a pointer is 4. Try strlen() instead.

    "One man's wage rise is another man's price increase." - Harold Wilson

    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

    "Man who follows car will be exhausted." - Confucius

    1 Reply Last reply
    0
    • W wbgxx

      #include <iostream.h>

      I want to print the "helloworld" but it print "hell" ,why??

      void show( char str[])
      {
      for (int i=0; i < sizeof(str)/sizeof(str[0]); ++i)
      cout << str[i];
      }

      int main()
      {

      char str[]="helloworld!";
      show(str);
      return 0;
      }

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #3

      wbgxx wrote:

      void show( char str[]) { for (int i=0; i < sizeof(str)/sizeof(str[0]); ++i) cout << str[i]; }

      This code is the same as this:

      void show(char *str)
      {
         for (int i=0; i < sizeof(str)/sizeof(str[0]); ++i)
            cout << str[i];
      }

      The array decays to a pointer. The size of a pointer is 4 bytes and the size of a char is 1.

      Steve

      1 Reply Last reply
      0
      • W wbgxx

        #include <iostream.h>

        I want to print the "helloworld" but it print "hell" ,why??

        void show( char str[])
        {
        for (int i=0; i < sizeof(str)/sizeof(str[0]); ++i)
        cout << str[i];
        }

        int main()
        {

        char str[]="helloworld!";
        show(str);
        return 0;
        }

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #4

        'str' is a pointer. sizeof a pointer is 4 (on Win32). so you get four characters: h,e,l,l. if you want to get the length of a character string, use strlen.

        image processing toolkits | batch image processing

        1 Reply Last reply
        0
        • W wbgxx

          #include <iostream.h>

          I want to print the "helloworld" but it print "hell" ,why??

          void show( char str[])
          {
          for (int i=0; i < sizeof(str)/sizeof(str[0]); ++i)
          cout << str[i];
          }

          int main()
          {

          char str[]="helloworld!";
          show(str);
          return 0;
          }

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #5

          It's worth noting this way it would work:

          void show( char str[], int size)
          {
          for (int i=0; i < size; ++i)
          cout << str[i];
          }

          int main()
          {
          char str[]="helloworld!";
          show(str, sizeof(str)/sizeof(str[0]));
          return 0;
          }

          :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          W 1 Reply Last reply
          0
          • C CPallini

            It's worth noting this way it would work:

            void show( char str[], int size)
            {
            for (int i=0; i < size; ++i)
            cout << str[i];
            }

            int main()
            {
            char str[]="helloworld!";
            show(str, sizeof(str)/sizeof(str[0]));
            return 0;
            }

            :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            W Offline
            W Offline
            wbgxx
            wrote on last edited by
            #6

            Yeah, thanks for advantage!

            1 Reply Last reply
            0
            • W wbgxx

              #include <iostream.h>

              I want to print the "helloworld" but it print "hell" ,why??

              void show( char str[])
              {
              for (int i=0; i < sizeof(str)/sizeof(str[0]); ++i)
              cout << str[i];
              }

              int main()
              {

              char str[]="helloworld!";
              show(str);
              return 0;
              }

              S Offline
              S Offline
              Stephen Hewitt
              wrote on last edited by
              #7

              Here's another alternative (MSVC6 can't handle it):

              void show(const char *pStr, int size)
              {
              for (int i=0; i<size; ++i)
              {
              cout << pStr[i];
              }
              }
               
              template <int N>
              inline
              void show(const char (&a)[N])
              {
              show(a, N-1);
              }
               
              int _tmain(int argc, _TCHAR* argv[])
              {
              char str[] = "Hello world!";
              show(str);
               
              cout << endl;
               
              return 0;
              }

              Steve

              C 1 Reply Last reply
              0
              • S Stephen Hewitt

                Here's another alternative (MSVC6 can't handle it):

                void show(const char *pStr, int size)
                {
                for (int i=0; i<size; ++i)
                {
                cout << pStr[i];
                }
                }
                 
                template <int N>
                inline
                void show(const char (&a)[N])
                {
                show(a, N-1);
                }
                 
                int _tmain(int argc, _TCHAR* argv[])
                {
                char str[] = "Hello world!";
                show(str);
                 
                cout << endl;
                 
                return 0;
                }

                Steve

                C Offline
                C Offline
                Chris Meech
                wrote on last edited by
                #8

                Interesting example, Steve. In your example, does the compiler set N to 12 or 13? I ask because the for loop increments i up to but not including the size, but your template function call passes the value of N-1 to the function. So characters 0-11, all 12 of them, would only be output if the value of N was set to 13. 13 being the 'correct' size when you include the NULL terminator as part of the size of the string. Just an interesting little difference from a lot of the std C string functions. On a side note, is it the array reference that MSVC6 doesn't like or something about the template function declaration?

                Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

                C 1 Reply Last reply
                0
                • W wbgxx

                  #include <iostream.h>

                  I want to print the "helloworld" but it print "hell" ,why??

                  void show( char str[])
                  {
                  for (int i=0; i < sizeof(str)/sizeof(str[0]); ++i)
                  cout << str[i];
                  }

                  int main()
                  {

                  char str[]="helloworld!";
                  show(str);
                  return 0;
                  }

                  S Offline
                  S Offline
                  SpaceMonkey1970
                  wrote on last edited by
                  #9

                  You don't need the loop at all. cout is savvy enough to know where your string ends. Unless there's some other reason for the loop then your show() function is simply:

                  void show( char str[] )
                  {
                  cout << str;
                  }

                  Although in C/C++ we tend to use pointer notation:

                  void show (char* str)
                  {
                  cout << str;
                  }

                  And now, given that your function is doing so little you might question its usefulness and move the code into main:

                  int main()
                  {
                  char str[] = "helloworld!";
                  cout << str;
                  return 0;
                  }

                  Or, finally, you might prefer to use a string constant:

                  int main()
                  {
                  cout << "helloworld!";
                  return 0;
                  }

                  None of which is particularly useful if all you wanted to know is why sizeof() returned 4.

                  S 1 Reply Last reply
                  0
                  • C Chris Meech

                    Interesting example, Steve. In your example, does the compiler set N to 12 or 13? I ask because the for loop increments i up to but not including the size, but your template function call passes the value of N-1 to the function. So characters 0-11, all 12 of them, would only be output if the value of N was set to 13. 13 being the 'correct' size when you include the NULL terminator as part of the size of the string. Just an interesting little difference from a lot of the std C string functions. On a side note, is it the array reference that MSVC6 doesn't like or something about the template function declaration?

                    Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

                    C Offline
                    C Offline
                    CPallini
                    wrote on last edited by
                    #10

                    Chris Meech wrote:

                    Interesting example, Steve. In your example, does the compiler set N to 12 or 13? I ask because the for loop increments i up to but not including the size, but your template function call passes the value of N-1 to the function. So characters 0-11, all 12 of them, would only be output if the value of N was set to 13. 13 being the 'correct' size when you include the NULL terminator as part of the size of the string. Just an interesting little difference from a lot of the std C string functions.

                    Mine (VS 2010) puts (as it should) 13. :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    1 Reply Last reply
                    0
                    • S SpaceMonkey1970

                      You don't need the loop at all. cout is savvy enough to know where your string ends. Unless there's some other reason for the loop then your show() function is simply:

                      void show( char str[] )
                      {
                      cout << str;
                      }

                      Although in C/C++ we tend to use pointer notation:

                      void show (char* str)
                      {
                      cout << str;
                      }

                      And now, given that your function is doing so little you might question its usefulness and move the code into main:

                      int main()
                      {
                      char str[] = "helloworld!";
                      cout << str;
                      return 0;
                      }

                      Or, finally, you might prefer to use a string constant:

                      int main()
                      {
                      cout << "helloworld!";
                      return 0;
                      }

                      None of which is particularly useful if all you wanted to know is why sizeof() returned 4.

                      S Offline
                      S Offline
                      Stephen Hewitt
                      wrote on last edited by
                      #11

                      I assumed printing a string was used as an example of the general problem of passing an array and determining its size automatically.

                      Steve

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups