Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ampersand character in strings to be concatenated

Status
Not open for further replies.

hopgal

Programmer
Mar 11, 2000
25
US
I want to create a string using strcat, problem is it will contain the ampersand character. How do you handle special characters like this using the string manipulation functions?

Here is my code that is failing on the last line with a memory access violation (I think because & is used in de-referencing pointers):

if (count > 0){//create string to accept AGs
s1= "AcceptStatus0=Y";
lr_output_message("s1 is: %s", s1);
s2 = "&AcceptStatus";
lr_output_message("s2 is: %s", s2);//OK so far
strcat(s1,s2);//fails here
}
 
The problem is probably due to the size of the char array s1 not being large enough to contain the concatenated strings.

The ampersand has no special meaning inside a string, its just like any other character. Try out the following code:

#include <stdio.h>
#include <string.h>

int main() {
char s1[255];
char s2[30];

strcpy(s1, &quot;Wallace &quot;);
strcpy(s2, &quot;& Grommett&quot;);

strcat(s1, s2);

printf(&quot;s1 is %s&quot;, s1);

return 0;
}
 
Yes, that was it. It wasn't the ampersand at all. I am more used to using Strings in Java, which is much easier! Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top