如何在不使用指针的情况下将结构传递给函数?

问题描述 投票:0回答:1

考虑以下程序:

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

struct Person {
        char *name;
        int age;
        int height;
        int weight;
};

// new
struct Person* Person_create(char *name, int age, int height, int weight)
{
        struct Person *who = malloc(sizeof(struct Person));
        assert(who != NULL);

        who->name = strdup(name);
        who->age = age;
        who->height = height;
        who->weight = weight;

        return who;
}

// Delete
void Person_destroy(struct Person* who)
{
        assert(who != NULL);

        free(who->name);
        free(who);
}

// Print
void Person_print(struct Person* who)
{       
        printf("Name : %s\n",who->name);
        printf("\tAge: %d\n",who->age);
        printf("\theight: %d\n",who->height);
        printf("\tweight: %d\n",who->weight);
}

int main(int argc, char* argv[])
{
        // make two people structures
        struct Person* joe = Person_create(
                        "Joe Alex", 32, 64, 140);

        struct Person* frank = Person_create(
                        "Frank Blank", 20, 72, 180);

        // print them out and where they are in memory
        printf("Joe is at memory location %p:\n",joe);
        Person_print(joe);

        printf("Frank is at memory location %p:\n",frank);
        Person_print(frank);

        // make everyone age 20 years and print them again
        joe->age += 20;
        joe->height -=2;
        joe->weight += 40;
        Person_print(joe);

        frank->age += 20;
        frank->weight += 20;
        Person_print(frank);

        // destory them both so we clean up
        Person_destroy(joe);
        Person_destroy(frank);

        // try to transfer NULL to Person_destroy
        // Person_destroy(NULL);

        return 0;
}

上面的程序是正确的,但是我想知道如何在不使用指针和malloc函数的情况下完成这个程序:

1。我想在堆栈上创建结构。

2。我不想使用指针将结构传递给其他函数。

第一个问题,在栈上创建结构,我觉得可以用alloca函数。

对于第二个问题,我想知道是否可以使用引用传递来完成这个事情。

pointers malloc structure
1个回答
0
投票

要在堆栈上创建变量,您可以使用

struct Person joe;

现在可以使用访问结构变量。 现在您可以简单地传递结构变量而无需使用指针。

修改后的代码如下:

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

 struct Person {
    char *name;
    int age;
    int height;
    int weight;
};

// new
struct Person Person_create(char *name, int age, int height, int weight)
{
    struct Person who;

    who.name = strdup(name);
    who.age = age;
    who.height = height;
    who.weight = weight;

    return who;
}

// Delete -- no longer needed as variable is defined on stack
/*void Person_destroy(struct Person who)
{
    assert(who != NULL);

    free(who->name);
    free(who);
}*/

// Print
void Person_print(struct Person who)
{       
    printf("Name : %s\n",who.name);
    printf("\tAge: %d\n",who.age);
    printf("\theight: %d\n",who.height);
    printf("\tweight: %d\n",who.weight);
}

int main(int argc, char* argv[])
{
    // make two people structures
    struct Person joe = Person_create(
                    "Joe Alex", 32, 64, 140);

    struct Person frank = Person_create(
                    "Frank Blank", 20, 72, 180);

    // print them out and where they are in memory
    printf("Joe is at memory location %p:\n",&joe);
    Person_print(joe);

    printf("Frank is at memory location %p:\n",&frank);
    Person_print(frank);

    // make everyone age 20 years and print them again
    joe.age += 20;
    joe.height -=2;
    joe.weight += 40;
    Person_print(joe);

    frank.age += 20;
    frank.weight += 20;
    Person_print(frank);

    // destory them both so we clean up
      //Person_destroy(joe);
       //Person_destroy(frank);

    // try to transfer NULL to Person_destroy
    // Person_destroy(NULL);

    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.