以下代碼定義了一個(gè)結(jié)構(gòu)的指針:
Dog *pDog = NULL;
這個(gè)聲明一個(gè)指針pDog,它可以存儲Dog類型結(jié)構(gòu)的地址。
不要忘記Dog的typedef是省略struct關(guān)鍵字的必要條件。
沒有typedef,你必須將語句寫成:
struct Dog *pDog = NULL;
您現(xiàn)在可以將pDog設(shè)置為具有結(jié)構(gòu)地址的值:
Dog aDog = { 3, 11, "name", "C", "C++"}; pDog = &aDog;
這里pDog指向aDog結(jié)構(gòu)。
指針可以將前一個(gè)示例中的Dogs數(shù)組中的元素的地址存儲起來:
pDog = &my_Dogs[1];
現(xiàn)在pDog指向結(jié)構(gòu)my_Dogs [1]。
您可以通過指針來引用此結(jié)構(gòu)的元素。
printf("The name is %s.\n", (*pDog).name);
你可以這樣寫上面的語句:
printf("The name is %s.\n", pDog->name );
- >
被稱為指向成員運(yùn)算符的指針。
Dog *pDogs[3];
此語句聲明了一個(gè)數(shù)組,指向Dog類型的結(jié)構(gòu)的3個(gè)指針。
這個(gè)語句只分配了指針的內(nèi)存。
您仍然必須分配存儲所需的每個(gè)結(jié)構(gòu)的實(shí)際成員所需的內(nèi)存。
#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;
}
上面的代碼生成以下結(jié)果。
您可以定義一個(gè)設(shè)計(jì)用于保存日期的結(jié)構(gòu)類型。
您可以使用此語句指定具有標(biāo)記名稱Date的合適結(jié)構(gòu):
struct Date { int day; int month; int year; };
你也可以包括一個(gè)typedef for Date和Dog:
typedef struct Dog Dog; // Define Dog as a type name typedef struct Date Date; // Define Date as a type name
現(xiàn)在您可以定義Dog結(jié)構(gòu),包括生日期變量,如下所示:
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;
任何指針都可以是結(jié)構(gòu)的一個(gè)成員。
指向相同類型結(jié)構(gòu)的指針結(jié)構(gòu)成員也被允許。
#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;
}
上面的代碼生成以下結(jié)果。
要傳遞一個(gè)結(jié)構(gòu)數(shù)組,只需向函數(shù)原型提供一個(gè)指向結(jié)構(gòu)的指針,如下一個(gè)修改的程序所示。
#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;
}
更多建議: