結構體指標變數與結構體成員指標變數

來源:互聯網
上載者:User

 

 

C代碼 {
dp.sh.Toolbar.CopyToClipboard(this);return false;
}" href="http://www.javaeye.com/topic/542899#">
  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3. #include <string.h>   
  4.   
  5. struct student{   
  6.   char *name;   
  7.   int score;   
  8.   struct student* next;   
  9. }stu,*stu1;    
  10.   
  11. int main(){    
  12.   stu.name = (char*)malloc(sizeof(char)); /*1.結構體成員指標需要初始化*/  
  13.   strcpy(stu.name,"Jimy");   
  14.   stu.score = 99;   
  15.   
  16.   stu1 = (struct student*)malloc(sizeof(struct student));/*2.結構體指標需要初始化*/  
  17.   stu1->name = (char*)malloc(sizeof(char));/*3.結構體指標的成員指標同樣需要初始化*/  
  18.   stu.next  = stu1;   
  19.   strcpy(stu1->name,"Lucy");   
  20.   stu1->score = 98;   
  21.   stu1->next = NULL;   
  22.   printf("name %s, score %d /n ",stu.name, stu.score);   
  23.   printf("name %s, score %d /n ",stu1->name, stu1->score);   
  24.   free(stu1);   
  25.   return 0;   
  26. }  
#include <stdio.h>#include <stdlib.h>#include <string.h>struct student{  char *name;  int score;  struct student* next;}stu,*stu1; int main(){  stu.name = (char*)malloc(sizeof(char)); /*1.結構體成員指標需要初始化*/  strcpy(stu.name,"Jimy");  stu.score = 99;  stu1 = (struct student*)malloc(sizeof(struct student));/*2.結構體指標需要初始化*/  stu1->name = (char*)malloc(sizeof(char));/*3.結構體指標的成員指標同樣需要初始化*/  stu.next  = stu1;  strcpy(stu1->name,"Lucy");  stu1->score = 98;  stu1->next = NULL;  printf("name %s, score %d /n ",stu.name, stu.score);  printf("name %s, score %d /n ",stu1->name, stu1->score);  free(stu1);  return 0;}

寫測試代碼的過程中我明白了,同事所說的二叉樹遍曆演算法中所用的左子樹和右子樹指標不需要初始化,其實是這樣的,左子樹和右子樹指向的必須是二叉樹節點類型的結構體指標(你填一個長度相同的指標也可以),而該結構體指標是需要初始化的(見注釋2),也就是並沒有通過malloc來分配記憶體,而是將另一個指標的值賦給它

頓時覺得挺無語的,確實,看了很多大學裡的教材,對於二叉樹的遍曆等演算法定義的結構體無非是以下形式

C代碼 {
dp.sh.Toolbar.CopyToClipboard(this);return false;
}" href="http://www.javaeye.com/topic/542899#">
  1. struct node{   
  2.   int data;   
  3.   struct node* lchild, rchild;   
  4. };  
struct node{  int data;  struct node* lchild, rchild;};

使用時都直接的

C代碼 {
dp.sh.Toolbar.CopyToClipboard(this);return false;
}" href="http://www.javaeye.com/topic/542899#">
  1. struct node* root;   
  2.  root = (struct node*)malloc(sizeof(struct node));   
  3.  root->data = 3;   
  4.   
  5.  struct node* nlchild;   
  6.  nlchild = (struct node*)malloc(sizeof(struct node));   
  7.  root->lchild = nlchild;   
  8.  nlchild->data = 2;    
  9.   
  10.  struct node* nrchild;   
  11.  nlrchild = (struct node*)malloc(sizeof(struct node));   
  12.  root->rchild = nrchild;   
  13.  nrchild->data = 4;   
 struct node* root;  root = (struct node*)malloc(sizeof(struct node));  root->data = 3;  struct node* nlchild;  nlchild = (struct node*)malloc(sizeof(struct node));  root->lchild = nlchild;  nlchild->data = 2;   struct node* nrchild;  nlrchild = (struct node*)malloc(sizeof(struct node));  root->rchild = nrchild;  nrchild->data = 4; 

這樣子給人造成一種錯覺好像結構體成員指標是不用初始化的。

可是,只要是指標,要使用它前就必須保證指標變數的值是一個有效值;否則,它指向的記憶體一定是垃圾資料!
C語言的記憶體管理很重要,集魄力和麻煩於一身,看你自己的心態如何了。如果你積極的面對,你正在控制一切;如果你覺得煩躁,你正不得不控制一切。C仍舊是博大精深的語言,信C哥!

/*附加:仍舊是指標*/

C代碼 {
dp.sh.Toolbar.CopyToClipboard(this);return false;
}" href="http://www.javaeye.com/topic/542899#">
  1. stu1 = (struct student*)malloc(sizeof(struct student));/*2.結構體指標需要初始化*/  
  stu1 = (struct student*)malloc(sizeof(struct student));/*2.結構體指標需要初始化*/

這一句可能會有人把sizeof裡邊也填成struct student*
可以理解這樣的行為,因為stu本來就是struct student*,可是這樣子你就沒有為結構體分配足夠的記憶體,使用中會因為記憶體錯誤同樣報錯的。
當然,僅僅為結構體指標分配記憶體還不夠,結構體成員指標仍然需要分配記憶體,如下

C代碼 {
dp.sh.Toolbar.CopyToClipboard(this);return false;
}" href="http://www.javaeye.com/topic/542899#">
  1. stu1->name = (char*)malloc(sizeof(char));  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.