下面的代碼是打印protoent結構。打印初始化結構
除非我做getprotoent()
我無法打印結構變量雖然我對其進行初始化。
如果我不getprotoent().
有誰知道這它結束細分動搖?
Samle.c
#include <stdio.h>
#include <string.h>
#include <netdb.h>
int
main(){
struct protoent *proto;
memset(&proto,0,sizeof(proto));
proto = getprotoent();
printf("proto.p_name = %s\n",(*proto).p_name);
printf("proto.p_aliases = %s\n",*(*proto).p_aliases);
printf("proto.p_proto = %d\n",(*proto).p_proto);
}
因爲沒有getprotoent,你沒有分配任何內存給結構指針。 – Ishmeet
'memset'只是'proto = NULL',所以這是浪費時間。 – Devolus
'* proto'只是一個指針,你永遠不會「初始化」它,當然你會得到段錯誤試圖解引用它。你需要malloc內存,最有可能在getprotoent()中完成。 – moeCake