高分求救,字符串赋值给结构体

wxdcxp 2010-06-02 11:47:46

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

typedef struct _test
{
char a[3];
char b[3];
char c[3];

}test;
//去除右边空格
int trim_right(char *str, int len)
{
while (len > 0) {
if (str[len-1] != 0x20)
break;
--len;
}
str[len] = '\0';
return len;
}

int main(void)
{
char c[20] = "12 3456 ";
char d[20] = {0};
test t = {0};

memcpy(&t,c,20);
trim_right(t.a, sizeof(t.a));
trim_right(t.b,sizeof(t.b));


sprintf((char *)d, "a: %s\r\n", t.a);
sprintf((char *)d, "%sb: %s\r\n", d, t.b);
sprintf((char *)d, "%sc: %s\r\n",d, t.c);
puts(d);

return 0;


}



比如我有一个字符串12 3456 ,如何把这个字符串赋值给结构体,使得输出的结果是
a:12
b:345
c:6

目前只能输出
a: 12
b:345
c:
因为数组b的大小是3,所以只能把空格存放到数组c里导致数组c不能输出内容。想了很多方法都无法实现,请高手帮忙解答

...全文
169 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
东莞某某某 2010-06-03
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>

typedef struct _test
{
char a[3];
char b[3];
char c[3];

}test;
//遍历字符串,去掉空格,并存放入结构中
void trim_right_cpy(char * str,test *pt)
{
char *p;
int i,j;
for(i=0;i<=6;i+=3)
{
p=(char *)pt+i;
for(j=0;j<3;j++)
{
if( str[i+j]!=' ' && str[i+j] )
{
*(p++)=str[i+j];
}
}
}
}



int main(void)
{
char c[20] = " 345 3 4535 ";
char d[20] = {0};
test t = {0};

trim_right_cpy(c, &t);


sprintf(d, "a:%c%c%c", t.a[0],t.a[1],t.a[2]);
sprintf(d, "%s\nb:%c%c%c", d, t.b[0],t.b[1],t.b[2]);
sprintf(d, "%s\nc:%c%c%c", d, t.c[0],t.c[1],t.c[2]);
puts(d);

return 0;

}

amekin 2010-06-03
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 nbda1121440 的回复:]

楼主,我想请教一下memcpy是干什么的
我是新手,不懂
[/Quote]

这个 是内存copy,
第一个参数是 目标地址 你要copy到哪里去
第二个参数是 源地址 你的数据哪里来
第三个参数是 copy长度 你要copy的长度
bobo364 2010-06-03
  • 打赏
  • 举报
回复
也就是从一个字符串中(不是字符数组),中复制设定大小的字符到另一个字符串(从头开始复制),所谓的重叠指新的串有字符
bobo364 2010-06-03
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 nbda1121440 的回复:]
楼主,我想请教一下memcpy是干什么的
我是新手,不懂
[/Quote]

memcpy
语法:




#include <string.h>
void *memcpy( void *to, const void *from, size_t count );


功能:函数从from中复制count 个字符到to中,并返回to指针。 如果to 和 from 重叠,则函数行为不确定。
bobo364 2010-06-03
  • 打赏
  • 举报
回复
明白了2楼的代码,原来那个去空格,是只要右边没空格,直接退出循环,有的话长度减一
#include<stdio.h>

typedef struct _test
{
char a[3+1];
char b[3+1];
char c[3+1]; //字符串操作,需要+1的空间保存\0字符
}test;


//去除右边空格
int trim_right(char *str, int len)
{
while (len > 0)
{
if (str[len-1]!= 0x20)
{
break;
}
else
{
--len;
str[len] = '\0';
}
}
return len;
}


int main()
{
char c[20] = "12 3456 ";
char d[20] = {0};
test t = {0};

//memcpy(&t,c,20);
strncpy(t.a,c,sizeof(t.a)-1); //strncpy指定字节数即可
strncpy(t.b,c+sizeof(t.a)-1,sizeof(t.b)-1); //注意计算copy的位置
strncpy(t.c,c+sizeof(t.a)+sizeof(t.b)-2,sizeof(t.c)-1);
trim_right(t.a, strlen(t.a));
trim_right(t.b,strlen(t.b));
trim_right(t.c,strlen(t.c));//用字符串操作函数


sprintf((char *)d, "a: %s\r\n", t.a);
sprintf((char *)d, "%sb: %s\r\n", d, t.b);
sprintf((char *)d, "%sc: %s\r\n",d, t.c);
puts(d);
system("pause");
return 0;
}
goodluckme2013 2010-06-03
  • 打赏
  • 举报
回复
//打印
printf("%c",t.c[0]);
周靖峰 2010-06-03
  • 打赏
  • 举报
回复
楼主,我想请教一下memcpy是干什么的
我是新手,不懂
pengzhixi 2010-06-03
  • 打赏
  • 举报
回复
用strncpy吧,这样你可以控制复制过去的字符数。
wxdcxp 2010-06-03
  • 打赏
  • 举报
回复
问题是别人定协议时没有把空格考虑在内,协议是跟其他平台定的不能改
比如日期201005他定义的长度char data[6],这样导致日期后面的数据无法输出。现在我在想有什么办法
空间定义没有包括结束符时任然能够正常输出
Mg 2010-06-03
  • 打赏
  • 举报
回复
顶上面的,拷贝的时候需要控制字节数,而且需要给'\0'预留空间~
cattycat 2010-06-03
  • 打赏
  • 举报
回复
对char数组最后应该有一个\0表示结束。用strncpy复制的时候控制字节数。
jixingzhong 2010-06-03
  • 打赏
  • 举报
回复
由于输出时%s,所以,必须有\0以保证操作正常


typedef struct _test
{
char a[3+1];
char b[3+1];
char c[3+1]; //字符串操作,需要+1的空间保存\0字符

}test;
//去除右边空格
int trim_right(char *str, int len)
{
while (len > 0) {
if (str[len-1] != 0x20)
break;
--len;
}
str[len] = '\0';
return len;
}


int main(int argc, char* argv[])
{
char c[20] = "12 3456 ";
char d[20] = {0};
test t = {0};

//memcpy(&t,c,20);
strncpy(t.a,c,sizeof(t.a)-1); //strncpy指定字节数即可
strncpy(t.b,c+sizeof(t.a)-1,sizeof(t.b)-1); //注意计算copy的位置
strncpy(t.c,c+sizeof(t.a)+sizeof(t.b)-2,sizeof(t.c)-1);
trim_right(t.a, strlen(t.a));
trim_right(t.b,strlen(t.b));
trim_right(t.c,strlen(t.c));//用字符串操作函数


sprintf((char *)d, "a: %s\r\n", t.a);
sprintf((char *)d, "%sb: %s\r\n", d, t.b);
sprintf((char *)d, "%sc: %s\r\n",d, t.c);
puts(d);

return 0;
}
qm65325790 2010-06-03
  • 打赏
  • 举报
回复
liutengfeigo 2010-06-03
  • 打赏
  • 举报
回复

怎么又发贴?

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧