C语言查漏补缺

大一上学期学的C语言,大一下学期学的Java,大二接触了Python,目前大二开了数据结构,用的是C语言,学的语言虽然很多,但是都不精,因此,学的也不扎实,所以,准备边学边补吧,特此,为这些语言可以说是做笔记吧。

C语言的字符串
定义

char a[]

输入

gets(s);或scanf("%s",s);

输出

puts(s);或printf("%s",s);

C语言的结构体

struct struct_name{
	struct_members list;
}struct_variables;

例如

struct student{
	int score;
	char name[];
	int num;
}a;
等价于
 struct student1{
int num;
struct student *next;
};
struct student a;

通过使用a.score,a.name,a.num就可以对结构体初始化和相应赋值,初始化也可以在变量定义时进行

struct student1{
	int score;
	char name[];
	int num;
}a={01,"Joy",78};

结构体数组

struct student1{
	int score;
	char name[];
	int num;
}a[5]={01,"Joy",78};

结构体指针 结合结构体变量使用

        struct student1 *p;
	struct student1{
		int score;
		char name[];
		int num;
	}a;
	struct student1 *p;
	p=&a;

小实例:把一个学生信息(姓名,学号,成绩)放在一个结构体变量里,然后输出信息

#include <stdio.h>
#include <stdlib.h>
void input(int score,char name[],int num){
	printf("num:");
	scanf("%d",&num);
	printf("score:");
	scanf("%d",&score);
	printf("name:");
	scanf("%s",name);
}
char prin(int score,char name[],int num){
	return printf("num:%d\nname:%s\nscore:%d",num,name,score);

} 
int main(){
	struct student1{
		int score;
		char name[];
		int num;
	}a;
	struct student2{
		int score;
		char name[];
		int num;
	}b;

	input(a.score,a.name,a.num);
	input(b.score,b.name,b.num); 
	a.score>b.score?prin(a.num,a.name,a.score):(a.score<b.score?prin(b.num,b.name,b.score):prin(b.num,b.name,b.score)+prin(a.num,a.name,a.score));
}

结构体建立链表

 struct student1{
	int num;
	struct student *next;    //next为指针变量,指向结构体变量
};

一个简单的静态单链表

#include<stdio.h>
struct node{
	int num;
	struct node *next;
};
int main(){
	struct node a,b,c,*head,*p;
	a.num=23;
	b.num=45;
	c.num=33;
	head=&a;
	a.next=&b;
	b.next=&c;
	c.next=NULL;
	p=head;
	while(p){
		printf("%d\n",p->num);
		p=p->next;
	}
} 

typedef 声明新类型名

  • 重命名,相当于name as
typedef int Integer
  • 简化复杂的表示方法
  1. 简化结构体的变量定义
typedef struct student{
	int num;
	char name[];
	int score;
	struct node *next;
}S1,S2;
S1 m;     //定义了结构体变量m,等价于没有typedef时的struct student m

2.简化数组

typedef int Num[100]; //Num为含100个元素的整型数组类型 
Num=a;  //定义a为含100个元素的整型数组 

以及其他的

typedef char* s;//字符型指针重命名为s

猜你喜欢

转载自blog.csdn.net/zhengmmm1999/article/details/83687569