将struct成员传递给C中的函数

时间:2016-03-09 12:55:44

标签: c function struct

我试图使用结构中的一个成员通过计算函数()传递它。此函数将为我的变量计算新值。你能否告诉我要将变量传递给我的主函数我该做些什么。我还要保留我的三个功能。谢谢

#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;
}

我将我的变量x存储到i中,以便使用此函数加5:

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

4 个答案:

答案 0 :(得分:2)

您的函数可以指向int

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

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

void struct_values()
{
    //as before

    calculations_using_struct_values(&variable.x);
}

请参阅:Passing by reference in 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);
}

答案 1 :(得分:1)

你可以让函数返回值而不是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;
}

答案 2 :(得分:0)

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

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

...
    calculations_using_struct_values(&variable.x);

答案 3 :(得分:0)

这是一组打印调用不同后果的调用。 注意地址和值在将它们传递给函数之后以及函数返回时的行为方式。根据您的编译器,可能不支持引用,因为它们不是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;
}