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. C string operations

C string operations

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 Posts 8 Posters 1 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.
  • U Offline
    U Offline
    User 10929916
    wrote on last edited by
    #1

    if I have this code : char * s =(char*)malloc(10); and then I write 's="test"' then s will have a different address right ? why is this happening ?

    M L P C L 5 Replies Last reply
    0
    • U User 10929916

      if I have this code : char * s =(char*)malloc(10); and then I write 's="test"' then s will have a different address right ? why is this happening ?

      M Offline
      M Offline
      Michael_Davies
      wrote on last edited by
      #2

      s is a pointer, when you assign a value to s it should be an address not a literal. You allocate 10 bytes of memory and s holds the address of that memory. To assign a literal to the memory pointed to by s use *s = "test";

      1 Reply Last reply
      0
      • U User 10929916

        if I have this code : char * s =(char*)malloc(10); and then I write 's="test"' then s will have a different address right ? why is this happening ?

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

        You are allocating assigning the address of the string literal "test" to the pointer s thus overwriting the address returned by malloc. You should be using strcpy to copy the four characters into the allocated buffer. [edit] better terminology as pointed out by member below. [/edit]

        C U 2 Replies Last reply
        0
        • L Lost User

          You are allocating assigning the address of the string literal "test" to the pointer s thus overwriting the address returned by malloc. You should be using strcpy to copy the four characters into the allocated buffer. [edit] better terminology as pointed out by member below. [/edit]

          C Offline
          C Offline
          Chris Losinger
          wrote on last edited by
          #4

          or, even better, strncpy.

          image processing toolkits | batch image processing

          L 1 Reply Last reply
          0
          • C Chris Losinger

            or, even better, strncpy.

            image processing toolkits | batch image processing

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

            Absolutely right, but I was trying to KISS.

            1 Reply Last reply
            0
            • U User 10929916

              if I have this code : char * s =(char*)malloc(10); and then I write 's="test"' then s will have a different address right ? why is this happening ?

              P Offline
              P Offline
              Patrice T
              wrote on last edited by
              #6

              Member 10964099 wrote:

              why is this happening ?

              It append because it designed this way. That is how pointers work. I recommend to read THE book "the C Language" from Kernigan & Ritchie. The C Programming Language - Wikipedia, the free encyclopedia[^]

              Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

              1 Reply Last reply
              0
              • U User 10929916

                if I have this code : char * s =(char*)malloc(10); and then I write 's="test"' then s will have a different address right ? why is this happening ?

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                Because s is a pointer.

                char * s =(char*)malloc(10); // here s points to the freshly allocated memory buffer

                s="test" // here s points to the memory containing the string literal

                1 Reply Last reply
                0
                • U User 10929916

                  if I have this code : char * s =(char*)malloc(10); and then I write 's="test"' then s will have a different address right ? why is this happening ?

                  L Offline
                  L Offline
                  leon de boer
                  wrote on last edited by
                  #8

                  What all the others said and more generally rarely if ever do use an equal sign on a string in C at any other time than creation. There are functions required to add, copy, compare, split and search strings and you can't just use mathematical symbols like "=" and "+" to do things with strings like you can in some higher languages. Strings are not mathematical types and you can't use mathematical functions to do things with them. Those coming from a coding background in Java or Basic tend to do what you did and you have to unlearn it when using C because "string1" + "string2" will not work either, you will need to use strcat functions. String compares are likewise a function and you can't use mathematical equality signs to compare two strings. Learning the string functions, it is a requirement of learning the C language.

                  In vino veritas

                  1 Reply Last reply
                  0
                  • L Lost User

                    You are allocating assigning the address of the string literal "test" to the pointer s thus overwriting the address returned by malloc. You should be using strcpy to copy the four characters into the allocated buffer. [edit] better terminology as pointed out by member below. [/edit]

                    U Offline
                    U Offline
                    User 12528795
                    wrote on last edited by
                    #9

                    Shouldn't that be: "you are assigning the address" instead of allocating?

                    L 1 Reply Last reply
                    0
                    • U User 12528795

                      Shouldn't that be: "you are assigning the address" instead of allocating?

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

                      Of course.

                      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