简体   繁体   English

我们可以通过另一个包含我们希望 Pointer 指向的其他变量的地址的变量为指针分配地址吗?

[英]Can we assign address to a pointer by another variable containing address of some other variable we want Pointer to point to?

I was trying to manually give address through another variable rather than using & .我试图通过另一个变量手动给出地址,而不是使用&

#include <stdio.h>

int main()
{
    int ab = 99;
    int *p;
    int f = &ab;
    p = f;
    printf("%d\n%d\n%d\n%d\n", &ab, p, f, p); // prints same address
    printf("%d\n", *p); // By adding this line, gives SEGFAULT
}

./a.out Gives: ./a.out给出:

284008584
284008584
284008584
284008584
Segmentation fault (core dumped)

You're attempting to assign a pointer value to an int .您正在尝试将指针值分配给int If a pointer is larger than an int on your system, which it most likely is if you're running on a 64 bit OS, then you end up loosing data in the conversion.如果系统上的指针比int大(最有可能是在 64 位操作系统上运行),那么您最终会在转换中丢失数据。 When you then convert back you end up with an invalid pointer, and attempting to dereference this pointer causes a crash.当您然后转换回来时,您最终会得到一个无效的指针,并且尝试取消引用该指针会导致崩溃。

You're also invoking undefined behavior by using the wrong format specifier to print a pointer.您还通过使用错误的格式说明符打印指针来调用未定义的行为。 You should be using %p , and you should be casting the pointer to a void * (one of the few cases when casting to void * is required).您应该使用%p ,并且应该将指针转换为void * (需要转换为void *的少数情况之一)。 If you do this the loss of data in the conversion will become apparent.如果您这样做,转换中的数据丢失将变得明显。

  1. Please raise the warning level of your compiler to the maximum, for GCC its -Wall -Wextra , if you like also -pedantic .如果您也喜欢-Wall -Wextra ,请将编译器的警告级别提高到最大,对于 GCC 其-Wall -Wextra -pedantic Because:因为:
  2. There is an error int your line int f = &ab;你的行中有一个错误 int int f = &ab; . . The expression on the RHS is an address of an int , but the variable on the LHS is an int . RHS 上的表达式是int的地址,但 LHS 上的变量是int

On your system the sizes of integers and pointers might be different, that's why dereferencing a pointer which was set from an integer segfaults.在您的系统上,整数和指针的大小可能不同,这就是取消引用从整数段错误设置的指针的原因。

In addition to all the above correct answers about different sizes for pointer and integer,除了以上关于指针和整数不同大小的所有正确答案外,
if you modify your line which prints the addresses to use the correct format modifier, you will see the output addresses are different, due to the loss during cast from pointer to int :如果您修改打印地址的行以使用正确的格式修饰符,您将看到输出地址不同,这是由于从指针转换为int期间的丢失:

printf("%p\n%x\n%p\n",&ab,p,f); // prints same address

Example:例子:

0x7ffc8b716ea8
8b716ea8
0x8b716ea8

Check if the size of int and the size of int * is same, so that an integer variable can save the address without losing any data.检查int的大小和int *的大小是否相同,以便整数变量可以保存地址而不会丢失任何数据。 Check this with below code:使用以下代码检查这一点:

printf("%lu, %lu \n", sizeof(int*), sizeof(int));

If both are not same, one can face the errors.如果两者不相同,则可以面对错误。

I will suggest take variable type uintptr_t , which can accommodate the pointer's address in it.我建议采用变量类型uintptr_t ,它可以容纳指针的地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM