指针问题 待高手

movie0125 2011-07-29 05:00:16
1. 想将 -1 赋值给一个指针
如:
dynamic_buff_node_t* a = (dynamic_buff_node_t*)-1;
警告:warning: assignment from incompatible pointer type

2. 对void*类型的指针做运算
如:
void* p_elements;
for (; i_count--; p_elements += i_element_size){……}
警告:warning: pointer of type 'void *' used in arithmetic

3. 结构体中包含结构体,但是初始化有警告
如:
typedef struct a {int a;} A;
typedef struct b {struct ba; int b;} B;
typedef struct c {struct cba; int c;} C;
C test = {0};
警告:warning: missing braces around initializer
warning: (near initialization for 'ba.a')

高手速速现身!
...全文
243 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
movie0125 2011-08-01
  • 打赏
  • 举报
回复
谢谢各位的指导,以上问题基本解决,但是那个结构体初始化还是报警告,
我的结构体里面有四层结构体,结构体定义的是全局变量,上面的方法试过后,还是警告信息。
来自山里的娃 2011-07-29
  • 打赏
  • 举报
回复
1 改写成这样没警告
#include <stdio.h>
typedef struct
{
char i;
}dynamic_buff_node_t;

int main(void)
{
dynamic_buff_node_t* a = (dynamic_buff_node_t*)-1;
printf("%c",a->i);
return 0;
}
2 void 类型不能做运算
jernymy 2011-07-29
  • 打赏
  • 举报
回复
gcc version 4.3.3 (Ubuntu 4.3.3-5ubuntu4)

jernymy@ubuntu:/mnt/hgfs/linux/07$ gcc -Wall -c warna.c
jernymy@ubuntu:/mnt/hgfs/linux/07$



#include "stdio.h"

typedef struct a {int a;} A;
typedef struct b {struct b *a ; int b;} B;
typedef struct c {struct c *ba; int c;} C;

typedef struct tagTmp
{
int nData;
struct tagTmp *ptNext;
}dynamic_buff_node_t;

int main(void)
{
dynamic_buff_node_t* a = (dynamic_buff_node_t*)-1;
void* p_elements;
int i_count = 10;
int i_element_size = 2;

C test = {0};
A tA;
B tB;

printf("&tA:%p\n", &tA);
printf("&tB:%p\n", &tB);

for (; i_count--; p_elements += i_element_size)
{
;
}
test.c = 10;
printf("&a:%p\n", a);

return 0;
}
movie0125 2011-07-29
  • 打赏
  • 举报
回复
union 中换成dynamic_buff_node_t* 也不行
movie0125 2011-07-29
  • 打赏
  • 举报
回复
这个试过了,不行
zywx2009 2011-07-29
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 movie0125 的回复:]

*(int*)(&a) = -1; 这个看不懂……
[/Quote]

dynamic_buff_node_t* a = 0;
*(int*)(&a) = -1;

1.&a取结构体的地址。
2.(int*)(&a)把取到的结构体指针转成int*类型指针
3.*(int*)(&a) = -1 把-1赋值给 取到的结构体指针转成int*类型指针指向的内容
movie0125 2011-07-29
  • 打赏
  • 举报
回复
我刚刚这样试了一下
external/vgx/libchain/src/buffchain.c:8: error: expected identifier or '(' before 'int'
报错了
www_adintr_com 2011-07-29
  • 打赏
  • 举报
回复
把指针类型转换成左值的 int 再用 -1 给它赋值.
movie0125 2011-07-29
  • 打赏
  • 举报
回复
*(int*)(&a) = -1; 这个看不懂……
www_adintr_com 2011-07-29
  • 打赏
  • 举报
回复
union 还不能去掉警告? 那你把结构里面的 void* 改成 dynamic_buff_node_t* 吧.

如果不想用 union 还可以这样:
dynamic_buff_node_t* a = 0;
*(int*)(&a) = -1;
movie0125 2011-07-29
  • 打赏
  • 举报
回复
刚刚试过了用union结构也不能去掉警告
movie0125 2011-07-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 adlay 的回复:]

1. 从 int 类型转换成指针是有警告的. 如果是 VC 可以这样转换来去除警告:
dynamic_buff_node_t* a = (dynamic_buff_node_t*)(INT_PTR)-1;
更通用点的话可以这样实现无警告的转换:
union {
void* p;
int i;
} x;
x.i = -1;

[/Quote]

我用的是linux的环境,又不想用union的结构,有其他的办法吗?

MicroStationFan 2011-07-29
  • 打赏
  • 举报
回复
3. 你的C结构中还含有一个结构,就需要这样来初始化:
C test = {{...}, 0};
shaoyunmeng 2011-07-29
  • 打赏
  • 举报
回复
我顶!
www_adintr_com 2011-07-29
  • 打赏
  • 举报
回复
1. 从 int 类型转换成指针是有警告的. 如果是 VC 可以这样转换来去除警告:
dynamic_buff_node_t* a = (dynamic_buff_node_t*)(INT_PTR)-1;
更通用点的话可以这样实现无警告的转换:
union {
void* p;
int i;
} x;
x.i = -1;
dynamic_buff_node_t* a = (dynamic_buff_node_t*)x.p;

2. 指针运算是和其指向的类型相关的. 转换成 char* 类型再运算就可以了.
3. 去掉那个 a,b,c 改成这样:
typedef struct {int a;} A;
typedef struct {struct ba; int b;} B;
typedef struct {struct cba; int c;} C;

C test = {0};
fengguixian520 2011-07-29
  • 打赏
  • 举报
回复
沙发!

69,382

社区成员

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

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