C语言基础入门详解三

简介: C语言基础入门详解三

一、C语言之函数指针

#include<stdio.h>
#include<stdlib.h>
/**
  函数指针 
*/ 
//定义一个函数 
int add(int x,int y){
  return x+y; 
} 
main(){
  //定义函数指针
  int (*android)(int x, int y);
  //函数指针赋值
  android = add;
  //使用函数指针
  int result = android(12,15);
  printf("result==%d\n",result);
  system("pause");
} 

输出到控制台内容如下:

二、C语言中联合体的理解

#include<stdio.h>
#include<stdlib.h>
/**
  Unition联合体 
*/ 
//定义一个联合体,特点,所有的字段都是使用同一块内存空间 
union Mix{
  long i;//4个字节
  int k; //4个字节
  char ii; //1个字节 
};
main(){
  printf("mix:%d\n",sizeof(union Mix));
  //实验
  union Mix m;
  m.i = 100;
  m.k = 123;
  m.ii = 50; 
  printf("m.i==%d\n",m.i); 
  printf("m.k==%d\n",m.k);
  printf("m.k==%d\n",m.ii);
  system("pause");
} 

输出到控制台内容如下:

三、C语言中的枚举

#include<stdio.h>
#include<stdlib.h>
/**
  枚举的值是递增的 
  默认首元素的值是0 
*/ 
enum WeekDay{
  Monday=0,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday 
};
main(){
  enum WeekDay day = Wednesday;
  printf("%d\n",day); 
  system("pause");
} 

输出到控制台内容如下:

四、C语言中的结构体

#include<stdio.h>
#include<stdlib.h>
/**
   结构体 
*/ 
//定义结构体 
struct student{
  int age;
  float score;
  char sex;
};
main(){
  //使用结构体 
  struct student stu = {
    18,95.6,'W'
  };
   //结构体取值 
   printf("stu.age==%d\n",stu.age);
   printf("stu.score==%.1f\n",stu.score);
   printf("stu.sex==%c\n",stu.sex);
   //结构体赋值
   stu.age = 20;
   stu.score = 100;
   stu.sex = 'M'; 
   printf("stu.age==%d\n",stu.age);
   printf("stu.score==%.1f\n",stu.score);
   printf("stu.sex==%c\n",stu.sex);
   //结构体的长度
   printf("struct student 结构体的长度==%d\n",sizeof(struct student)); 
   system("pause");
} 

输出到控制台内容如下:

五、C语言中结构体指针

#include<stdio.h>
#include<stdlib.h>
/**
   结构体指针 
*/ 
//定义结构体 
struct student{
  int age;
  float score;
  char sex;
};
main(){
  //使用结构体 
  struct student stu = {
    18,95.6,'W'
  };
  //结构体指针
  struct student* point = &stu;
  struct student** point2 = &point;
  //取值运算  (*point).age 等价于 point->age 
   printf("(*point).age ==%d\n",(*point).age);
   printf("point->age ==%d\n",point->age );
   printf("point->score ==%f\n",point->score );
   printf("point->sex ==%c\n",point->sex );
   //赋值运算
   point->age = 20; 
   point->score = 100; 
   point->sex = 'M'; 
   printf("point->age ==%d\n",point->age);
   printf("point->score ==%f\n",point->score);
   printf("point->sex ==%c\n",point->sex );
   //二级结构体指针取值  (*point).age 等价于 point->age 所以 (**point).age 等价于 (*point)->age
   printf("(**point2).age ==%d\n",(**point2).age);
   printf("(*point2)->age ==%d\n",(*point2)->age );
   printf("(*point2)->score ==%f\n",(*point2)->score );
   printf("(*point2)->sex ==%c\n",(*point2)->sex ); 
   //二级结构体指针赋值运算
   (*point2)->age = 60;
   printf("(*point2)->age ==%d\n",(*point2)->age); 
   system("pause");
} 

输出到控制台内容如下:


目录
相关文章
|
23小时前
|
编译器 C语言 C++
从C语言到C++③(第一章_C++入门_下篇)内联函数+auto关键字(C++11)+范围for+nullptr(下)
从C语言到C++③(第一章_C++入门_下篇)内联函数+auto关键字(C++11)+范围for+nullptr
6 0
|
23小时前
|
存储 安全 编译器
从C语言到C++②(第一章_C++入门_中篇)缺省参数+函数重载+引用(下)
从C语言到C++②(第一章_C++入门_中篇)缺省参数+函数重载+引用
4 0
|
23小时前
|
存储 编译器 C语言
从C语言到C++②(第一章_C++入门_中篇)缺省参数+函数重载+引用(中)
从C语言到C++②(第一章_C++入门_中篇)缺省参数+函数重载+引用
4 0
|
23小时前
|
自然语言处理 编译器 C语言
从C语言到C++②(第一章_C++入门_中篇)缺省参数+函数重载+引用(上)
从C语言到C++②(第一章_C++入门_中篇)缺省参数+函数重载+引用
9 0
|
23小时前
|
编译器 C语言 C++
从C语言到C++①(第一章_C++入门_上篇)C++学习介绍(命名空间和C++输入输出流)(下)
从C语言到C++①(第一章_C++入门_上篇)C++学习介绍(命名空间和C++输入输出流)
5 0
|
23小时前
|
Java 编译器 C#
从C语言到C++①(第一章_C++入门_上篇)C++学习介绍(命名空间和C++输入输出流)(上)
从C语言到C++①(第一章_C++入门_上篇)C++学习介绍(命名空间和C++输入输出流)
11 0
|
5天前
|
C语言
C语言——入门分支与循环
C语言——入门分支与循环
4 0
|
6天前
|
编译器 C语言
函数深入解析(C语言基础入门)
函数深入解析(C语言基础入门)
|
6天前
|
C语言
数组深入剖析(C语言基础入门)
数组深入剖析(C语言基础入门)
|
6天前
|
算法 C语言 芯片
振南技术干货集:振南当年入门C语言和单片机的那些事儿(1)
振南技术干货集:振南当年入门C语言和单片机的那些事儿(1)