簡體   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