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. size of char *

size of char *

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structurestutorial
7 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.
  • B Offline
    B Offline
    baerten
    wrote on last edited by
    #1

    Hi everybody, i have a class which stores char arrays, for example char name[50]; If i write int test = sizeof(inst->name); test has the value 50, which his OK. Now i like to send a the char array to a function, there'in it has the form char* and i sizeof() of this char* returns only the size of the pointer or something like this. How can i find the number of allocated chars from this char* ? Big thanks ! :)

    N C W D N 6 Replies Last reply
    0
    • B baerten

      Hi everybody, i have a class which stores char arrays, for example char name[50]; If i write int test = sizeof(inst->name); test has the value 50, which his OK. Now i like to send a the char array to a function, there'in it has the form char* and i sizeof() of this char* returns only the size of the pointer or something like this. How can i find the number of allocated chars from this char* ? Big thanks ! :)

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      baerten wrote:

      How can i find the number of allocated chars from this char*

      you can use _msize() to find the size of buffer allocated in heap. I.e the function will work in the secnarios like char *pszName = new char[50]; fun( pszName ); but not for char szName[50]; fun( szName ); So to solve such pblms we usually pass the size of array also to such functions.

      nave [OpenedFileFinder]

      1 Reply Last reply
      0
      • B baerten

        Hi everybody, i have a class which stores char arrays, for example char name[50]; If i write int test = sizeof(inst->name); test has the value 50, which his OK. Now i like to send a the char array to a function, there'in it has the form char* and i sizeof() of this char* returns only the size of the pointer or something like this. How can i find the number of allocated chars from this char* ? Big thanks ! :)

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        You should prefer strlen which will check the real size of your null-terminated string and not the size of the 'container'. So, if you copy "Hello" in your buffer, strlen will return 5 and not 50. Is that what you are looking for ? If no, then you should maybe elaborate a little bit more.


        Cédric Moonen Software developer
        Charting control [v1.2]

        1 Reply Last reply
        0
        • B baerten

          Hi everybody, i have a class which stores char arrays, for example char name[50]; If i write int test = sizeof(inst->name); test has the value 50, which his OK. Now i like to send a the char array to a function, there'in it has the form char* and i sizeof() of this char* returns only the size of the pointer or something like this. How can i find the number of allocated chars from this char* ? Big thanks ! :)

          W Offline
          W Offline
          Waldermort
          wrote on last edited by
          #4

          short answer, you can't. When passing arrays around, it is always good practice to also send the number of items, or use one of the STL templates like std::vector

          Waldermort

          1 Reply Last reply
          0
          • B baerten

            Hi everybody, i have a class which stores char arrays, for example char name[50]; If i write int test = sizeof(inst->name); test has the value 50, which his OK. Now i like to send a the char array to a function, there'in it has the form char* and i sizeof() of this char* returns only the size of the pointer or something like this. How can i find the number of allocated chars from this char* ? Big thanks ! :)

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

            Have you not ever used functions that take a buffer and the size of that buffer as arguments?


            "A good athlete is the result of a good and worthy opponent." - David Crow

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            1 Reply Last reply
            0
            • B baerten

              Hi everybody, i have a class which stores char arrays, for example char name[50]; If i write int test = sizeof(inst->name); test has the value 50, which his OK. Now i like to send a the char array to a function, there'in it has the form char* and i sizeof() of this char* returns only the size of the pointer or something like this. How can i find the number of allocated chars from this char* ? Big thanks ! :)

              N Offline
              N Offline
              Nemanja Trifunovic
              wrote on last edited by
              #6

              That's called "array decay" :) Just google for the term and you'll find the explanation.


              Programming Blog utf8-cpp

              1 Reply Last reply
              0
              • B baerten

                Hi everybody, i have a class which stores char arrays, for example char name[50]; If i write int test = sizeof(inst->name); test has the value 50, which his OK. Now i like to send a the char array to a function, there'in it has the form char* and i sizeof() of this char* returns only the size of the pointer or something like this. How can i find the number of allocated chars from this char* ? Big thanks ! :)

                S Offline
                S Offline
                Signal 9
                wrote on last edited by
                #7

                one word (sorta) std::string myfancystringclass;

                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