首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

希望大神帮小弟我解说下这都是什么意思

2013-06-25 
希望大神帮我解说下这都是什么意思?typedef struct cell {int indexstruct cell * next} celltypedef c

希望大神帮我解说下这都是什么意思?
typedef struct cell {
  int index;
  struct cell * next;
} cell;
typedef cell * pcell;

typedef struct {
  int v_sommets;
  int nbsom;
  int nbmaxar;
  int nbar; 
  int ind;  /*number of edges*/
  double * weight; /*edge weight indexed by cell index*/
  int *tete;
  int *queue;
  int cs;
  int rs;
  cell *tasar;    /* tableau des cellules (tas) */
  cell *librear;  /* liste des cellules libres geree en pile lifo */
  pcell *listar;  /* tableau des listes d'aretes indexe par les sommets */
} graph; 大神
[解决办法]
typedef struct cell {
  int index;
  struct cell * next;
} cell;


typedef  给某类型给一个别名,不占用内存空间。
如typedef int U_32;
当我们定义一个变量时就可以有以下两种方式,两种方式是等价的
int a;与U_32 a;

那么typedef struct cell {int index;struct cell * next;} cell;
也就不难理解了给“struct cell {int index;struct cell * next;} "这种类型的结构体类型一个别名叫”cell“
等同于以下两个
 1:
  struct cell {
  int index;
  struct cell * next;
};
struct cell fc;
2:
cell fc;
你可以发现第二种写的代码少一些,这只是一个优点,还有其它的,自己去百度吧.
[解决办法]
typedef cell * pcell;
这个就是给 cell *起个别名叫pcell
最终你pcell p;的时候
其实是 struct cell * p;

[解决办法]

引用:
typedef struct cell {
  int index;
  struct cell * next;
} cell;


typedef  给某类型给一个别名,不占用内存空间。
如typedef int U_32;
当我们定义一个变量时就可以有以下两种方式,两种方式是等价的
int a;与U_32 a;

那么typedef struct cell {int index;struct cell * next;} cell;
也就不难理解了给“struct cell {int index;struct cell * next;} "这种类型的结构体类型一个别名叫”cell“
等同于以下两个
 1:
  struct cell {
  int index;
  struct cell * next;
};
struct cell fc;
2:
cell fc;
你可以发现第二种写的代码少一些,这只是一个优点,还有其它的,自己去百度吧.

除了这个有点,另外就是让你的代码更具有可读性,你可以通过这种取别名的方式,给你们的类型取个更具可读性的名字,这一点在大中型软件项目中,具有很大的优点。

[解决办法]

//为了测试typedef关键字
class Stu
{
public:
int age;
public:
Stu(int _age):age(_age){}



};

/*
namespace TesttypedefSp
{
//作用域遮蔽原则,遮蔽比当前作用域(namespace)更大的文件作用域中同名的标识符
void Testtypedef()
{
typedef Stu* pStu;
pStu pstu = new Stu(21);
pstu->age = 11;

typedef pStu const cpStu;
cpStu cp = new Stu(22);
cp->age = 12;
cout<<cp->age<<endl;
delete cp;
//cp = new Stu(22);  //cpStu代表的是指向Stu类对象的常指针,是不可修改的左值


typedef const pStu cpStu;
cpStu cp2 = new Stu(33);
cp2->age = 13;
cout<<cp2->age<<endl;
delete cp2;
//cp2 = new Stu(33);  //这里和上面情况一致,不可修改的左值
//typedef在进行类型重定义的时候不是采用define宏那样一味的整体替换
//在这里pStu相当于一种“新的类型”,const关键字与pStu之间的顺序不会影响pStu变量是一个常量的性质
//就好像是const int 和int const是一个性质一样,这里的pStu相当于是int
}
}
*/



//为了和typedef做出对比,下面特意测试一下define宏定义方式下的类型重命名(一股脑的整体代换)
namespace MyDefine
{
#define pStu Stu*
#define cpStu const pStu
#define pcStu pStu const
}


void Testdefine()
{
using namespace MyDefine;
cpStu cp = new Stu(11);
//cp->age = 11;  //不可修改的左值,意指指向常量Stu对象的指针
cout<<cp->age<<endl;
delete cp;
cp = new Stu(111);  //可以重新指向

pcStu pc2 = new Stu(11);
pc2->age = 11;
delete pc2;
//pc2 = new Stu(11);   //错误
}
//以此可以对比出typedef 与 define 两者在对于类型重命名法则上的一些差异性
//结论:typedef不是简单的一味的代替,而是被当做是一种“纯正的”类型看待


找找看,有没有你需要的点

热点排行