C中的基本指针?

tha*_*rif 4 c pointers

我在下面提到三个不同的例子.我不明白为什么ex1有相同的ex2输出和ex3的输出不同,为什么ex2与ex3不同,我只是在另一行创建!

EX1

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int x=2;
    int *y;
    y = &x;
    printf("value: %d\n", *y);
    printf("address: %d\n", y);
    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

产量

value: 2
address: 2686744
Run Code Online (Sandbox Code Playgroud)

EX2

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int x=2;
    int *y = &x;
    printf("value: %d\n", *y);
    printf("address: %d\n", y);
    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

产量

value: 2
address: 2686744
Run Code Online (Sandbox Code Playgroud)

EX3

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int x=2;
    int *y;
    *y = &x;
    printf("value: %d\n", *y);
    printf("address: %d\n", y);
    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

产量

value: 2686744
address: 2130567168
Run Code Online (Sandbox Code Playgroud)

当我认为明星必须成为(y)NOT(int)并且我用(int)NOT(y)(^_ ^)明星时,我对指针有很大的误解我现在对我来说一切都很清楚...感谢所有你的答案

Nic*_*ica 5

在示例3中,您首先声明一个指针:

int *y;
Run Code Online (Sandbox Code Playgroud)

然后你说int价值*y是地址x.

那是因为int *y你有声明:

  • y 是类型的 int *
  • *y是类型的int.

因此,示例3中的正确代码行应为:

int *y;
y = &x;
Run Code Online (Sandbox Code Playgroud)