简体   繁体   English

警告:指针在打印其地址时使用未初始化

[英]Warning: pointer is used uninitialized when printing its address

I'm new to learning programming by the way so don't be too harsh.顺便说一句,我是学习编程的新手,所以不要太苛刻。 Why exactly am I getting this warning and how do I fix it?为什么我会收到此警告,我该如何解决?

Here's the full code:这是完整的代码:

#include <stdio.h>

int main()
{
    int a; // this declares the 'a' variable
    int *ptr_a; // this declares the pointer variable

    printf("a:");
    scanf("%d", &a);

    printf("a is %d\n", a); // this prints the value of 'a' that is read from the keyboard
    printf("the address of the pointer is %p\n", ptr_a); // this prints the address of the pointer

    ptr_a = &a; // this points the pointer to the 'a' variable
    *ptr_a = *ptr_a + 5; // this modifies the 'a' variable with its pointer

    printf("the value of the modified a is %d\n", a); // this prints the modified value of 'a'
    printf("the address of the modified pointer is %p\n", ptr_a); // this prints the modified address of the pointer

    return 0;
}

The warnings are to take seriously, even though they may seem unnecessary at times, doing so will create good programming habits, you can initialize the pointer to NULL then reassign it later, when you need to:警告是要认真对待的,即使它们有时看起来不必要,这样做会养成良好的编程习惯,您可以将指针初始化为NULL ,然后在需要时重新分配它:

int *ptr_a = NULL;

However what's triggering the problem here is the fact that you are printing the value of the pointer not its address, which would be perfectly fine as it still has an address even when uninitialized, it only has a value when you assign it the address of a variable or otherwise make it point to some memory location.然而,这里引发问题的是你打印的是指针的值而不是它的地址,这很好,因为它即使在未初始化时仍然有一个地址,只有当你为它分配一个地址时它才有一个值变量或以其他方式使其指向某个 memory 位置。

If you want to print the address of the pointer you'll need &ptr_a and for good measure, being pedantic I'll admit, cast it to void* as %p specifier expects a pointer to void , not to int :如果你想打印指针的地址,你需要&ptr_a并且为了好的措施,我承认是迂腐的,将它转换为void*因为%p说明符期望指针指向void ,而不是int

printf("the address of the pointer is %p\n", (void*)&ptr_a);

This line:这一行:

printf("the address of the pointer is %p\n", ptr_a); // this prints the address of the pointer

does not print the address of the pointer.不打印指针的地址。 It attempts to print the value of ptr_a .它尝试打印ptr_a的值。 Since ptr_a has not been initialized, the compiler warns you that you are using it uninitialized.由于ptr_a尚未初始化,编译器会警告您正在使用未初始化的它。

If you printed the address of ptr_a , with:如果您打印了ptr_a的地址,则:

printf("the address of the pointer is %p\n", (void *) &ptr_a);

Then the compiler will not warn use, because this does not use the value of ptr_a (which is not initialized), just its address (which is set when ptr_a is created).然后编译器不会警告使用,因为这不使用ptr_a的值(未初始化),只是它的地址(在创建ptr_a时设置)。

After the line ptr_a = &a;在行ptr_a = &a;之后, ptr_a is initialized with a value, and a line like this: , ptr_a被初始化为一个值,一行是这样的:

printf("The address of a is %p.\n", (void *) ptr_a);

will print the value without a compiler warning.将在没有编译器警告的情况下打印该值。

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

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