string concatenation using pointer
-
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
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_
-
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_
-
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
-
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
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] -
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
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_
-
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
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
-
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
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] -
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] -
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_
-
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
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_
-
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
-
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
-
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?
Well you may allocate a predefined amount of memory (on statistical grounds) and then reallocate (for instance, in
C
language, you may usemalloc
andrealloc
) a bigger size if needed. BTW: you know, there areC
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] -
Well you may allocate a predefined amount of memory (on statistical grounds) and then reallocate (for instance, in
C
language, you may usemalloc
andrealloc
) a bigger size if needed. BTW: you know, there areC
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] -
Well you may allocate a predefined amount of memory (on statistical grounds) and then reallocate (for instance, in
C
language, you may usemalloc
andrealloc
) a bigger size if needed. BTW: you know, there areC
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]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
-
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
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] -
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?
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
-
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
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)
-
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]