指向指针的指针的初始化
来源:优易学  2011-12-11 10:26:44   【优易学:中国教育考试门户网】   资料下载   IT书店
  int **ptr = new int*[N];
  for ( i = 0; i < N; i++)
  ptr[i] = new int[N];
  #include <iostream>
  #include <string>
  using namespace std;
  void print_char(char* array[],int len);//青年人网提示函数原形声明
  void main(void)
  {
  //----段1----
  char *a[]={"abc","cde","fgh"};//字符指针数组
  char* *b=a;//定义一个指向指针的指针,并赋予指针数组首地址所指向的第一个字符串的地址也就是abc\0字符串的首地址
  cout<<*b<<"|"<<*(b+1)<<"|"<<*(b+2)<<endl;
  //----段2----
  char* test[]={"abcgdf","cde","fgh","dsadsgasff"};//注意这里是引号,表示是字符串,以后的地址每加1就是加4位(在32位系统上)
  int num=sizeof(test)/sizeof(char*);//计算字符串个数
  int **t;
  cout<<sizeof(test)<<endl<<sizeof(*a)<<endl<<sizeof(*t)<<endl;
  print_char(test,num);
  cin.get();
  }
  void print_char(char* array[],int len)//当调用的时候传递进来的不是数组,而是字符指针他每加1也就是加上sizeof(char*)的长度
  {
  for(int i=0;i<len;i++)
  {
  cout<<*array++<<endl;
  }
  }
  运行结果:
  abc|cde|fgh
  16
  4
  4
  abcgdf
  cde
  fgh
  dsadsgasff

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训