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.
  • 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

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

    What error do you get [Compile/RunTime]. Please provide the details

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

    H 1 Reply Last reply
    0
    • _ _AnsHUMAN_

      What error do you get [Compile/RunTime]. Please provide the details

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

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

      Thnaks for your reply I am getting an runtime error... (Unhandled exception...Access violation) Thanks...(anyway,..My brothers name is Anshumaan)

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

      _ 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

        R Offline
        R Offline
        rrrado
        wrote on last edited by
        #4

        What error? I cannot see syntactic error there. Do you allocate memory for temp?

        H 1 Reply Last reply
        0
        • R rrrado

          What error? I cannot see syntactic error there. Do you allocate memory for temp?

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

          I am getting an runtime error... (Unhandled exception...Access violation)

          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

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

            You should must allocate memory for temp. :)

            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

              Thnaks for your reply I am getting an runtime error... (Unhandled exception...Access violation) Thanks...(anyway,..My brothers name is Anshumaan)

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

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

              Ah! that may be because temp is not initialized. Initialize it! And believe it or not, but my brothers name is HIMANGSHU

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

              H 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

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

                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

                C _ H 3 Replies 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

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

                  Sure? :)

                  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]

                  1 Reply Last reply
                  0
                  • C CPallini

                    You should must allocate memory for temp. :)

                    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
                    #10

                    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 S 2 Replies Last reply
                    0
                    • _ _AnsHUMAN_

                      Ah! that may be because temp is not initialized. Initialize it! And believe it or not, but my brothers name is HIMANGSHU

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

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

                      thanks a lot... About the name..that's interesting...I feel somewhat to talk personal staff in this forum. Lets not violet the rules..hehe...Could you give me Gmail Id if you dont mind.We can talk in Gtalk . Regards, Himangshu

                      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

                        _ 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