简体   繁体   English

不了解指针

[英]Not understanding pointer

I have following C program. 我有以下C程序。

#include <stdio.h>
int main()
{
    int *p, *q;
    *p=5;
    *q=6;
    printf("%d %d", *p, *q);
    return 0;
}

The code is not running. 该代码未运行。 Whats wrong with this code?? 此代码有什么问题??

Your pointer are not initialized, they need to point to valid memory using malloc or by taking address of a local variable. 您的指针未初始化,它们需要使用malloc或通过获取局部变量的地址来指向有效内存。

#include <stdio.h>
int main()
{
    int p, q;
    int *pp = &p;
    int *pq = &q;
    *pp=5;
    *pq=6;
    printf("%d %d", *pp, *pq);
    return 0;
}

would work. 会工作。

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

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