我对C相当新,但我想知道,我们可以像无效指针一样使用像unsigned int这样的东西。我写了这个简短的例子来说明我想做什么,但我似乎无法让它工作,所以我想知道它是否有可能:
int main () {
int i;
char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
int int_array[5] = {1, 2, 3, 4, 5};
unsigned int unsigned_int;
unsigned_int = (unsigned int) int_array;
for(i=0; i < 5; i++) {
printf("[unsigned_int] points to %p, which contains the int %d\n", unsigned_int, *((int *) unsigned_int));
unsigned_int = unsigned_int + sizeof(int);
}
unsigned_int = (unsigned int) char_array;
for(i=0; i<5 ; i++) {
printf("[unsigned_int] points to %p, which contains the char '%c'\n", unsigned_int, *((char *) unsigned_int));
unsigned_int = unsigned_int + sizeof(char);
}
}
上面的代码是我想要的工作,但下一个例子是我希望它的工作原理:
int main () {
int i;
int int_array[5] = {1, 2, 3, 4, 5};
char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
void *void_pointer;
void_pointer = (void *) int_array;
for(i=0; i < 5; i++) {
printf("[integer pointer] has an adress of %p, and contains %d\n", void_pointer, *((int *)void_pointer));
void_pointer = (void *) ((int *)void_pointer + 1);
}
void_pointer = (void *)char_array;
for(i=0; i<5; i++) {
printf("[char pointer] has an adress of %p and contans %c\n", void_pointer, *((char *)void_pointer));
void_pointer = (void *) ((char *)void_pointer + 1);
}
}
我明白我试图做的事情可能是非传统的,但我真的想知道我是否能做到这一点。当我尝试编译第一个时,我得到一个错误,上面写着:
pointer_type5.c: In function ‘main’:
pointer_type5.c:12:24: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
unsigned_int = (unsigned int) int_array;
^
pointer_type5.c:15:92: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
printf("[unsigned_int] points to %p, which contains the int %d\n", unsigned_int, *((int *) unsigned_int));
^
pointer_type5.c:20:24: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
unsigned_int = (unsigned int) char_array;
^
pointer_type5.c:23:103: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
printf("[unsigned_int] points to %p, which contains the char '%c'\n", unsigned_int, *((char *) unsigned_int));
当我跑第二个时,我得到了这个:
[integer pointer] has an adress of 0x7fffcba7a5f0, and contains 1
[integer pointer] has an adress of 0x7fffcba7a5f4, and contains 2
[integer pointer] has an adress of 0x7fffcba7a5f8, and contains 3
[integer pointer] has an adress of 0x7fffcba7a5fc, and contains 4
[integer pointer] has an adress of 0x7fffcba7a600, and contains 5
[char pointer] has an adress of 0x7fffcba7a5e0 and contans a
[char pointer] has an adress of 0x7fffcba7a5e1 and contans b
[char pointer] has an adress of 0x7fffcba7a5e2 and contans c
[char pointer] has an adress of 0x7fffcba7a5e3 and contans d
[char pointer] has an adress of 0x7fffcba7a5e4 and contans e
在考虑了您的回答后,我将原始代码更改为:
int main () {
int i;
char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
int int_array[5] = {1, 2, 3, 4, 5};
unsigned int *unsigned_int;
unsigned_int = (unsigned int *) int_array;
for(i=0; i<5; i++) {
printf("[unsigned_int] points to %p, which contains the int %d\n", unsigned_int, *((int *) unsigned_int));
unsigned_int = (unsigned int *) ((int *) unsigned_int + 1);
}
unsigned_int = (unsigned int *) char_array;
for(i=0; i<5 ; i++) {
printf("[unsigned_int] points to %p, which contains the char '%c'\n", unsigned_int, *((char *) unsigned_int));
unsigned_int = (unsigned int *)((char *) unsigned_int + sizeof(char));
}
}
这不完全是我想做的事情,但我想我想做的事情是不能完成的,所以这段代码服务于同样的输出。编辑代码让我想到了下一个小问题。 我们为什么可以使用:
unsigned_int = (unsigned int *)((char *) unsigned_int + sizeof(char));
当我们用int尝试它时,它一次跳过太多字节并给我们随机数。要解决这个问题,我只需将其更改为:
unsigned_int = (unsigned int *) ((int *) unsigned_int + 1);
但为什么它不适用于sizeof(int)?
答案 0 :(得分:1)
一般来说,没有。无法保证任何指针都可以在unsigned int
中表示。从指针到整数的转换是实现定义的,可能毫无意义。实际上它通常是有意义的,但是你需要一个具有足够多的值位的整数类型来表示所有指针。 uintptr_t
(来自stdint.h
),如果已定义,则为此类型。