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 concatenation using pointer

string concatenation using pointer

Scheduled Pinned Locked Moved C / C++ / MFC
help
21 Posts 7 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.
  • D Divyang Mithaiwala

    A code you have past here is the exact code that is in your system? But if you have taken temp as array (char temp[XX]) then you might get error which you told. But a code that we are sawing has no that kind of error.


    Do not trust a computer... Always check what computer is doing regards, Divyang Mithaiwala Software Engineer

    modified on Thursday, May 7, 2009 5:38 AM

    _ Offline
    _ Offline
    _AnsHUMAN_
    wrote on last edited by
    #12

    Divyang Mithaiwala wrote:

    Because if you have taken temp as array (char temp[XX]) then you might get error

    :doh: Why so?

    Divyang Mithaiwala wrote:

    But a code that we are sawing has no that kind of error.

    Did you trust your computer to check if there was a runtime error? :)

    You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

    1 Reply Last reply
    0
    • D Divyang Mithaiwala

      A code you have past here is the exact code that is in your system? But if you have taken temp as array (char temp[XX]) then you might get error which you told. But a code that we are sawing has no that kind of error.


      Do not trust a computer... Always check what computer is doing regards, Divyang Mithaiwala Software Engineer

      modified on Thursday, May 7, 2009 5:38 AM

      H Offline
      H Offline
      hrishiS
      wrote on last edited by
      #13

      sorry, But I really didnot get that

      ----------------------------- I am a beginner

      D 1 Reply Last reply
      0
      • H hrishiS

        sorry, But I really didnot get that

        ----------------------------- I am a beginner

        D Offline
        D Offline
        Divyang Mithaiwala
        wrote on last edited by
        #14

        When I was posted I haven't seen your clarification that you are facing run time error (due to no refresh page). So, If you have taken variable as char temp[10]; and try to execute *temp++ = *a++; it will give you compile time error.


        Do not trust a computer... Always check what computer is doing regards, Divyang Mithaiwala Software Engineer

        1 Reply Last reply
        0
        • H hrishiS

          thanks But there is one problem. How much memory should I allocate? Since It is a program for concatenation of string...And I wont be knowing the no of characters after contcatination(ie the size of the temp) in advance. How to go about it?

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

          Well you may allocate a predefined amount of memory (on statistical grounds) and then reallocate (for instance, in C language, you may use malloc and realloc) a bigger size if needed. BTW: you know, there are C runtime library functions for the purpose of concatenating strings. :)

          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.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          H 2 Replies Last reply
          0
          • C CPallini

            Well you may allocate a predefined amount of memory (on statistical grounds) and then reallocate (for instance, in C language, you may use malloc and realloc) a bigger size if needed. BTW: you know, there are C runtime library functions for the purpose of concatenating strings. :)

            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.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            H Offline
            H Offline
            hrishiS
            wrote on last edited by
            #16

            yes But I wanted to implement it with my own code....So that I learn pointer concept too..

            ----------------------------- I am a beginner

            1 Reply Last reply
            0
            • C CPallini

              Well you may allocate a predefined amount of memory (on statistical grounds) and then reallocate (for instance, in C language, you may use malloc and realloc) a bigger size if needed. BTW: you know, there are C runtime library functions for the purpose of concatenating strings. :)

              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.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              H Offline
              H Offline
              hrishiS
              wrote on last edited by
              #17

              I used the following code.....No error but not giving any results char *a,*b; char *temp; temp=new char[5]; a="aa"; b="bb"; while(*a!='\0') { *temp++ =*a++ ; } while(*b!='\0') { *temp++= *b++ ; } *temp = '\0'; printf("%s",temp);

              ----------------------------- I am a beginner

              C 1 Reply Last reply
              0
              • H hrishiS

                I used the following code.....No error but not giving any results char *a,*b; char *temp; temp=new char[5]; a="aa"; b="bb"; while(*a!='\0') { *temp++ =*a++ ; } while(*b!='\0') { *temp++= *b++ ; } *temp = '\0'; printf("%s",temp);

                ----------------------------- I am a beginner

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

                That's almost the solution... :rolleyes:, try:

                char *a,*b;
                char *temp, *dest;
                dest=new char[5];
                temp = dest;
                a="aa";
                b="bb";
                while(*a!='\0')
                {
                *temp++ =*a++ ;
                }
                while(*b!='\0')
                {
                *temp++= *b++ ;
                }
                *temp = '\0';
                printf("%s",dest);

                of course you need now to generalize the code (ad remember to free dest when you no longer need it) :)

                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.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                H 1 Reply Last reply
                0
                • H hrishiS

                  thanks But there is one problem. How much memory should I allocate? Since It is a program for concatenation of string...And I wont be knowing the no of characters after contcatination(ie the size of the temp) in advance. How to go about it?

                  S Offline
                  S Offline
                  Stuart Dootson
                  wrote on last edited by
                  #19

                  himangshuS wrote:

                  I wont be knowing the no of characters after contcatination(ie the size of the temp) in advance.

                  Yes you can - you need to allocate strlen(A) + strlen(B) + 1 characters, where strlen is defined as follows:

                  int strlen(const char* s)
                  {
                  int count;
                  while (*(s++))
                  ++count;
                  }

                  Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                  1 Reply Last reply
                  0
                  • H hrishiS

                    Hi to All, Here I am getting an error. Breaking my head but not able to get it done. Please help me out with. I know I have done some silly mistake,..But not able to view it Mu code is... char *a,*b,*temp; a="aa"; b="bb"; while(*a!='\0') { *temp++ = *a++; } while(*b!='\0') { *temp++ = *b++; } *temp = '\0'; .... its showing error in the line...*temp++ = *a++; Thanks in advance

                    F Offline
                    F Offline
                    Fatbuddha 1
                    wrote on last edited by
                    #20

                    I haven't tested that nor tried something similar, but I think that the hole think wont give you the right result anyway. By incrementing the char * temp it jumps to the next position and you will lose the previous position, don't you? So if you really want to do something like that you should have a pointer to the first position of temp (I guess). So why not simply allocate mem for temp and then do a memcpy with a and b? Given memcpy temp and for b &temp[2] for example. I hope this will do the trick, but I am just guessing. Cheers

                    You have the thought that modern physics just relay on assumptions, that somehow depends on a smile of a cat, which isn’t there.( Albert Einstein)

                    1 Reply Last reply
                    0
                    • C CPallini

                      That's almost the solution... :rolleyes:, try:

                      char *a,*b;
                      char *temp, *dest;
                      dest=new char[5];
                      temp = dest;
                      a="aa";
                      b="bb";
                      while(*a!='\0')
                      {
                      *temp++ =*a++ ;
                      }
                      while(*b!='\0')
                      {
                      *temp++= *b++ ;
                      }
                      *temp = '\0';
                      printf("%s",dest);

                      of course you need now to generalize the code (ad remember to free dest when you no longer need it) :)

                      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.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      H Offline
                      H Offline
                      hrishiS
                      wrote on last edited by
                      #21

                      Oh thanks a lot...its working fine... I have to increase my knowledge towards pointer

                      ----------------------------- I am a beginner

                      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