C语言学习笔记-知识点随记2

getchar()

可以读取缓冲器的一个字符,读取错误返回值为EOF(ctrl+z可以返回这个EOF)

可以用getchar()清理缓冲区的内容(需要适当修改)

需要用一变量将getchar里面的值读出来,否则将被下一个值覆盖,无法使用

int tmp=0;
while((tmp=getchar())!='\n')
{
    ;
}

运算优先级

C语言运算符优先级(超详细)_yuliying的专栏-CSDN博客_运算符优先级

putchar()

打印一个字符

关于for()的两种种情形

for(;a<3;a++)如果在涉及到循环嵌套的情况,a依然还是2

for(a+0,b=5;a<5&&b>1;a++,b--)其中只要语句2中间判断前条件为假,此函数将结束循环

关于给结构体数组赋值的问题

#include<stdio.h>
#include<string.h>
struct test {
	char *name1;
	char name2[20];
}t1,t2;

int main()
{
	char * str;
	char str2[20] = "nice to meet you";
	char *name3;
	char name4[20] = "test";
	str = "hello word";
	//特别的
	*name4 = *str;//复制内容
	printf("%s\n", name4);

	//name1
	t1.name1 = str;//交换首地址
	printf("%s\n", t1.name1);
	t1.name1 = str2;//交换首地址
	printf("%s\n", t1.name1);

	//name2
	strcpy(t1.name2, str);//复制内容
	printf("%s\n", t1.name2);
	strcpy(t1.name2, str2);//复制内容
	printf("%s\n", t1.name2);

	//name3
	name3 = str;//交换首地址
	printf("%s\n", name3);
	name3 = str2;//交换首地址
	printf("%s\n", name3);

	//name4
	strcpy(name4, str);//复制内容
	printf("%s\n", name4);
	strcpy(name4, str2);//复制内容
	printf("%s\n", name4);

	//特别的
	*name4 = *str;//复制内容
	printf("%s\n", name4);



	return 0;
}

 结构体里面字符串数组,则赋值的时候使用strcpy(string.h)

 结构体里面是字符指针,则赋值直接用结构体成员(地址)=字符串(地址) 

输出结果
hest                //替换了第一个元素 后面输出原来的
hello word
nice to meet you
hello word
nice to meet you
hello word
nice to meet you
hello word
nice to meet you
hice to meet you    //替换了第一个元素 后面输出原来的 由于strcpy 数组内容改变

 结构体内部定义函数

代码参考: 结构体函数作用及示例_dingdongkk~博客-CSDN博客_结构体函数

#include<stdio.h>
#include<stdlib.h>
 
/*structure declare*/
struct str_func{
    int a;
    int b;
    int (*add)(int a, int b);
    int (*sub)(int a, int b);
    int (*compare)(int a, int b);
};
 
int add(int a, int b){
    return a+b;
}
 
int sub(int a, int b){
    return a - b;
}
 
int compare(int a, int b){
    if (a>b)
        return a;
    else 
        return b;
}
 
/*create a structure and init*/
struct str_func test = {
    .a = 5,
    .b = 7,
    .add = add,              //function pointer point to function
    .sub = sub,
    .compare = compare,
};
 
int main(){
    if (test.compare)
        printf("a b max = %d\n",(test.compare(test.a,test.b)));
    if (test.compare)
        printf("a add b = %d\n",(test.add(test.a,test.b)));
    if (test.compare)
        printf("a sub b = %d\n",(test.sub(test.a,test.b)));
    return 0;
}
————————————————
版权声明:本文为CSDN博主「dingdongkk」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sinat_29891353/article/details/83067747

typedef的使用

typedef的用法,C语言typedef详解 (biancheng.net)

记住如果:

typedef char* Point;

const Point 不等同于const char* 而是等同于char *const(将指针变为常量指针,即指针的地址不可以变)

结构体运用指针返回值

定义一个函数体指针,指针地址不变,则不需要返回值,直接将定义结构体变量t1首地址赋给函数即可

/*结构体使用测试*/
#include<stdio.h>
struct AGE {
	int year;
	int month;
	int day;
}birthday;

typedef struct test {
	int id ;
	struct AGE birthday;
	char *name;

}test2;

void record(test2 *p,int year,int month,int day,int id,char *str )
{
	(*p).birthday.year = year;
	(*p).birthday.month = month;
	(*p).birthday.day = day;
	(*p).id = id;
	(*p).name = str;
}

void test1(test2 *a)
{
	record(a, 1997, 05, 21, 152121, "楚云歌");
}

int main()
{
	test2 t1;
	test1(&t1);
	printf("%s %d %d/%d/%d\n", t1.name, t1.id, t1.birthday.day, t1.birthday.month, t1.birthday.year);
	record(&t1, 1997, 05, 21, 152121, "楚云歌");
	printf("%s %d %d/%d/%d\n", t1.name, t1.id, t1.birthday.day, t1.birthday.month, t1.birthday.year);
	t1.birthday.year = 2222;
	t1.birthday.month = 5;
	t1.birthday.day = 21;
	t1.id = 1502121;
	t1.name = "chuyunge";	
	printf("%s %d %d/%d/%d\n", t1.name, t1.id, t1.birthday.day, t1.birthday.month, t1.birthday.year);
	return 0;
}

函数体整体返回值

 定义一个和原来结构体类型相同的函数,形参类型为结构体内部成员类型,此时在使用时用一个同样结构体类型的结构体变量可以接收到结构体函数内部对于数值的处理,此时如果要封装到一个外部饮用的函数,可以看到的是,在外部函数可以使用,回到主函数内部则不可使用(函数生命周期结束,相关内存被释放,如果想达到效果,建议使用指针)


/*结构体整体返回*/
#include<stdio.h>
struct AGE {
	int year;
	int month;
	int day;
}birthday;

typedef struct test {
	int id ;
	struct AGE birthday;
	char *name;

}test2;

test2 record(int year,int month,int day,int id,char *str )
{
	test2 temp;
	temp.birthday.year = year;
	temp.birthday.month = month;
	temp.birthday.day = day;
	temp.id = id;
	temp.name = str;
	return temp;
}

void test1(test2 *a)
{
 	test2 t1;
	t1 = record(1997, 05, 21, 152121, "chuyunge");
	printf("%s %d %d/%d/%d\n", t1.name, t1.id, t1.birthday.day, t1.birthday.month, t1.birthday.year);
	a = &t1;
	printf("%s %d %d/%d/%d\n", a->name, a->id, a->birthday.day, a->birthday.month, a->birthday.year);
}

int main()
{
	test2 *t1=0x00000000;
	test2 t2;
	t2= record(1997, 05, 21, 152121, "楚云歌");
	printf("%s %d %d/%d/%d\n", t2.name, t2.id, t2.birthday.day, t2.birthday.month, t2.birthday.year);
	test1(t1);
	printf("%s %d %d/%d/%d\n", t1->name, t1->id, t1->birthday.day, t1->birthday.month, t1->birthday.year);
	return 0;
}

运行结果

楚云歌 152121 21/5/1997    //打印结构体返回的数值
chuyunge 152121 21/5/1997  //在外部函数打印出来
chuyunge 152121 21/5/1997  //在外部函数中打印数值
                           //调用外部函数出现问题

 指针直接定义在函数中,可以返回多个值

#include<stdio.h>
void test2(int a, int b, int* p1, int* p2, int* p3)
{
	*p1 = a + b;
	*p2 = a - b;
	*p3 = a * b;
		
}

int main()
{
	int* add = 0, * minus = 0, *times = 0;
	test2(2, 3, &add, &minus, &times);
	printf("add=%-4d  minus=%-4d  times=%-4d\n", add, minus, times);
	return 0;
}

此外话可以定义static和extern来得到想要返回的值

eg:

#include<stdio.h>
extern int b = 0;
static i = 0;
int test1(int a)
{
	a = 3;
	i = 4;
	b = 5;
	return 0;
}
int  main()
{
	test1(i);
	printf("%d\n", i);
	test1(b);
	printf("%d", b);
	return 0;
}

返回值是4 5;由此可见,把变量代入形参进去哪怕在外部函数内部中有这个形参被赋值,仍然不可以带回到主函数中,二在外部函数可以直接用这个变量,并给他赋值,可以带回到主函数中

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值