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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. char str[256] AND char *str = new char(256)

char str[256] AND char *str = new char(256)

Scheduled Pinned Locked Moved C / C++ / MFC
help
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.
  • I Offline
    I Offline
    Igor Mihailov
    wrote on last edited by
    #1

    Hi, people. Can sommebody explain me the difference between 1. char str[256] 2. char *str = new char(256) I thought it's equevalent, but my program works good with 1st and fails sometimes when I use the second variant. Unfortunately I can't watch when it happens. Help me

    K G A 3 Replies Last reply
    0
    • I Igor Mihailov

      Hi, people. Can sommebody explain me the difference between 1. char str[256] 2. char *str = new char(256) I thought it's equevalent, but my program works good with 1st and fails sometimes when I use the second variant. Unfortunately I can't watch when it happens. Help me

      K Offline
      K Offline
      Kosk
      wrote on last edited by
      #2

      I think what you want is: char *str = new char [256]; [] instead of ()

      1 Reply Last reply
      0
      • I Igor Mihailov

        Hi, people. Can sommebody explain me the difference between 1. char str[256] 2. char *str = new char(256) I thought it's equevalent, but my program works good with 1st and fails sometimes when I use the second variant. Unfortunately I can't watch when it happens. Help me

        G Offline
        G Offline
        Gary R Wheeler
        wrote on last edited by
        #3

        Kosk has the correct answer, but here's a little explanation: The first form is an array of 256 char's. The second form returns a pointer to a single char, initialized to the value 256 (which actually initializes the value to zero, which is the lower 8 bits that fit into a single char). What you actually want is char *str = new char[256];, which sets str to a pointer that is an array of 256 char's. Note that, with this form, when you no longer need the array, you should use delete []str.


        Software Zen: delete this;

        1 Reply Last reply
        0
        • I Igor Mihailov

          Hi, people. Can sommebody explain me the difference between 1. char str[256] 2. char *str = new char(256) I thought it's equevalent, but my program works good with 1st and fails sometimes when I use the second variant. Unfortunately I can't watch when it happens. Help me

          A Offline
          A Offline
          Archer282
          wrote on last edited by
          #4

          char str[256]; is an array of characters (actually unsigned integers) char *str = new char[256]; is a pointer to an array of characters stored in memory this is mostly used in functions that take an output parameter.

          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