简体   繁体   English

Output 通过 function 以指针作为参数的结构值

[英]Output of values of a struct via a function with a pointer as a parameter

I have two structures.我有两个结构。 A pointer is assigned to one.一个指针被分配给一个。 Now I would like to output data previously entered via scanf via a function (outputAddress) with a pointer as a parameter.现在我想通过 function (outputAddress) 将先前通过 scanf 输入的 output 数据与指针作为参数。 It works with the variables via the pointer.它通过指针处理变量。 But how do I do that with the values from the other structure?但是我如何使用来自其他结构的值来做到这一点? How can I output this in the function?我怎么能在 function 这个 output 这个?

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

struct structPerson
{
char name[30];
char forename[50];
int age;
};

struct structAddress
{
int zip;
char location[35];
char street[40];
int hNumber;
struct structPerson *ptrPerson;
};

void outputAddress(struct structPerson *ptrPerson)
{
printf("\n\nOutput Address: \n");
printf("ptrPerson->name: %s", ptrPerson->name);
printf("\nptrPerson->forename: %s", ptrPerson->forename);
return;
}

int main()
{
struct structPerson person1;
struct structAddress address1;
address1.ptrPerson = &person1;

printf("Location: ");
scanf("%s", &address1.location);
printf("Zip: ");
scanf("%d", &address1.zip);

printf("\nName: ");
scanf("%s", &address1.ptrPerson->name);
printf("Forename: ");
scanf("%s", &address1.ptrPerson->forename);

printf("\nOutput: %d %s %s\n", address1.zip, address1.location, address1.ptrPerson->name);
// strcpy( address1.location, "");
//  printf("structAddress1: %d %s\n", address1.zip, address1.location);


outputAddress(&person1);
return 0;

} }

Your datamodel is probably broken anyway.无论如何,您的数据模型可能已损坏。 The address struct has a pointer to a person, but it often makes more sense to have it the other way around. address 结构体有一个指向人的指针,但反过来使用它通常更有意义。

In your data model structAddress is associated with a person via the personPtr field.在您的数据中 model structAddress通过personPtr字段与人员相关联。 As a result, Address is a main data struct.因此, Address是一个主要的数据结构。 If I understand your intention correctly, you want to print info about the person and then his/her address.如果我正确理解您的意图,您想打印有关此人的信息,然后是他/她的地址。

For this you need to do a couple of changes.为此,您需要进行一些更改。 Firstly, you should pass the Address struct to the print function, because it has all information available, including the pointer to the person.首先,您应该将 Address 结构体传递给 print function,因为它包含所有可用信息,包括指向此人的指针。 Secondly, you should access your person information using the pointer: ptrAddress->ptrPerson-><field> .其次,您应该使用指针访问您的个人信息: ptrAddress->ptrPerson-><field> Here is an example.这是一个例子。

void outputAddress(struct structAddress *ptrAddress)
{
printf("\n\nOutput Address: \n");
// use ptrAddress->ptrPreson to accesss person information
printf("ptrPerson->name: %s", ptrAddress->ptrPerson->name);
printf("\nptrPerson->forename: %s", ptrAddress->ptrPerson->forename);

// use ptrAddress-> to access address fields.
printf("\nptrAddress->zip: %d", ptrAddress->zip);
...
return;
}

int main() {
   ...
   outputAddress(&address1); // << use address1 here.
   ...
}

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

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