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. Problem with char*

Problem with char*

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++data-structuresperformance
5 Posts 5 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.
  • G Offline
    G Offline
    gReaen
    wrote on last edited by
    #1

    Hello, This might be a basic point in C++ but i need help...... This is the code snippet i've been using: typedef char* myString; main() { myString i="abcd"; myString j="ABCD"; .... } I need to copy j to i. But i'm not able to use usual strcpy. I also tried assigning char by char using for loop. ( i[1]=j[1] etc). But it didn't work.:confused: I dont want to use array format for the string, it should be char* only. Also i dont want to directly point i to j(i=j) coz it might result in memory leak. Plz, suggest some alternative.....

    C CPalliniC D B 4 Replies Last reply
    0
    • G gReaen

      Hello, This might be a basic point in C++ but i need help...... This is the code snippet i've been using: typedef char* myString; main() { myString i="abcd"; myString j="ABCD"; .... } I need to copy j to i. But i'm not able to use usual strcpy. I also tried assigning char by char using for loop. ( i[1]=j[1] etc). But it didn't work.:confused: I dont want to use array format for the string, it should be char* only. Also i dont want to directly point i to j(i=j) coz it might result in memory leak. Plz, suggest some alternative.....

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

      gReaen wrote:

      Also i dont want to directly point i to j(i=j) coz it might result in memory leak.

      No it won't. Anyway, for such string manipulations, it is much easier to use the string class from the STL (std::string).

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

      1 Reply Last reply
      0
      • G gReaen

        Hello, This might be a basic point in C++ but i need help...... This is the code snippet i've been using: typedef char* myString; main() { myString i="abcd"; myString j="ABCD"; .... } I need to copy j to i. But i'm not able to use usual strcpy. I also tried assigning char by char using for loop. ( i[1]=j[1] etc). But it didn't work.:confused: I dont want to use array format for the string, it should be char* only. Also i dont want to directly point i to j(i=j) coz it might result in memory leak. Plz, suggest some alternative.....

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        A typedef isn't enough to transform a string literal (as well a char pointer) into a string. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        [my articles]

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • G gReaen

          Hello, This might be a basic point in C++ but i need help...... This is the code snippet i've been using: typedef char* myString; main() { myString i="abcd"; myString j="ABCD"; .... } I need to copy j to i. But i'm not able to use usual strcpy. I also tried assigning char by char using for loop. ( i[1]=j[1] etc). But it didn't work.:confused: I dont want to use array format for the string, it should be char* only. Also i dont want to directly point i to j(i=j) coz it might result in memory leak. Plz, suggest some alternative.....

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

          gReaen wrote:

          But it didn't work.

          The only way that would work would be to change the declarations to:

          char i[] = "abcd";
          char j[] = "ABCD";

          See here for the difference in why one can be changed and not the other.

          "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

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

          1 Reply Last reply
          0
          • G gReaen

            Hello, This might be a basic point in C++ but i need help...... This is the code snippet i've been using: typedef char* myString; main() { myString i="abcd"; myString j="ABCD"; .... } I need to copy j to i. But i'm not able to use usual strcpy. I also tried assigning char by char using for loop. ( i[1]=j[1] etc). But it didn't work.:confused: I dont want to use array format for the string, it should be char* only. Also i dont want to directly point i to j(i=j) coz it might result in memory leak. Plz, suggest some alternative.....

            B Offline
            B Offline
            Bram van Kampen
            wrote on last edited by
            #5

            You should never loose sight of the code generated by the compiler. In your case the Compiler will include two char arrays into the excecutable, namely "abcd" and "ABCB" The fact that you Tyedefed them as myString has confused you more than the compiler. You cannot copy i to j, because both are in memory reserved for string constants. In other words: Both are in 'read only' memory. I strongly suggest that you get a copyof the excelent book written by Brian Kernighan and Denis Ritchie about the 'C' language (Never mind CPP). And rephrase the question above after reading these books. If you do the appropriate amount of study,you will realise that in the above example code fragments, memoryleaks are not even close to being an issue.

            Bram van Kampen

            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