C指针与结构体
约 381 字大约 1 分钟
2025-02-12
1 指针
- 指针用于指向存储在计算机内存中任何位置的值的地址
#include <stdio.h>  
int main()
{
    int a = 5;
    int *b;
    int **c;
    b = &a;
    c = &b;
    printf("value of a = %d\n", a);
    printf("value of a = %d\n", *(&a));
    printf("value of a = %d\n", *b);
    printf("value of a = %d\n", **c);
    printf("value of b = address of a = %u\n", b);
    printf("value of c = address of b = %u\n", c);
    printf("address of a = %u\n", &a);
    printf("address of a = %u\n", b);
    printf("address of a = %u\n", *c);
    printf("address of b = %u\n", &b);
    printf("address of b = %u\n", c);
    printf("address of c = %u\n", &c);
    system("pause");
    return 0;
}
//更多请阅读:https://www.yiibai.com/data_structure/data-structure-pointer.html
//result
value of a = 5
value of a = 5
value of a = 5
value of a = 5
value of b = address of a = 16252636
value of c = address of b = 16252624
address of a = 16252636
address of a = 16252636
address of a = 16252636
address of b = 16252624
address of b = 16252624
address of c = 16252612
//更多请阅读:https://www.yiibai.com/data_structure/data-structure-pointer.html#article-start2 结构体
- 定义了一组变量列表,这些变量将放在一个内存块中的一个名称下
- 使用指向结构的一个指针来访问不同的变量
#include<stdio.h>  
#include<conio.h>  
void main( )  
{  
    struct employee  
    {  
        int id ;  
        float salary ;  
        int mobile ;  
    };
    // 定义结构体变量
    struct employee e1,e2,e3 ;  
    clrscr();  
    printf ("\nEnter ids, salary & mobile no. of 3 employee\n"  
    scanf ("%d %f %d", &e1.id, &e1.salary, &e1.mobile);  
    scanf ("%d%f %d", &e2.id, &e2.salary, &e2.mobile);  
    scanf ("%d %f %d", &e3.id, &e3.salary, &e3.mobile);  
    printf ("\n Entered Result ");  
    printf ("\n%d %f %d", e1.id, e1.salary, e1.mobile);  
    printf ("\n%d%f %d", e2.id, e2.salary, e2.mobile);  
    printf ("\n%d %f %d", e3.id, e3.salary, e3.mobile);  
    getch();  
}
//更多请阅读:https://www.yiibai.com/data_structure/data-structure-structure.html#article-start