F********g 发帖数: 475 | 1 /* add chars */
char test_str[32] = "Mr John Smith ";
void string_add_char(char *str)
{
unsigned int indx = 0;
char string_m[32];
//char *string_m;
while(*(str+indx) != '\0')
{
if(*(str+indx) != ' ')
{
indx++;
}
else
{
indx++;
if(*(str+indx) == ' ')
{
break;
}
else
{
strcpy(string_m,str+indx);
printf("%s\r\n",string_m);
indx += 2;
strcpy(str+indx,string_m);
*(str+indx-3*sizeof(char))='%';
*(str+indx-2*sizeof(char))='2';
*(str+indx-sizeof(char))='0';
printf("%s\r\n",str);
}
}
}
}
int main(void)
{
printf("Before: %s\r\n",test_str);
string_add_char(test_str);
printf("After: %s\r\n",test_str);
return 0;
}
Before: Mr John Smith
John Smith
Mr%20John Smith
Smith
Mr%20John%20Smith
After: Mr%20John%20Smith
把 char string_m[32]换成char *string_m
输出变成
Before: Mr John Smith
John Smith
Mr%20hn Smith
Smith
Mr%20Sm%20
After: Mr%20Sm%20
为甚莫? |
X****r 发帖数: 3557 | 2 You can't just use "char *string_m;" without allocating memory for it.
【在 F********g 的大作中提到】 : /* add chars */ : char test_str[32] = "Mr John Smith "; : void string_add_char(char *str) : { : unsigned int indx = 0; : char string_m[32]; : //char *string_m; : while(*(str+indx) != '\0') : { : if(*(str+indx) != ' ')
|
X****r 发帖数: 3557 | 3 And this is so inefficient. You could have done it in-place
without using any string functions.
【在 F********g 的大作中提到】 : /* add chars */ : char test_str[32] = "Mr John Smith "; : void string_add_char(char *str) : { : unsigned int indx = 0; : char string_m[32]; : //char *string_m; : while(*(str+indx) != '\0') : { : if(*(str+indx) != ' ')
|
F********g 发帖数: 475 | 4 求解:)
【在 X****r 的大作中提到】 : And this is so inefficient. You could have done it in-place : without using any string functions.
|
X****r 发帖数: 3557 | 5 void string_add_char(char *str) {
char *p, *q;
int n = 0;
for (p = str; *p; p++) {
if (*p == ' ') {
if (p[1] == ' ') {
break;
}
n++;
}
}
q = p + n * 2;
*q = *p;
while(p != str) {
if (*--p == ' ') {
*--q = '0';
*--q = '2';
*--q = '%';
} else {
*--q = *p;
}
}
}
【在 F********g 的大作中提到】 : 求解:)
|
F********g 发帖数: 475 | |
r*********r 发帖数: 3195 | |
F********g 发帖数: 475 | 8 哈哈,损的好。
看来COMPILER靠不住哈。以上输出是CODEPAD上的。
在NIOS下的GCC RUN就直接CORE DUMP了。
【在 r*********r 的大作中提到】 : 菜鸟问题都要牛人来回答,太浪费了。
|
p*********t 发帖数: 2690 | 9 char test_str[32]不professional.
建议把数组长度定义为
#define STR_LENGTH 32
【在 F********g 的大作中提到】 : /* add chars */ : char test_str[32] = "Mr John Smith "; : void string_add_char(char *str) : { : unsigned int indx = 0; : char string_m[32]; : //char *string_m; : while(*(str+indx) != '\0') : { : if(*(str+indx) != ' ')
|
l******d 发帖数: 530 | 10 我记得lz好像是做embedded systems下的drivers开发的,按理不应该问这种问题的呀 |
|
|
F********g 发帖数: 475 | 11 又不是啥FANCY的东西好混的很
平时CODING都象小脚老太一样小心翼翼
啥动态内存分配都不用的哈哈
【在 l******d 的大作中提到】 : 我记得lz好像是做embedded systems下的drivers开发的,按理不应该问这种问题的呀
|
p*********t 发帖数: 2690 | 12 做的什么嵌入式的,微波炉,打印机,还是啥别的?
【在 F********g 的大作中提到】 : 又不是啥FANCY的东西好混的很 : 平时CODING都象小脚老太一样小心翼翼 : 啥动态内存分配都不用的哈哈
|
A**u 发帖数: 2458 | 13 牛阿
【在 F********g 的大作中提到】 : 又不是啥FANCY的东西好混的很 : 平时CODING都象小脚老太一样小心翼翼 : 啥动态内存分配都不用的哈哈
|
l******d 发帖数: 530 | 14 你那还招人不,能不能refer我一下,滴水之恩当涌泉相报
【在 F********g 的大作中提到】 : 又不是啥FANCY的东西好混的很 : 平时CODING都象小脚老太一样小心翼翼 : 啥动态内存分配都不用的哈哈
|
F********g 发帖数: 475 | 15 check your msg.
【在 l******d 的大作中提到】 : 你那还招人不,能不能refer我一下,滴水之恩当涌泉相报
|