简体   繁体   English

将struct成员传递给C函数

[英]Passing a struct member into functions in C

I'm trying to use one of the members in the struct to pass it through the calculation function(). 我正在尝试使用结构中的成员之一通过计算function()传递它。 This function will calculate a new value for my variable. 此函数将为我的变量计算一个新值。 Can you please show me what I have to do to pass my variable into my main function. 您能告诉我如何将变量传递到主函数中吗? I also what to keep my three functions. 我还保留了我的三个职能。 Thanks 谢谢

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

My prototype: 我的原型:

void calculations_using_struct_values(int i);

My struct: 我的结构:

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

My main: 我的主要:

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

Initializing the values for my variables: 初始化变量的值:

void struct_values()
{
    test variable;

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

    calculations_using_struct_values(variable.x);
    return;
}

I stored my variable.x into i for this function to plus it by 5: 我将我的variable.x存储到i中,以便将该函数加5:

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

Your function can take a pointer to an int . 您的函数可以使用指向int的指针。

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

And pass the address of your struct member to the function ( & ): 并将您的struct成员的地址传递给函数( & ):

void struct_values()
{
    //as before

    calculations_using_struct_values(&variable.x);
}

See: Passing by reference in C 请参阅: 在C中通过引用传递

Or you could if needed pass the whole struct: 或者,如果需要,您可以传递整个结构:

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

And pass the address of your struct to the function ( & ): 并将您的结构的地址传递给函数( & ):

void struct_values()
{
    //as before

    calculations_using_struct_values(&variable);
}

You could have the functions return values rather than void. 您可以让函数返回值而不是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;
}

If you want to modify value in place, you need to use pointers: 如果要在适当位置修改值,则需要使用指针:

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

...
    calculations_using_struct_values(&variable.x);

Here is a collection of calls that prints the different consequences of calling. 这是一组呼叫,显示了呼叫的不同结果。 Note how address and value behave after passing them to functions and when the functions return. 请注意地址和值在传递给函数后以及函数返回时的行为。 Depending on your compiler references may not be supported since they are not ansi-c. 取决于您的编译器,可能不支持引用,因为它们不是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