学习C – C结构体指针
以下代码定义了一个结构的指针:
Dog *pDog = NULL;
这个声明一个指针pDog,它可以存储Dog类型结构的地址。
不要忘记Dog的typedef是省略struct关键字的必要条件。
没有typedef,你必须将语句写成:
struct Dog *pDog = NULL;
您现在可以将pDog设置为具有结构地址的值:
Dog aDog = { 3, 11, "name", "C", "C++"}; pDog = &aDog;
这里pDog指向aDog结构。
指针可以将前一个示例中的Dogs数组中的元素的地址存储起来:
pDog = &my_Dogs[1];
现在pDog指向结构my_Dogs [1]。
您可以通过指针来引用此结构的元素。
printf("The name is %s.\n", (*pDog).name);
你可以这样写上面的语句:
printf("The name is %s.\n", pDog->name );
- >
被称为指向成员运算符的指针。
动态内存分配结构
Dog *pDogs[3];
此语句声明了一个数组,指向Dog类型的结构的3个指针。
这个语句只分配了指针的内存。
您仍然必须分配存储所需的每个结构的实际成员所需的内存。
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h> // For malloc()
typedef struct Dog Dog; // Define Dog as a type name
struct Dog // Structure type definition
{
int age;
int height;
char name[20];
char father[20];
char mother[20];
};
int main(void) {
Dog *pDogs[3]; // Array of pointers to structure
int hcount = 0;
char test = "\0"; // Test value for ending input
for(hcount = 0 ; hcount < sizeof(pDogs)/sizeof(Dog*) ; ++hcount) {
printf("Do you want to enter details of a%s Dog (Y or N)? ",
hcount?"nother" : "" );
scanf(" %c", &test, sizeof(test));
if(tolower(test) == "n")
break;
// allocate memory to hold a Dog structure
pDogs[hcount] = (Dog*) malloc(sizeof(Dog));
printf("Enter the name of the Dog: " );
scanf("%s", pDogs[hcount]->name, sizeof(pDogs[hcount]->name));
printf("How old is %s? ", pDogs[hcount]->name );
scanf("%d", &pDogs[hcount]->age);
printf("How high is %s ( in hands )? ", pDogs[hcount]->name);
scanf("%d", &pDogs[hcount]->height);
printf("Who is %s"s father? ", pDogs[hcount]->name);
scanf("%s", pDogs[hcount]->father, sizeof(pDogs[hcount]->father));
printf("Who is %s"s mother? ", pDogs[hcount]->name);
scanf("%s", pDogs[hcount]->mother, sizeof(pDogs[hcount]->mother));
}
// Now tell them what we know.
printf("\n");
for (int i = 0 ; i < hcount ; ++i) {
printf("%s is %d years old, %d hands high,", pDogs[i]->name, pDogs[i]->age, pDogs[i]->height);
printf(" and has %s and %s as parents.\n", pDogs[i]->father, pDogs[i]->mother);
free(pDogs[i]);
}
return 0;
}
上面的代码生成以下结果。
结构作为结构的成员
您可以定义一个设计用于保存日期的结构类型。
您可以使用此语句指定具有标记名称Date的合适结构:
struct Date { int day; int month; int year; };
你也可以包括一个typedef for Date和Dog:
typedef struct Dog Dog; // Define Dog as a type name typedef struct Date Date; // Define Date as a type name
现在您可以定义Dog结构,包括生日期变量,如下所示:
struct Dog { Date dob; int height; char name[20]; char father[20]; char mother[20]; }; Dog aDog; aDog.height = 14; aDog.dob.day = 5; aDog.dob.month = 12; aDog.dob.year = 1962;
作为结构成员的结构指针
任何指针都可以是结构的一个成员。
指向相同类型结构的指针结构成员也被允许。
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
typedef struct Dog Dog; // Define Dog as a type name
struct Dog // Structure type definition
{
int age;
int height;
char name[20];
char father[20];
char mother[20];
Dog *next; // Pointer to next Dog structure
};
int main(void) {
Dog *first = NULL; // Pointer to first Dog
Dog *current = NULL; // Pointer to current Dog
Dog *previous = NULL; // Pointer to previous Dog
char test = "\0"; // Test value for ending input
for( ; ; ) {
printf("Do you want to enter details of a%s Dog (Y or N)? ", first != NULL?"nother" : "" );
scanf(" %c", &test, sizeof(test));
if(tolower(test) == "n")
break;
// Allocate memory for a Dog structure
current = (Dog*) malloc(sizeof(Dog));
if(first == NULL) // If there"s no 1st Dog...
first = current; // ...set this as 1st Dog
if(previous != NULL) // If there was a previous...
previous->next = current; // ...set its next to this one
printf("Enter the name of the Dog: ");
scanf("%s", current->name, sizeof(current->name));
printf("How old is %s? ", current->name);
scanf("%d", ¤t->age);
printf("How high is %s ( in hands )? ", current -> name );
scanf("%d", ¤t->height);
printf("Who is %s"s father? ", current->name);
scanf("%s", current->father,sizeof(current->father));
printf("Who is %s"s mother? ", current->name);
scanf("%s", current->mother, sizeof(current->mother));
current->next = NULL; // In case it"s the last...
previous = current; // ...save its address
}
// Now tell them what we know...
printf("\n");
current = first; // Start at the beginning
while (current != NULL) // As long as we have a valid pointer
{ // Output the data
printf("%s is %d years old, %d hands high,", current->name, current->age, current->height);
printf(" and has %s and %s as parents.\n", current->father, current->mother);
previous = current; // Save the pointer so we can free memory
current = current->next; // Get the pointer to the next
free(previous); // Free memory for the old one
previous = NULL;
}
first = NULL;
return 0;
}
上面的代码生成以下结果。
传递结构数组
要传递一个结构数组,只需向函数原型提供一个指向结构的指针,如下一个修改的程序所示。
#include <stdio.h>
#include <string.h>
typedef struct employee {
int id;
char name[10];
float salary;
} e;
void processEmp( e * ); //supply prototype with pointer of structure type
main()
{
e emp1[3] = {0,0,0};
int x;
processEmp( emp1 ); //pass array name, which is a pointer
for ( x = 0; x < 3; x++ ) {
printf("\nID: %d\n", emp1[x].id);
printf("Name: %s\n", emp1[x].name);
printf("Salary: $%.2f\n\n", emp1[x].salary);
}
}
void processEmp( e * emp ) //function receives a pointer
{
emp[0].id = 123;
strcpy(emp[0].name, "A");
emp[0].salary = 65.00;
emp[1].id = 234;
strcpy(emp[1].name, "B");
emp[1].salary = 28.00;
emp[2].id = 456;
strcpy(emp[2].name, "C");
emp[2].salary = 48.00;
}