繁体   English   中英

将struct成员传递给C函数

[英]Passing a struct member into functions in C

我正在尝试使用结构中的成员之一通过计算function()传递它。 此函数将为我的变量计算一个新值。 您能告诉我如何将变量传递到主函数中吗? 我还保留了我的三个职能。 谢谢

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

我的原型:

void calculations_using_struct_values(int i);

我的结构:

struct test
{
    int x,y,z;
};

我的主要:

int main()
{
    calculations_using_struct_values();
    return 0;
}

初始化变量的值:

void struct_values()
{
    test variable;

    variable.x=50;
    variable.y=100;
    variable.z=150;

    calculations_using_struct_values(variable.x);
    return;
}

我将我的variable.x存储到i中,以便将该函数加5:

void calculations_using_struct_values(int i)
{
    int a=5;
    i += a;
    printf("%d\n",i);
    return;
}

您的函数可以使用指向int的指针。

void calculations_using_struct_values(int *i)
{
    int a=5;
    *i += a;
}

并将您的struct成员的地址传递给函数( & ):

void struct_values()
{
    //as before

    calculations_using_struct_values(&variable.x);
}

请参阅: 在C中通过引用传递

或者,如果需要,您可以传递整个结构:

void calculations_using_struct_values(struct test *s)
{
    int a=5;
    s->x += a;
}

并将您的结构的地址传递给函数( & ):

void struct_values()
{
    //as before

    calculations_using_struct_values(&variable);
}

您可以让函数返回值而不是void。

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

struct test
{
    int x,y,z;
};

int calculations_using_struct_values(int i)
{
    int a=5;
    i += a;
    printf("%d\n",i);
    return i;
}

struct test struct_values()
{
    struct test variable;

    variable.x=50;
    variable.y=100;
    variable.z=150;

    variable.x = calculations_using_struct_values(variable.x);
    return variable;
}

int main( int argc, char *argv[])
{
    struct test values;

    values = struct_values();
    printf("%d\n",values.x);

    return 0;
}

如果要在适当位置修改值,则需要使用指针:

void calculations_using_struct_values(int *i)
{
    int a=5;
    *i += a;
    printf("%d\n", *i);
    return;
}

...
    calculations_using_struct_values(&variable.x);

这是一组呼叫,显示了呼叫的不同结果。 请注意地址和值在传递给函数后以及函数返回时的行为。 取决于您的编译器,可能不支持引用,因为它们不是ansi-c。

#include <stdio.h>

void AddByValue(int i)
{
    printf("%8s: address: 0x%X value: %i\n", "Val A", &i, i);
    i = i + 1;
    printf("%8s: address: 0x%X value: %i\n", "Val B", &i, i);
}

void AddByPointer(int *iptr)
{
    //note that you copy the pointer itself by value
    //the value of the pointer is the address of a place in memory
    //using the asterix the compiler can navigate to that memory address
    //using the typeinfo the compiler knows what is supposed to be at that place in memory
    printf("%8s: address: 0x%X value: 0x%X\n", "Point A", &iptr, iptr);
    printf("%8s: address: 0x%X value: %i\n", "Point B", iptr, *iptr);
    (*iptr) = (*iptr) + 1;
    printf("%8s: address: 0x%X value: %i\n", "Point C", iptr, *iptr);
}

void AddByReference(int &i)
{
    printf("%8s: address: 0x%X value: %i\n", "Ref A", &i, i);
    i = i + 1;
    printf("%8s: address: 0x%X value: %i\n", "Ref B", &i, i);
}

struct test
{
    int x, y, z;
};

void PrintStrruct(test t)
{
    printf("%8s: value: %i\n", "test x", t.x);
    printf("%8s: value: %i\n", "test y", t.y);
    printf("%8s: value: %i\n", "test z", t.z);
}

void PassingAStruct(test *testptr)
{
    (*testptr).x = 0;
    testptr->y = 0;
    test & ref = *testptr;
    ref.z = 0;
}

int main()
{
    int i = 1;
    printf("%8s: address: 0x%X value: %i\n", "main A", &i, i);
    AddByValue(i);
    printf("%8s: address: 0x%X value: %i\n", "main B", &i, i);
    AddByPointer(&i);
    printf("%8s: address: 0x%X value: %i\n", "main C", &i, i);
    AddByReference(i);
    printf("%8s: address: 0x%X value: %i\n", "main D", &i, i);

    printf("--- structs ---\n");
    test mytest;
    PrintStrruct(mytest);
    PassingAStruct(&mytest);
    PrintStrruct(mytest);
    AddByValue(mytest.x);
    PrintStrruct(mytest);
    AddByPointer(&mytest.y);
    PrintStrruct(mytest);
    AddByReference(mytest.z);
    PrintStrruct(mytest);
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM