简体   繁体   English

如何在C中使用结构指针作为参数调用void函数?

[英]How to call a void function with a struct pointer as parameter in C?

All I really need is just examples on how to return these kind of functions to it's caller.我真正需要的只是关于如何将这些类型的函数返回给调用者的示例。 Doesn't even have to relate to my code except for the type of function and parameter.除了函数和参数的类型之外,甚至不必与我的代码相关。

I wrote this function below that is supposed to read the name and age of three user defined people.我在下面写了这个函数,它应该读取三个用户定义的人的姓名和年龄。

void readData(Person *p) 
{
    char name1[20], name2[20], name3[20];
    int age1, age2, age3;
    printf("Enter person 1: \n");
    scanf("%s %d", &name1, &age1);
    printf("Enter person 2: \n");
    scanf("%s %d", &name2, &age2);
    printf("Enter person 3: \n");
    scanf("%s %d", &name3, &age3);
    struct person1 {char name1[20]; int age1};
    struct person2 {char name2[20]; int age2};
    struct person3 {char name3[20]; int age3};
    struct Person {struct person1; struct person2; struct person3};
    struct Person man[3] = {{name1, age1}, {name2, age2}, {name3, age3}};
    p = &man;
}

I would like to re-use the variables of the above function so that I can use it in the function below.我想重新使用上面函数的变量,以便我可以在下面的函数中使用它。 How am I supposed to do that as I cannot 're-declare' the variables because they are user-defined?我该怎么做,因为我不能“重新声明”变量,因为它们是用户定义的? Do I call by reference or value and if so, how do I do that?我是按引用还是按值调用,如果是,我该怎么做? Do I need to return anything?我需要return吗? I can't find examples for this kind of functions, any help is really appreciated.我找不到此类功能的示例,非常感谢您的帮助。

Person findMiddleAge(Person *p) 
{
    int middle;
    if ((age1 < age2 && age2 < age3) || (age3 < age2 && age2 < age1)) {
        middle = age2;
        struct Person man[1] = {name2, age2};
        }
    else if ((age2 < age1 && age1 < age3) || (age3 < age1 && age1 < age2)) {
            middle = age1; 
            struct Person man[1] = {name1, age1};
            }
    else if ((age1 < age3 && age3 < age2) || (age2 < age3 && age3 < age1)) {
        middle = age3;
        struct Person man[1] = {name3, age3};
        }
    printf("%d \n", middle);
}

The chunk of code below shows my whole code just in case it matters.下面的代码块显示了我的整个代码,以防万一。 Nothing can be amended except for the lines of code within the readData(Person *p) and Person findMiddleAge(Person *p) functions.除了readData(Person *p)Person findMiddleAge(Person *p)函数中的代码行之外,什么都不能修改。

typedef struct {
   char name[20]; 
   int age;
} Person; 

void readData(Person *p);
Person findMiddleAge(Person *p);
int main() 
{
   Person man[3], middle;   

   readData(man);
   middle = findMiddleAge(man);
   printf("findMiddleAge(): %s %d\n", middle.name, middle.age);
   return 0;
}

void readData(Person *p) 
{
    char name1[20], name2[20], name3[20];
    int age1, age2, age3;
    printf("Enter person 1: \n");
    scanf("%s %d", &name1, &age1);
    printf("Enter person 2: \n");
    scanf("%s %d", &name2, &age2);
    printf("Enter person 3: \n");
    scanf("%s %d", &name3, &age3);
    struct person1 {char name1[20]; int age1};
    struct person2 {char name2[20]; int age2};
    struct person3 {char name3[20]; int age3};
    struct Person {struct person1; struct person2; struct person3};
    struct Person man[3] = {{name1, age1}, {name2, age2}, {name3, age3}};
    p = &man;
}

Person findMiddleAge(Person *p) 
{
    int middle;
    if ((age1 < age2 && age2 < age3) || (age3 < age2 && age2 < age1)) {
        middle = age2;
        struct Person man[1] = {name2, age2};
        }
    else if ((age2 < age1 && age1 < age3) || (age3 < age1 && age1 < age2)) {
            middle = age1; 
            struct Person man[1] = {name1, age1};
            }
    else if ((age1 < age3 && age3 < age2) || (age2 < age3 && age3 < age1)) {
        middle = age3;
        struct Person man[1] = {name3, age3};
        }
    printf("%d \n", middle);
}

And yes, this is homework but I really tried my best (I tried over 6 hours to get the desired results but I still can't, C is really hard for me to understand).是的,这是家庭作业,但我真的尽力了(我尝试了 6 多个小时以获得想要的结果,但我仍然不能,C 对我来说真的很难理解)。 I am supposed to find the person whose age is the middle of the three people, and return the name and age of that person to the caller.我应该找到年龄在三个人中间的那个人,并将那个人的姓名和年龄返回给来电者。 Am I going about everything incorrectly?我是否对所有事情都做错了? I actually just want to know how to return this kind of functions to the caller.我实际上只是想知道如何将这种函数返回给调用者。 Do I need to put any return statements?我需要放置任何return语句吗? I can't find examples on it (if anyone has any examples, I think I can figure the code out myself with those examples)?我找不到它的例子(如果有人有任何例子,我想我可以用这些例子自己找出代码)?

Thank you for any help in advance, I really appreciate it.提前感谢您的任何帮助,我真的很感激。

In readData and findMiddleAge p points to man[3] in main .readDatafindMiddleAge p指向main man[3]
In readData the array may be filled directly.readData ,可以直接填充数组。 It is a good habit to check the return of scanf .检查scanf的返回是一个好习惯。 If name and age are scanned, it will return 2. A return of 1, 0 or EOF indicates a problem.如果扫描姓名和年龄,则返回 2。返回 1、0 或 EOF 表示有问题。
In findMiddleAge determine which is the middle value of p[0].age , p[1].age or p[2].age and return the correct one.findMiddleAge确定哪个是p[0].agep[1].agep[2].age的中间值并返回正确的值。

#include <stdio.h>

typedef struct {
    char name[20];
    int age;
} Person;

void readData(Person *p);
Person findMiddleAge(Person *p);

int main()
{
    Person man[3] = { { "", 0}}, middle = { "", 0};

    readData(man);
    middle = findMiddleAge(man);
    printf("findMiddleAge(): %s %d\n", middle.name, middle.age);
    return 0;
}

void readData(Person *p)
{
    printf("Enter person 1: \n");
    scanf("%19s %d", p[0].name, &p[0].age);
    printf("Enter person 2: \n");
    scanf("%19s %d", p[1].name, &p[1].age);
    printf("Enter person 3: \n");
    scanf("%19s %d", p[2].name, &p[2].age);
}

Person findMiddleAge(Person *p)
{
    //figure out which is the middle value of
    //p[0].age
    //p[1].age
    //p[2].age
    return p[1];//or p[0] or p[2]
}

In one line you have two problems:在一行中,您有两个问题:

p = &man
  1. You assign pointer to the local variable (and dereference it after the function return) which is the UB.您将指针分配给作为 UB 的局部变量(并在函数返回后取消引用它)。
  2. You assign local pointer p and this assignment does not make any effect (as p stops to exist when function returns).您分配本地指针p并且此分配没有任何效果(因为p在函数返回时停止存在)。 To modify the pointer itself you need to pass pointer to pointer.要修改指针本身,您需要将指针传递给指针。
void readData(Person **p) 
{
    /* ... */
    *p = newpvalue;
}

I believe there is much more similar problems in your code.我相信您的代码中有更多类似的问题。 I advice to focus on the C basics (scopes, variables, pointers etc.)我建议专注于 C 基础知识(范围、变量、指针等)

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

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