免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 7323 | 回复: 13
打印 上一主题 下一主题

[C] 结构体(指针)作为函数参数,结果函数内部无法strcpy结构体的某个成员? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-14 21:44 |只看该作者 |倒序浏览
如下的结构定义:
struct XXX{
    char src1[10];
    char src2[10];
};

struct XXX x = {
    "abcd",
    "efghijk",
};

并定义了如下函数:
void func(char *dst, struct XXX s)    //struct XXX*s的话现象一样
{
    strcpy(dst, s.src1);
}

main()中调用这个函数
func(dst, x);

结果,无法将x.a复制到dst中,dst保持原值不变

请问这是怎么回事啊?

论坛徽章:
0
2 [报告]
发表于 2009-08-14 21:56 |只看该作者
最好把代码贴出来,要不然帮不了你

论坛徽章:
0
3 [报告]
发表于 2009-08-14 22:15 |只看该作者
以下是测试代码,请帮看看
问题都在注释中标着呢

#include <stdlib.h>

typedef struct{
        char item1[17];
        char item2[17];
}items_for_select;

int select_item(char *dst, items_for_select src, int *current_idx)
{
        printf("test1:%s, %s\n", items_for_select src.item1,  items_for_select src.item2);
                //这里的item1和item2都可以正确读到
        *current_idx = ~*current_idx&1;                       
        if (current_idx == 0)               
                memcpy(dst, src.item1, strlen(src.item1));  //!!!memcpy和strcpy都不可以copy,strlen(...)+1也不行
        else if (current_idx == 1)
                memcpy(dst, src.item2, strlen(src.item2));  //同上       
        printf("test2:current_idx = %d,  dst = %s\n", *current_idx,  dst);  //dst依然是调用此函数之前的值
        return 0;
}

int main(void)
{
        int select_idx = 1;

        items_for_select rf_items_for_select[] = {
                { "1\n", "2\n" },                               
                { "ENABLE\n", "DISABLE\n" },       
                { "SF", "DF" },
                { NULL, NULL },
                { "ON\n", "OFF\n" },                               
        };
       
        char a[17] = "SK|spawn";
        memcpy(a, rf_items_for_select[1].item1, strlen(rf_items_for_select[1].item1));
        printf("%s",a);    //没问题,可以成功copy

        strcpy(a, "SK|spawn");
        select_item(a, rf_items_for_select[1], &select_idx);
        printf("%s",a);    //这里就copy不了
       
        return 0;
}

[ 本帖最后由 idolspawn 于 2009-8-14 22:18 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2009-08-15 00:01 |只看该作者

回复 #3 idolspawn 的帖子

LZ你确认memcpy有被执行?  current_idx 不是取的select_idx的地址么? 把它和0和1比较是怎么回事?

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
5 [报告]
发表于 2009-08-15 00:05 |只看该作者

回复 #4 kouu 的帖子

int select_item(char *dst, items_for_select src, int *current_idx);

第2个参数不是数组, 是一个值, 从调用处也能看出来:

select_item(a, rf_items_for_select[1], &select_idx);

所以current_idx的意思应该是 ……
取一次item1 , 然后取一次 item2, 再取一次item1 , 再取一次item2 ……

论坛徽章:
0
6 [报告]
发表于 2009-08-15 00:29 |只看该作者

回复 #5 OwnWaterloo 的帖子

int select_item(char *dst, items_for_select src, int *current_idx); 中, current_idx 参数传入的是 &select_idx, 而select_idx是栈上的一个变量, 那么在函数select_item中, current_idx的值就是一个栈上的地址, 这没错吧?

select_item函数中没有写过current_idx的值(只写过*current_idx), 所以判断current_idx==0和current_idx==1的时候, current_idx还是那个栈上的值. 而那两个if显然是不成立的. 那么, if下面的memcpy不就没被执行了?

难道我看走眼了...

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
7 [报告]
发表于 2009-08-15 00:38 |只看该作者

回复 #6 kouu 的帖子

我把你的话理解错了……

原帖由 OwnWaterloo 于 2009-8-15 00:05 发表
int select_item(char *dst, items_for_select src, int *current_idx);
第2个参数不是数组, 是一个值, 从调用处也能看出来:
select_item(a, rf_items_for_select[1], &select_idx);


这些话你就当它不存在吧 ……


        *current_idx = ~*current_idx&1;
这个确实很讨厌 ……  优先级很难记住 ……

但是:
#include <stdio.h>
void f(int* flag) {
    *flag = ~*flag&1;
}
int main() {
    int i = 1;
    int j = 0;
    for (;j<12;++j) {
        f(&i);
        printf("%d\n",i);
    }
    return 0;
}

输出:
0
1
0
1
0
1
0
1
0
1
0
1

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
8 [报告]
发表于 2009-08-15 00:42 |只看该作者

回复 #6 kouu 的帖子

原帖由 kouu 于 2009-8-15 00:29 发表
select_item函数中没有写过current_idx的值(只写过*current_idx), 所以判断current_idx==0current_idx==1的时候, current_idx还是那个栈上的值. 而那两个if显然是不成立的. 那么, if下面的memcpy不就没被执行了?
难道我看走眼了...


我看走眼了我看走眼了……


lz居然写的是:
        if (current_idx == 0) ...
        else if (current_idx == 1) ...

而不是:
if ( *current_idx  == 0 ) ...
else if ( *current_idx == 1) ...


应该就是这个地方错了 …………

[ 本帖最后由 OwnWaterloo 于 2009-8-15 00:43 编辑 ]

论坛徽章:
0
9 [报告]
发表于 2009-08-15 00:43 |只看该作者

回复 #8 OwnWaterloo 的帖子

呵呵, 应该是LZ的笔误.

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
10 [报告]
发表于 2009-08-15 01:36 |只看该作者

回复 #9 kouu 的帖子

这种笔误, 一般都会被警告, 甚至是错误。  没有太多这方面的血的教训……

反而是操作符优先级…… 有时候会被阴……
当然, 是被别人的代码阴……
我倾向于加上 (), 除非特别清晰。


还有
        items_for_select rf_items_for_select[] = {
                { "1\n", "2\n" },                                
                { "ENABLE\n", "DISABLE\n" },        
                { "SF", "DF" },
                { NULL, NULL },
                { "ON\n", "OFF\n" },                                
        };

如果是
struct s {
    int a[2];
    int b[2];
};
struct s[] = {
   { 1,2,3,4 },
   { 2,3,4,5 },
};
是会被一些编译器警告的, 要写作
struct s[] = {
   { {1,2},{3,4} },
   { {2,3},{4,5} },
};

因此也想过, 会不会item2里面全是null。 但分析调用代码, 被cpy的应该是item1……
死都没想到是这种笔误……

还是应该老老实实的, gcc -Wall ...  瞬间明了……
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP