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. String

String

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
8 Posts 4 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.
  • F Offline
    F Offline
    Fareed Rizkalla
    wrote on last edited by
    #1

    If I assign a string to a variable. char* t = "Hello World!"; Doesn't this string get a fixed array with a null terminator at the end?

    L M L 3 Replies Last reply
    0
    • F Fareed Rizkalla

      If I assign a string to a variable. char* t = "Hello World!"; Doesn't this string get a fixed array with a null terminator at the end?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Yes. Take a look at it in the debugger if you are not sure.

      Just say 'NO' to evaluated arguments for diadic functions! Ash

      F 1 Reply Last reply
      0
      • F Fareed Rizkalla

        If I assign a string to a variable. char* t = "Hello World!"; Doesn't this string get a fixed array with a null terminator at the end?

        M Offline
        M Offline
        Moak
        wrote on last edited by
        #3

        No. It assigns a pointer to a fixed null-terinated string, there is a slight difference between arrays and pointers. A writable block of memory is not allocated and you should get a compiler warning because of the unsafe conversion... change the code to: const char* t = "Hello World!"; Hope this helps :)

        Chat in Europe :java: Now with 24% more Twitter

        L 1 Reply Last reply
        0
        • L Lost User

          Yes. Take a look at it in the debugger if you are not sure.

          Just say 'NO' to evaluated arguments for diadic functions! Ash

          F Offline
          F Offline
          Fareed Rizkalla
          wrote on last edited by
          #4

          wcscat_s keeps throwing an error that the string isn't null terminated! :S

          M L 2 Replies Last reply
          0
          • F Fareed Rizkalla

            wcscat_s keeps throwing an error that the string isn't null terminated! :S

            M Offline
            M Offline
            Moak
            wrote on last edited by
            #5

            Are you sure it doesn't throw an exception, because the memory provided in strDestination argument is read-only?

            Chat in Europe :java: Now with 24% more Twitter

            1 Reply Last reply
            0
            • F Fareed Rizkalla

              If I assign a string to a variable. char* t = "Hello World!"; Doesn't this string get a fixed array with a null terminator at the end?

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              every literal string gets a NULL appended automatically by the compiler. However there are 8-bit and 16-bit characters and strings, and you can't just mix them. If you use wcscat_s, you want wide char strings, the example in MSDN is:

              wchar_t wszStr[] = L"1a1g";

              :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

              1 Reply Last reply
              0
              • F Fareed Rizkalla

                wcscat_s keeps throwing an error that the string isn't null terminated! :S

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Fareed Rizkalla wrote:

                wcscat_s keeps throwing an error that the string isn't null terminated!

                Yes, I'm not surprised since you are sending it a char* (i.e. ASCII string) but wcscat_s() expects Unicode.

                Just say 'NO' to evaluated arguments for diadic functions! Ash

                1 Reply Last reply
                0
                • M Moak

                  No. It assigns a pointer to a fixed null-terinated string, there is a slight difference between arrays and pointers. A writable block of memory is not allocated and you should get a compiler warning because of the unsafe conversion... change the code to: const char* t = "Hello World!"; Hope this helps :)

                  Chat in Europe :java: Now with 24% more Twitter

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Moak wrote:

                  a pointer to a fixed null-terinated string,

                  Which is simply an array of characters.

                  Moak wrote:

                  there is a slight difference between arrays and pointers.

                  I would say there is a huge difference.

                  Just say 'NO' to evaluated arguments for diadic functions! Ash

                  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