theta
asked on
Problem using char *, value being overridden.
I am trying to combine bunch of strings into two char * variables called StanAdd1 and StanAdd2. First I copy..say five strings into StanAdd1 and then I copy couple more into StanAdd2. I do this by first using strcpy and then strcat for each additional char *.
But, as soon as I use strcpy on StanAdd2, my strlen on StanAdd1 changes, and it should not.
Here is a snippet of my code:
void main(int argc, char **argv) {
. . .
char *StanAdd1, *StanAdd2;
. . .
StanAdd1 = calloc(1, 95);
StanAdd2 = calloc(1, 166);
. . .
strcpy(StanAdd1, strtrim(HNumber)); /* 10 chars */
strcat(StanAdd1, strtrim(HNumberSuff)); /* 10 chars */
strcat(StanAdd1, strtrim(StrPrefDir)); /* 3 chars */
strcat(StanAdd1, strtrim(StrPrefType)); /* 20 chars */
strcat(StanAdd1, strtrim(StreetName)); /* 25 chars */
while (strlen(StanAdd1) < 127) strcat(StanAdd1, " ");
printf("1. strlen(StanAdd1): %d.\n", strlen(StanAdd1));
strcpy(StanAdd2, BoxType);
printf("2. strlen(StanAdd1): %d.\n", strlen(StanAdd1));
. . .
} /* end of main */
Program Output:
1. strlen(StanAdd1): 127.
2. strlen(StanAdd1): 119.
The strlen should be 127 on the second line. What am I doing wrong here?
Thanks in advance.
Theta.
But, as soon as I use strcpy on StanAdd2, my strlen on StanAdd1 changes, and it should not.
Here is a snippet of my code:
void main(int argc, char **argv) {
. . .
char *StanAdd1, *StanAdd2;
. . .
StanAdd1 = calloc(1, 95);
StanAdd2 = calloc(1, 166);
. . .
strcpy(StanAdd1, strtrim(HNumber)); /* 10 chars */
strcat(StanAdd1, strtrim(HNumberSuff)); /* 10 chars */
strcat(StanAdd1, strtrim(StrPrefDir)); /* 3 chars */
strcat(StanAdd1, strtrim(StrPrefType)); /* 20 chars */
strcat(StanAdd1, strtrim(StreetName)); /* 25 chars */
while (strlen(StanAdd1) < 127) strcat(StanAdd1, " ");
printf("1. strlen(StanAdd1): %d.\n", strlen(StanAdd1));
strcpy(StanAdd2, BoxType);
printf("2. strlen(StanAdd1): %d.\n", strlen(StanAdd1));
. . .
} /* end of main */
Program Output:
1. strlen(StanAdd1): 127.
2. strlen(StanAdd1): 119.
The strlen should be 127 on the second line. What am I doing wrong here?
Thanks in advance.
Theta.
ASKER
Sorry, following is the correct version of strtrim function:
/*** Trim string, accept for one space at the end of the string ***/
char *strtrim(char *inStr) {
char *result;
char lastchar;
int count=0;
free(result);
result = calloc(1, strlen(inStr));
while (strncmp(inStr+count, " ", 1) == 0) {count++;}
strncat(result, inStr+count, 1);
count++;
while (count <= strlen(inStr)) {
if (strncmp(inStr+count, " ", 1) != 0) {
lastchar = (char)inStr[count];
strncat(result, inStr+count, 1);
}
else {
if (lastchar != ' ') {
lastchar = (char)inStr[count];
strncat(result, inStr+count, 1);
}
}
count++;
}
return result;
}
/*** Trim string, accept for one space at the end of the string ***/
char *strtrim(char *inStr) {
char *result;
char lastchar;
int count=0;
free(result);
result = calloc(1, strlen(inStr));
while (strncmp(inStr+count, " ", 1) == 0) {count++;}
strncat(result, inStr+count, 1);
count++;
while (count <= strlen(inStr)) {
if (strncmp(inStr+count, " ", 1) != 0) {
lastchar = (char)inStr[count];
strncat(result, inStr+count, 1);
}
else {
if (lastchar != ' ') {
lastchar = (char)inStr[count];
strncat(result, inStr+count, 1);
}
}
count++;
}
return result;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes..that was the bug.....which I couldn't see.
Thank you.
Theta.
Thank you.
Theta.
ASKER
/*** Trim string, accept for one space at the end of the string ***/
char strtrim(char *inStr) {
char *result;
char lastchar;
int count=0;
free(result);
result = calloc(1, strlen(inStr));
while (strncmp(inStr+count, " ", 1) == 0) {count++;}
strncat(result, inStr+count, 1);
count++;
while (count <= strlen(inStr)) {
if (strncmp(inStr+count, " ", 1) != 0) {
lastchar = (char)inStr[count];
strncat(result, inStr+count, 1);
}
else {
if (lastchar != ' ') {
lastchar = (char)inStr[count];
strncat(result, inStr+count, 1);
}
}
count++;
}
return *result;
}