[英]Can we use a non-pointer as a pointer in C
I'm fairly new to C but I was wondering, can we use something like an unsigned int like a void pointer. 我对C还是很陌生,但我想知道,我们可以使用像unsigned int这样的东西(如void指针)吗? I wrote this short example to illustrate what I want to do but I can't seem to get it to work so I was wondering if it was even possible:
我写了这个简短的示例来说明我想做什么,但是我似乎无法使其正常工作,因此我想知道是否有可能:
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);
}
} }
The above code is what I want to get working but this next example is what I want it to work as: 上面的代码是我想要的工作,但是下一个示例是我希望它的工作方式:
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);
}
}
I understand that what I'm trying to do is probably unconventional but I really want to know if I can do this. 我了解我想做的可能与众不同,但我真的很想知道我是否可以这样做。 When I try to compile the first one I get an error that says this:
当我尝试编译第一个文件时,出现错误消息:
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));
When I run the second i get this: 当我运行第二个我得到这个:
[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
After taking your responses into consideration, I changed the original code to this: 考虑到您的答复后,我将原始代码更改为:
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));
}
} }
Its not exactly what I was trying to do but I guess what I wanted to do can't be done so this code serves the same output. 它不完全是我要尝试执行的操作,但是我想我想执行的操作无法完成,因此此代码提供了相同的输出。 Editing the code led me to this next small question.
编辑代码使我想到了下一个小问题。 Why can we use:
我们为什么可以使用:
unsigned_int = (unsigned int *)((char *) unsigned_int + sizeof(char));
and when we try it with int, it jumps too many bytes at a time and gives us random numbers. 当我们尝试使用int时,它一次跳转了太多字节,并为我们提供了随机数。 To fix that issue I just changed it to:
为了解决这个问题,我将其更改为:
unsigned_int = (unsigned int *) ((int *) unsigned_int + 1);
But why didn't it work with sizeof(int)? 但是为什么它不能与sizeof(int)一起使用?
In general, no. 一般来说,没有。 There is no guarantee that any pointer can be represented in
unsigned int
. 不能保证任何指针都可以用
unsigned int
表示。 The conversion from pointers to integers is implementation-defined, and might be meaningless. 从指针到整数的转换是实现定义的,可能没有意义。 In practice it usually is meaningful, but you need an integer type with sufficiently many value bits to represent all pointers.
实际上,它通常是有意义的,但是您需要一个具有足够多的值位的整数类型来表示所有指针。
uintptr_t
(from stdint.h
), if defined, is such a type. uintptr_t
(来自stdint.h
)(如果已定义)就是这种类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.