結構體指標的應用理解

2021-06-27 16:59:18 字數 1227 閱讀 1461

在cjson呼叫時遇到的結構體引數賦值時的問題,需要cjson**才行,下面兩種方法都行,對比一下。

也對指標理解更深入:

結構體給函式當形參時,由於主函式裡也沒分配結構體空間,只有結構體的位址,區域性函式裡面只能給區域性結構體位址,給結構體的位址放在結構體位址的空間中,要有存放結構體位址空間的位址,也就是下面例子中的cjson**

#include

#include

#include "cjson.h"

/* parse text to json, then render back to text, and print! */

//1、使用返回值進行,返回json*

/*cjson *doit(char *text)

*///2、使用json**給形參

void doit2(char *text,cjson** root)

int main (int argc, const char * argv) 

,,,,,,]}";

//cjson *root = (cjson*)malloc(sizeof(cjson));

//1、使用返回值進行,返回cjson*

/*cjson *root;

char *out;

root = doit(sensorvalue);

printf("222 root->type = %d\n", root->type);

out=cjson_print(root);

cjson_delete(root);

printf("doit:%s\n",out);

*///2、使用cjson**給形參

cjson *root;

char *out;

doit2(sensorvalue,&root);

printf("222 root->type = %d\n", root->type);

out=cjson_print(root);

cjson_delete(root);

printf("doit2:%s\n",out);

//char * out= null;printf("aaabbb\n");

//out = cjson_print(root);printf("aaabbb\n");

//printf("aaaaaaaaaaa\n%s\n",out);

return 0;

}

結構體理解與應用

知道為什麼要結構化資訊 下面用 說明乙個簡單的 int id 4 char name 4 16 char phone 4 16 這種方法表達乙個 資訊,表示的資訊不直觀,最重要的是資料的訪問不方便,比如要按id來查詢乙個人,那麼用上面這種方式儲存資訊,來顯示查詢結果就要對下面函式的每個傳入引數都輸出...

理解c語言結構體指標

void changei int i void changei int p void changep int p int main 如上所示 僅用於本示例,不糾檔案結構,函式申明什麼的。從主函式從上往下輸出 前四個輸出 i 21,j i 是常見的賦值語句,即變數j的值現在為21。p1 i,p2 i ...

函式 指標 結構體的綜合應用

接下來我將以一道題目來總結我這乙個星期學習的內容 剛拿到題目時心裡慌兮兮的,這麼恐怖 100分鐘15題,而且要有6種排序,8個輸出 還能不能好好玩耍了.t.t 看過一次題目後 我好像明白點啥了 多看幾次題目後,發現其實老師已經把解題思路 命名問題都給解決了 特別是命名問題 只要按著題目上的順序寫,思...