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. How do I get the size of a character array of unspecified size?

How do I get the size of a character array of unspecified size?

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
8 Posts 6 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.
  • D Offline
    D Offline
    David Z
    wrote on last edited by
    #1

    This is a newbie question but I somehow cannot get this part of my code to work. I have the user input into a character array of unspecified size like this: char *cArray1 = new char[]; cin >> cArray1; and then when I try to calculate how many elements is in the array, the sizeof() function always returns 4 for some reason. I'm calculating the number of elements by the following. size_t iArraySize = sizeof(cArray1)/sizeof(*cArray1); (I also tried int as the datatype instead of size_t but no luck) I always get 4 as the return cause sizeof(cArray1) always gives me 4 and 4/1=4. I think sizeof() cannot be used with dynamically allocated arrays, so is there anyways to do this? :confused: Please give me some advice how so solve this question... Thanks! :)

    M C B T D 6 Replies Last reply
    0
    • D David Z

      This is a newbie question but I somehow cannot get this part of my code to work. I have the user input into a character array of unspecified size like this: char *cArray1 = new char[]; cin >> cArray1; and then when I try to calculate how many elements is in the array, the sizeof() function always returns 4 for some reason. I'm calculating the number of elements by the following. size_t iArraySize = sizeof(cArray1)/sizeof(*cArray1); (I also tried int as the datatype instead of size_t but no luck) I always get 4 as the return cause sizeof(cArray1) always gives me 4 and 4/1=4. I think sizeof() cannot be used with dynamically allocated arrays, so is there anyways to do this? :confused: Please give me some advice how so solve this question... Thanks! :)

      M Offline
      M Offline
      Maciej Pirog
      wrote on last edited by
      #2

      Use strlen function (don't forget to include )! David Z wrote: I always get 4 as the return cause sizeof(cArray1) always gives me 4 and 4/1=4 cArray1 is a pointer, not an array. It points at the first element of an array (null-terminated string). It is 4, because in Win32 pointers are 32-bit (4 * 8 bits - that's why you get 4)!

      Ñ There is only one MP Ð

      M 1 Reply Last reply
      0
      • M Maciej Pirog

        Use strlen function (don't forget to include )! David Z wrote: I always get 4 as the return cause sizeof(cArray1) always gives me 4 and 4/1=4 cArray1 is a pointer, not an array. It points at the first element of an array (null-terminated string). It is 4, because in Win32 pointers are 32-bit (4 * 8 bits - that's why you get 4)!

        Ñ There is only one MP Ð

        M Offline
        M Offline
        Maciej Pirog
        wrote on last edited by
        #3

        Maciej Pirog wrote: (don't forget to include )! Don't forget to include <string.h> of course! :-O

        Ñ There is only one MP Ð

        1 Reply Last reply
        0
        • D David Z

          This is a newbie question but I somehow cannot get this part of my code to work. I have the user input into a character array of unspecified size like this: char *cArray1 = new char[]; cin >> cArray1; and then when I try to calculate how many elements is in the array, the sizeof() function always returns 4 for some reason. I'm calculating the number of elements by the following. size_t iArraySize = sizeof(cArray1)/sizeof(*cArray1); (I also tried int as the datatype instead of size_t but no luck) I always get 4 as the return cause sizeof(cArray1) always gives me 4 and 4/1=4. I think sizeof() cannot be used with dynamically allocated arrays, so is there anyways to do this? :confused: Please give me some advice how so solve this question... Thanks! :)

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

          David Z wrote: sizeof() function always returns 4 because that's the size of a pointer. -c


          For men use, if they have an evil turn, to write it in marble: and whoso doth us a good turn we write it in dust. -- Sir Thomas More

          image effects!

          1 Reply Last reply
          0
          • D David Z

            This is a newbie question but I somehow cannot get this part of my code to work. I have the user input into a character array of unspecified size like this: char *cArray1 = new char[]; cin >> cArray1; and then when I try to calculate how many elements is in the array, the sizeof() function always returns 4 for some reason. I'm calculating the number of elements by the following. size_t iArraySize = sizeof(cArray1)/sizeof(*cArray1); (I also tried int as the datatype instead of size_t but no luck) I always get 4 as the return cause sizeof(cArray1) always gives me 4 and 4/1=4. I think sizeof() cannot be used with dynamically allocated arrays, so is there anyways to do this? :confused: Please give me some advice how so solve this question... Thanks! :)

            B Offline
            B Offline
            BlackSmith
            wrote on last edited by
            #5

            The only reason you get 4 (4 bytes) for sizeof (cArray1) is that sizeof actually measures the size of the cArray1, which is a pointer. And as you probably know.... a size of a pointer is 4 bytes (in your compiler anyway...). char *cArray1 = new char[]; //very bad idea! No array size specified. instead, use fixed sized buffer: char cArray1[ARR_SIZE]; This one you can measure: size_t iArraySize = sizeof (cArray1)/sizeof(*cArray1);**

            --BlackSmith--

            **/*The roof is on fire, we don't need no water, let the MF burn*/. BHG.

            1 Reply Last reply
            0
            • D David Z

              This is a newbie question but I somehow cannot get this part of my code to work. I have the user input into a character array of unspecified size like this: char *cArray1 = new char[]; cin >> cArray1; and then when I try to calculate how many elements is in the array, the sizeof() function always returns 4 for some reason. I'm calculating the number of elements by the following. size_t iArraySize = sizeof(cArray1)/sizeof(*cArray1); (I also tried int as the datatype instead of size_t but no luck) I always get 4 as the return cause sizeof(cArray1) always gives me 4 and 4/1=4. I think sizeof() cannot be used with dynamically allocated arrays, so is there anyways to do this? :confused: Please give me some advice how so solve this question... Thanks! :)

              T Offline
              T Offline
              Tomasz Sowinski
              wrote on last edited by
              #6

              David Z wrote: I think sizeof() cannot be used with dynamically allocated arrays, so is there anyways to do this? Use std::vector and its size() member instead. Tomasz Sowinski -- http://www.shooltz.com

              ** Putt knot yore thrust inn spel chequers. **

              1 Reply Last reply
              0
              • D David Z

                This is a newbie question but I somehow cannot get this part of my code to work. I have the user input into a character array of unspecified size like this: char *cArray1 = new char[]; cin >> cArray1; and then when I try to calculate how many elements is in the array, the sizeof() function always returns 4 for some reason. I'm calculating the number of elements by the following. size_t iArraySize = sizeof(cArray1)/sizeof(*cArray1); (I also tried int as the datatype instead of size_t but no luck) I always get 4 as the return cause sizeof(cArray1) always gives me 4 and 4/1=4. I think sizeof() cannot be used with dynamically allocated arrays, so is there anyways to do this? :confused: Please give me some advice how so solve this question... Thanks! :)

                D Offline
                D Offline
                David Z
                wrote on last edited by
                #7

                Ok, I really feel dumb now hehe. I was out of code for a while so I totally overlooked that fact about the operator referring to the pointer. Thanks for the help. I just used strlen and it works fine. Thanks for all the help! :-D

                1 Reply Last reply
                0
                • D David Z

                  This is a newbie question but I somehow cannot get this part of my code to work. I have the user input into a character array of unspecified size like this: char *cArray1 = new char[]; cin >> cArray1; and then when I try to calculate how many elements is in the array, the sizeof() function always returns 4 for some reason. I'm calculating the number of elements by the following. size_t iArraySize = sizeof(cArray1)/sizeof(*cArray1); (I also tried int as the datatype instead of size_t but no luck) I always get 4 as the return cause sizeof(cArray1) always gives me 4 and 4/1=4. I think sizeof() cannot be used with dynamically allocated arrays, so is there anyways to do this? :confused: Please give me some advice how so solve this question... Thanks! :)

                  A Offline
                  A Offline
                  aritosteles
                  wrote on last edited by
                  #8

                  Well... Your array is actually a pointer, so the function sizeof() will always return 4 because that's the size of a pointer. Getting the number of elements in an array is a different thing. Now, I think you should do this some other way. First of all, why would you want to give the user unlimited input length? Just specify a limit and save yourself a headache. Second, maybe it could help to know the length of the array before allocating any memory... but, well, what is it you want to do? Aritosteles

                  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