有关指针的指针的问题
问题:
从文件中读取几个字符赋给char *类型变量。
然后,将char *类型变量的地址赋给char **类型变量。
我想输出char **类型变量的内容,但是在运行printf时出错。
编译器:gcc
出错信息:Segmentation fault
请各位大虾不吝指教,严重感谢。
测试程序如下:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE * fd;
char * data1,*data2;
char * t1,*t2;
char **pp;
data1=(char *)malloc(sizeof(char)*10);
data2=(char *)malloc(sizeof(char)*10);
fd=fopen("./fu","rb");
if(fd==NULL) return -1 ;
fread(data1,sizeof(char),5,fd);
fread(data2,sizeof(char),5,fd);
strcpy(t1,data1);
strcpy(t2,data2);
// printf("tt address is %p\n",t1); //1
// printf("tt address is %p\n",t2); //2
pp=(char **)malloc(sizeof(char *)*2);
*pp++=t1;
*pp=t2;
pp--;
printf("value of pp is %x\n",*pp++); //1
printf("value of pp is %x\n",*pp); //2
pp--;
// printf("pp 1 is %s\n",**pp++); //出错
// printf("pp 2 is %s\n",**pp); //出错
free(data1);
free(data2);
free(pp);
fclose(fd);
return 0;
}
要运行的话你需要自己写一个文件,填入多于10个字符,我上面没有文件操作的
容错处理。