经典编程900例(c语言)(第八篇)

例87:结构体的使用

#include <stdio.h>

// 定义结构
struct Shape {
    int type;
    int color;
    float radius;
    float area;
    float perimeter;
};

/**
 * 给结构体变量赋值
 * @Author dust_fall
 * @param  shape     要改变结构体变量的指针
 */
void change_structure(struct Shape *shape)
{
    (*shape).type = 0;
    (*shape).color = 1;
    (*shape).radius = 5.0;
    (*shape).area = 22.0 / 7.0 * (*shape).radius * (*shape).radius; 
    (*shape).perimeter = 2.0 * 22.0 / 7.0 * (*shape).radius;
}

int main(int argc, char const *argv[])
{
    struct Shape circle;

    change_structure(&circle);

    printf("circle.type %d\n", circle.type);
    printf("circle.color %d\n", circle.color);
    printf("circle.radius %f circle.area %f circle.perimeter %f\n", circle.radius, circle.area, circle.perimeter);

    return 0;
}

例88:结构体的另一种使用(看看和上面的区别)

#include <stdio.h>

struct Shape {
    int type;
    int color;
    float radius;
    float area;
    float perimeter;
};

void change_structure(struct Shape *shape)
{
    shape->type = 0;
    shape->color = 1;
    shape->radius = 5.0;
    shape->area = 22.0 / 7.0 * shape->radius * shape->radius; 
    shape->perimeter = 2.0 * 22.0 / 7.0 * shape->radius;
}

int main(int argc, char const *argv[])
{
    struct Shape circle;
   
    change_structure(&circle);
   
    printf("circle.type %d\n", circle.type);
    printf("circle.color %d\n", circle.color);
    printf("circle.radius %f circle.area %f circle.perimeter %f\n", circle.radius, circle.area, circle.perimeter);
    return 0;
}

例89:一段古老的代码

#include <stdio.h>
// 据说这里的dos.h在非纯16位dos系统下无法调用, 博主试过gcc、vc、vs都无法通过编译
// 错误是“curr_date”使用未定义的 struct“main::date”
#include <dos.h>

int main(int argc, char const *argv[])
{
    struct date curr_date;

    getdate(&curr_date);

    printf("Current date: %d-%d-%d\n", curr_date.da_mon, curr_date.da_day, curr_date.da_year); 
    
    return 0;
}

例90:又一段古老的代码

#include <stdio.h>
#include <dos.h>

int main(int argc, char const *argv[])
{
    union REGS inregs, outregs;

    inregs.h.ah = 0x30;
    inregs.h.al = 0;
    intdos(&inregs, &outregs);

    printf("Current version %d.%d\n", outregs.h.al, outregs.h.ah);

    return 0;
}

例91:还一段古老的代码

#include <stdio.h>
#include <dos.h>

void main(void)
{
	union REGS inregs, outregs;
	
	inregs.x.ax = 0x3000;
	intdos(&inregs, &outregs);
	
	printf("Current version %d.%d\n", outregs.x.ax & 0xFF, outregs.x.ax >> 8);
}

例92:似乎又一段…(Dos下near指针,far指针,huge指针)

#include <stdio.h>
#include <malloc.h>

int main(int argc, char const *argv[])
{
    int i;
    float huge *values;

    if ((values = (float huge *) halloc (17000, sizeof(float))) == NULL)
        printf ("Error allocating huge array\n");
    else
    {             
        printf("Filling the array\n");

        for (i = 0; i < 17000; i++)
            values[i] = i * 1.0;

        for (i = 0; i < 17000; i++)
            printf ("%8.1f ", values[i]);

        hfree(values);
    }
    return 0;
}

例93:在main中定义结构体

#include <stdio.h>

int main(int argc, char const *argv[])
{
    // 定义结构并初始化变量
    struct Shape {
        int type;
        int color;
        float radius;
        float area;
        float perimeter;
    } circle = {0, 1, 5.0, 78.37, 31.42};
       
    printf("circle.type %d\n", circle.type);        // circle.type 0
    printf("circle.color %d\n", circle.color);      // circle.color 1
    printf("circle.radius %f circle.area %f circle.perimeter %f\n", circle.radius, circle.area, circle.perimeter);  // circle.radius 5.000000 circle.area 78.370003 circle.perimeter 31.420000
    
    return 0;
}

例94:使用指针修改变量

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int counter = 10;
    // 定义指针变量
    int *iptr;        

    // 指针指向counter变量
    iptr = &counter; 
    printf("Addres in iptr %x Value at *iptr %d\n", iptr, *iptr);   // Addres in iptr 61ff18 Value at *iptr 10

    // 通过指针修改变量
    *iptr = 25;     

    printf("Value of counter %d\n", counter);   //Value of counter 25

    return 0;
}

例95:使用指针遍历数组

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int values[5] = {1, 2, 3, 4, 5};
    int counter;
    int *iptr;

	// 指向数组首元素(数组名就是数组的首地址)
    iptr = values;

    for (counter = 0; counter < 5; counter++)
    {
        printf("%d\n", *iptr);
		// 指针++来指向下一个元素
        iptr++;
    }
    
    return 0;
}

例96:不使用printf来打印字符串

#include <stdio.h>

void show_string(char *string)
{
    while (*string)
        // putchar函数打印单个字符
        putchar(*string++);
}

int main(int argc, char const *argv[])
{
    show_string("Jamsa's 1001 C/C++ Tips");

    return 0;
}

例97:结构体字符串的访问

#include <stdio.h>

int main(int argc, char const *argv[])
{
    struct Date {
        char month_name[64];
        int  month;
        int  day;
        int  year;
    } current_date = { "July", 7, 4, 1994 };

    int i;

    // 当结构体变量的month_name字符串到'\0'时结束循环
    for (i = 0; current_date.month_name[i]; i++)
        putchar(current_date.month_name[i]);

    return 0;
}

例98:结构体变量做实参传入函数

#include <stdio.h>

struct Shape {
    int type;
    int color;
    float radius;
    float area;
    float perimeter;
};

void show_structure(struct Shape shape)
{
    printf("shape.type %d\n", shape.type);
    printf("shape.color %d\n", shape.color);
    printf("shape.radius %f shape.area %f shape.perimeter %f\n", shape.radius, shape.area, shape.perimeter);
}

int main(int argc, char const *argv[])
{
    struct Shape circle;

    circle.type = 0;
    circle.color = 1;
    circle.radius = 5.0;
    circle.area = 22.0 / 7.0 * circle.radius * circle.radius; 
    circle.perimeter = 2.0 * 22.0 / 7.0 * circle.radius;

    show_structure(circle);
    
    return 0;
}

例99:关于共用体(联合体)的内存大小问题

#include <stdio.h>
/**
 * 共用体(联合体)的共用内存等于最大属性的字节数
 * (如果共用体内部包含结构体还需考虑内存对齐)
 */    
int main(int argc, char const *argv[])
{
    union EmployeeDates {
        int days_worked;
        struct Date {
            int month;
            int day;
            int year;
        } last_day;
    } emp_info;

    union Numbers {
        int a;
        float b;
        long c;
        double d;
    } value;

    printf("Size of EmployeeDates %d bytes\n", sizeof(emp_info));// Size of EmployeeDates 12 bytes
    printf("Size of Numbers %d bytes\n", sizeof(value));         // Size of Numbers 8 bytes

	// 关于内存对齐的计算, 这里不多做介绍, 可以自己百度(其实不复杂, 但要用文字组织就多了), 实在不明白的可以私信博主
	
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值