在C基准传递结构 [英] Pass struct by reference in C

查看:121
本文介绍了在C基准传递结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是code正确吗?它运行如预期,但是这是code正确使用指针和点符号的结构?

 结构someStruct {
 无符号整型总;
};INT测试(结构someStruct *状态){
 语句个总= 4;
}诠释主(){
 结构someStruct S;
 s.total = 5;
 试验(&放大器;多个);
 的printf(\\ ns.total =%d个\\ N,s.total);
}


解决方案

您使用指针和点符号的还是不错的。如果有一个问题,编译器应该给你的错误和/或警告。

下面是你的code与一些附加说明和东西的清单去想,只要利用结构和指针和函数和变量的范围之内。

 结构someStruct {
    无符号整型总;
};/ *
 *修改存在于调用函数的结构体。
 * /
INT测试(结构someStruct *状态){
    语句个总= 4;
    返回0;
}/ *
 *改变了结构的本地副本,原
 *在调用功能不被修改。
 * /
INT TEST2(结构someStruct状态){
    state.total = 8;
    返回0;
}INT TEST3(结构someStruct *状态){
    结构someStruct stateCopy;
    stateCopy = *状态; //使结构的本地副本
    stateCopy.total = 12; //修改结构的本地副本
    *状态= stateCopy; / *拷贝到本地拷贝回原
                              在调用功能。 * /
    返回0;
}诠释主(){
    结构someStruct S;    / *设置,则该值调用,这将改变值的函数。 * /
    s.total = 5;
    试验(&放大器;多个);
    的printf(测试()后:s.total =%d个\\ N,s.total);    / *
     *设置则该值调用将改变其本地副本功能
     *但不是这一个。
     * /
    s.total = 5;
    测试2(S);
    的printf(测试2()后:s.total =%d个\\ N,s.total);    / *
     *调用一个函数,将一个副本,改变副本,
       然后将复制到该之一。
     * /
    TEST3(安培; S);
    的printf(TEST3()后:s.total =%d个\\ N,s.total);    返回0;
}

Is this code correct? It runs as expected, but is this code correctly using the pointers and dot notation for the struct?

struct someStruct {
 unsigned int total;
};

int test(struct someStruct* state) {
 state->total = 4;
}

int main () {
 struct someStruct s;
 s.total = 5;
 test(&s);
 printf("\ns.total = %d\n", s.total);
}

解决方案

Your use of pointer and dot notation is good. The compiler should give you errors and/or warnings if there was a problem.

Here is a copy of your code with some additional notes and things to think about so far as the use of structs and pointers and functions and scope of variables.

struct someStruct {
    unsigned int total;
};

/*
 * Modifies the struct that exists in the calling function.
 */
int test(struct someStruct *state) {
    state->total = 4;
    return 0;
}

/* 
 * Modifies the local copy of the struct, the original
 * in the calling function is not modified.
 */
int test2(struct someStruct state) {
    state.total = 8;
    return 0;
}

int test3(struct someStruct *state) {
    struct someStruct  stateCopy;
    stateCopy = *state;    // make a local copy of the struct
    stateCopy.total = 12;  // modify the local copy of the struct
    *state = stateCopy;    /* copy the local copy back to the original 
                              in the calling function. */
    return 0;
}

int main () {
    struct someStruct s;

    /* Set the value then call a function that will change the value. */
    s.total = 5;
    test(&s);
    printf("after test(): s.total = %d\n", s.total);

    /*
     * Set the value then call a function that will change its local copy 
     * but not this one.
     */
    s.total = 5;
    test2(s);
    printf("after test2(): s.total = %d\n", s.total);

    /* 
     * Call a function that will make a copy, change the copy,
       then put the copy into this one.
     */
    test3(&s);
    printf("after test3(): s.total = %d\n", s.total);

    return 0;
}

这篇关于在C基准传递结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆