matmatmat
August 22nd, 2009, 05:00 PM
I have this code that strips '-' from a string:
void strip(char *porig, char *pnew)
{
char *p, *p1;
char buf[2];
register int i, j;
memset(buf, 0, sizeof(buf));
p = porig, p1 = pnew;
for(i = 0, j = 0; p[i] != '\0'; ) {
if (p[i] == '\n') break;
if (p[i] == '-') {
i++;
continue;
}
p1[j] = p[i];
i++, j++;
}
}
& this code that calls it:
int top = 0;
int bot = 0;
char test[2];
strip("1-3", test);
printf("%s\n", test);
bot = atoi(&test[0]);
top = atoi(&test[1]);
printf("%i\n%i\n", bot, top);
which prints:
13
13
3
when I expected it to print:
13
1
3
(I'm guessing the &test[0] is passing the whole array?)
What should I do?
void strip(char *porig, char *pnew)
{
char *p, *p1;
char buf[2];
register int i, j;
memset(buf, 0, sizeof(buf));
p = porig, p1 = pnew;
for(i = 0, j = 0; p[i] != '\0'; ) {
if (p[i] == '\n') break;
if (p[i] == '-') {
i++;
continue;
}
p1[j] = p[i];
i++, j++;
}
}
& this code that calls it:
int top = 0;
int bot = 0;
char test[2];
strip("1-3", test);
printf("%s\n", test);
bot = atoi(&test[0]);
top = atoi(&test[1]);
printf("%i\n%i\n", bot, top);
which prints:
13
13
3
when I expected it to print:
13
1
3
(I'm guessing the &test[0] is passing the whole array?)
What should I do?