关于下标运算符和用指针加偏移量访问的区别

nightkids_008 2012-08-25 02:58:47
RT,部分代码如下所示:

//analysis the shstrtab offset in the file
ioffset = shdr[phead->e_shstrndx].sh_offset;
isize = shdr[phead->e_shstrndx].sh_size;

pshstr = (char*)malloc(sizeof(char)*isize);
//read the shstrtable from file
if( 0 != fseek(p,ioffset,SEEK_SET) )
{
printf("move the offset for shstrtab failed!\n");
}
iRet = fread(pshstr,isize,1,p);
if( 1 != iRet )
{
printf("read the shstrtab from file failed!\n");
}
printf("The Section of Elf file: %d\n",phead->e_shnum );
printf("Index Name size offset \n");
for( i = 1; i< phead->e_shnum ; ++i )
{
printf("%d ", i);
printf("%s ",pshstr+shdr[i].sh_name );//使用下标pshstr[shdr[i].sh_name]就会出现段错误
printf("%d ",shdr[i].sh_size);
printf("%d ",shdr[i].sh_offset);
printf("\n");
}

求大牛给小弟一点提示。
...全文
150 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lin5161678 2012-08-25
  • 打赏
  • 举报
回复
不好意思 CSDN抽搐了 我看不到我的回复 以为没发出就重发了
lin5161678 2012-08-25
  • 打赏
  • 举报
回复
printf("%s ",pshstr+shdr[i].sh_name );//使用下标pshstr[shdr[i].sh_name]就会出现段错误
printf("%s ",&pshstr[shdr[i].sh_name] );//使用下标pshstr[shdr[i].sh_name]就会出现段错误
lin5161678 2012-08-25
  • 打赏
  • 举报
回复
printf("%s ",pshstr+shdr[i].sh_name );//使用下标pshstr[shdr[i].sh_name]就会出现段错误
printf("%s ",&pshstr[shdr[i].sh_name] );//使用下标pshstr[shdr[i].sh_name]就会出现段错误
nightkids_008 2012-08-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
printf("%s ",pshstr+shdr[i].sh_name );//使用下标pshstr[shdr[i].sh_name]就会出现段错误
printf("%s ",&pshstr[shdr[i].sh_name] );//使用下标pshstr[shdr[i].sh_name]就会出现段错误
[/Quote]
3Q for your answer
nightkids_008 2012-08-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
printf("%s ",pshstr+shdr[i].sh_name );//使用下标pshstr[shdr[i].sh_name]就会出现段错误
printf("%s ",&pshstr[shdr[i].sh_name] );//使用下标pshstr[shdr[i].sh_name]就会出现段错误
[/Quote]
下标运算符返回当前位置的元素?不是地址?是这么解释么
lin5161678 2012-08-25
  • 打赏
  • 举报
回复
printf("%s ",pshstr+shdr[i].sh_name );//使用下标pshstr[shdr[i].sh_name]就会出现段错误
printf("%s ",&pshstr[shdr[i].sh_name] );//使用下标pshstr[shdr[i].sh_name]就会出现段错误

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧