<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 結構和指針 > 原文: [https://www.programiz.com/c-programming/c-structures-pointers](https://www.programiz.com/c-programming/c-structures-pointers) #### 在本教程中,您將學習在 C 編程中使用指針訪問結構的成員。 您還將學習動態分配結構類型的內存。 在了解如何將指針與結構一起使用之前,請務必閱讀以下教程: * [C 指針](/c-programming/c-pointers "C Pointers") * [C 結構](/c-programming/c-structures "C structs") * * * ## C 指向結構的指針 這是創建結構指針的方法。 ```c struct name { member1; member2; . . }; int main() { struct name *ptr, Harry; } ``` 這里,`ptr`是指向`struct`的指針。 * * * ## 示例:使用指針訪問成員 要使用指針訪問結構的成員,我們使用`->`運算符。 ```c #include <stdio.h> struct person { int age; float weight; }; int main() { struct person *personPtr, person1; personPtr = &person1; printf("Enter age: "); scanf("%d", &personPtr->age); printf("Enter weight: "); scanf("%f", &personPtr->weight); printf("Displaying:\n"); printf("Age: %d\n", personPtr->age); printf("weight: %f", personPtr->weight); return 0; } ``` 在此示例中,使用`personPtr = &person1;`將`person1`的地址存儲在`personPtr`指針中。 現在,您可以使用`personPtr`指針訪問`person1`的成員。 順便說說, * `personPtr->age`等同于`(*personPtr).age` * `personPtr->weight`等同于`(*personPtr).weight` * * * ## 結構的動態內存分配 在繼續本節之前,建議您檢查 [C 動態內存分配](https://www.programiz.com/c-programming/c-dynamic-memory-allocation)。 有時,您聲明的結構變量的數量可能不足。 您可能需要在運行時分配內存。 這是您可以在 C 編程中實現此目標的方法。 ### 示例:結構的動態內存分配 ```c #include <stdio.h> #include <stdlib.h> struct person { int age; float weight; char name[30]; }; int main() { struct person *ptr; int i, n; printf("Enter the number of persons: "); scanf("%d", &n); // allocating memory for n numbers of struct person ptr = (struct person*) malloc(n * sizeof(struct person)); for(i = 0; i < n; ++i) { printf("Enter first name and age respectively: "); // To access members of 1st struct person, // ptr->name and ptr->age is used // To access members of 2nd struct person, // (ptr+1)->name and (ptr+1)->age is used scanf("%s %d", (ptr+i)->name, &(ptr+i)->age); } printf("Displaying Information:\n"); for(i = 0; i < n; ++i) printf("Name: %s\tAge: %d\n", (ptr+i)->name, (ptr+i)->age); return 0; } ``` 運行該程序時,輸出為: ```c Enter the number of persons: 2 Enter first name and age respectively: Harry 24 Enter first name and age respectively: Gary 32 Displaying Information: Name: Harry Age: 24 Name: Gary Age: 32 ``` 在上面的示例中,創建了`n`個結構變量,其中用戶輸入了`n`。 為了為`struct person`的`n`個分配內存,我們使用了, ```c ptr = (struct person*) malloc(n * sizeof(struct person)); ``` 然后,我們使用`ptr`指針訪問`person`的元素。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看