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. declaring const strings

declaring const strings

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 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.
  • C Offline
    C Offline
    Chintoo723
    wrote on last edited by
    #1

    static const char str[3] = {'a','b','c'}; static const char str[3] = "abc"; what is the difference between the above two? or are they same? is the first one null terminated? why do some folks use the first one when the second one is easier.

    L R R 3 Replies Last reply
    0
    • C Chintoo723

      static const char str[3] = {'a','b','c'}; static const char str[3] = "abc"; what is the difference between the above two? or are they same? is the first one null terminated? why do some folks use the first one when the second one is easier.

      L Offline
      L Offline
      LighthouseJ
      wrote on last edited by
      #2

      I think the second is null terminated. I used something similar to the first, but I specified the characters in hexidecimal form like this: static const char cmd[3] = { 0x0A, 0x10, 0x0D }; I had to use characters not easily expressible on a regular Qwerty keyboard and I also used only characters 2 and 3 sometimes. Character arrays were convenient because the serial port sends 1 byte at a time and characters are 1 byte long.

      1 Reply Last reply
      0
      • C Chintoo723

        static const char str[3] = {'a','b','c'}; static const char str[3] = "abc"; what is the difference between the above two? or are they same? is the first one null terminated? why do some folks use the first one when the second one is easier.

        R Offline
        R Offline
        Rage_bla
        wrote on last edited by
        #3

        The first one is an array of three characters and it is not 0 terminated. The second one won't even compile. The static const char str[4] = "abc" will compile - "abc" is 0 terminated thus the array has 4 members.

        1 Reply Last reply
        0
        • C Chintoo723

          static const char str[3] = {'a','b','c'}; static const char str[3] = "abc"; what is the difference between the above two? or are they same? is the first one null terminated? why do some folks use the first one when the second one is easier.

          R Offline
          R Offline
          Rick York
          wrote on last edited by
          #4

          I think that most people do this : static const char *str = "abc";

          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