使用某类型是否用声明的思考
2017-04-06 13:17
246 查看
原因:
由于看linux协议栈内核源码使遇到了如下情况:
neigh_parms中用的struct neighbour类型在使用前声明了下,而struct neigh_table类型却没有进行任何声明,这样难道不会出错吗?
验证:
验证一:
运行结果为:
define1.c:7:17: 错误:字段‘st’的类型不完全
struct str2 st;
^
验证二:
运行结果:
$ gcc define.c
$ ./a.out
2-----1
结论:
用的是指针类型无妨,若是实体类型将会报错
由于看linux协议栈内核源码使遇到了如下情况:
#ifndef _NET_NEIGHBOUR_H #define _NET_NEIGHBOUR_H #include <linux/neighbour.h> /* * Generic neighbour manipulation * * Authors: * Pedro Roque <roque@di.fc.ul.pt> * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> * * Changes: * * Harald Welte: <laforge@gnumonks.org> * - Add neighbour cache statistics like rtstat */ #include <asm/atomic.h> #include <linux/netdevice.h> #include <linux/skbuff.h> #include <linux/rcupdate.h> #include <linux/seq_file.h> #include <linux/err.h> #include <linux/sysctl.h> #include <linux/workqueue.h> #include <net/rtnetlink.h> /* * NUD stands for "neighbor unreachability detection" */ #define NUD_IN_TIMER (NUD_INCOMPLETE|NUD_REACHABLE|NUD_DELAY|NUD_PROBE) #define NUD_VALID (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE|NUD_PROBE|NUD_STALE|NUD_DELAY) #define NUD_CONNECTED (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE) struct neighbour; struct neigh_parms { #ifdef CONFIG_NET_NS struct net *net; #endif struct net_device *dev; struct neigh_parms *next; int (*neigh_setup)(struct neighbour *); void (*neigh_cleanup)(struct neighbour *); struct neigh_table *tbl; void *sysctl_table; int dead; atomic_t refcnt; struct rcu_head rcu_head; int base_reachable_time; int retrans_time; int gc_staletime; int reachable_time; int delay_probe_time; int queue_len; int ucast_probes; int app_probes; int mcast_probes; int anycast_delay; int proxy_delay; int proxy_qlen; int locktime; };
neigh_parms中用的struct neighbour类型在使用前声明了下,而struct neigh_table类型却没有进行任何声明,这样难道不会出错吗?
验证:
验证一:
#include <stdio.h> struct str2; struct str1 { struct str2 st; int i1; }; struct str2 { int i2; }; int main() { struct str1 stest1; stest1.st.i2 = 2; stest1.i1 = 1; printf("%d----%d\n", stest1.st.i2, stest1.i1); return 0; }
运行结果为:
define1.c:7:17: 错误:字段‘st’的类型不完全
struct str2 st;
^
验证二:
#include <stdio.h> //typedef struct str2* ST; //struct str2; struct str1 { // ST pst; struct str2 * pst; int i1; }; struct str2 { int i2; }; int main() { struct str1 stest1; struct str2 stest2; stest1.pst = &stest2; stest1.pst->i2 = 2; stest1.i1 = 1; printf("%d-----%d\n", stest1.pst->i2, stest1.i1); return 0; }
运行结果:
$ gcc define.c
$ ./a.out
2-----1
结论:
用的是指针类型无妨,若是实体类型将会报错
相关文章推荐
- 使用泛型委声明来动态指派委托的返回类型
- 使用java判断字符串是否日日期类型
- GCC 关于声明,定义,调用(使用),类型定义
- JPA学习笔记---JPA实体Bean的建立---链接上一个博文:对实体Bean中属性进行操作:保存日期类型,设置字段的长度,名字,是否为空,可以声明枚举字段;可以存放二进制数据,可以存放
- 声明一个实例,是否使用new的区别
- 使用临时表与声明一个表数据类型的变量有什么区别
- 在静态方法中使用变量 (需要将变量在.m文件中声明未静态static 类型)
- JS 判断浏览器类型(是否使用IE,Firefox,Opera浏览器)(转)
- 函数返回值是否使用引用类型的问题:理解引用、返回值
- 例题3-8 使用whle循环和折半法查找一个整数是否在一个排序的int类型数组中
- 在javaScript中使用正则表达试判断输入是否是float类型
- WCF:使用启用声明的 WCF Web 服务作为 SharePoint 2010 外部内容类型(第4 部分,共 4 部分)
- 交互式让用户输入一个文件路径,判断文件是否存在,若存在判断文件类型,若文件是普通文件,不能使用wc。
- C语言多文件编译时,编译器不检测其声明的变量类型与定义时的类型是否匹配
- windows phone 7获得当前使用网络类型(设备是否通过zune与PC相连)
- 关于.NET 使用堆栈来维护值类型变量的思考和理解
- IDE使用问题:vc2003中类型无法转到声明和定义,按F12提示未定义的符号(CSDN一个帖子)
- 丶使用is关键字判断对象是否与指定类型兼容
- C/C++编写dll动态链接库使用操作与非基本类型参数声明错误(2)
- 声明变量时是否使用var的不同