C语言 函数指针 、 指针函数、字符转换、设置随机数、时间函数、char指针类型分析

1.函数指针
type (*name)(参数){} ----- 相当于接口。

可以作为一般函数的参数进行调用。
举例:线程调用思路。
----本身是指针,指针指向的是一个函数类型的地址。

2.指针函数

---- 本身是函数,函数的返回值是一个指针

void*  arr(void){}
3.字符转换

小写字符 = 大写字符 + 32。
字符串转整型:

  • 字符“A”的ASCII码和十进制ASCII值分别是多少?
  • A是65,a是97,65换为16进制就是41,所以是0x41
#include <stdlib.h>
int atoi(const char *nptr); ---把字符串转整型。
long atol(const char *nptr); ---把字符串转长整型。
long long atoll(const char *nptr); ----把字符串转双字。
4.设置随机数

编写程序产生随机数:使得程序每次运行出来的结果都不一样。
头文件:

	#include <stdlib.h>  //c语言标准函数库
	#include <sys/time.h> //时间函数头文件

实现代码:

	struct timeval t;   //时间结构体
	gettimeofday(&t,NULL);  //获取系统时间
	srandom(t.tv_usec); //设置随机数种子 时间微妙数
	int a = rand()%100 //生成随机数

函数原型:

int gettimeofday(struct timeval *tv, struct timezone *tz);
struct timeval
{
	time_t  tv_sec;     /* seconds */
	suseconds_t  tv_usec;    /* microseconds */
};
5.时间函数

//获取时间(从1970-1-1 0:0:0到现在的秒数)

time_t time(time_t *tloc);

//time_t的时间间距换算为当前时间–并且以字符形式返回

char *ctime(const time_t *timep); 

//将time_t的时间间距换算为时间结构体

struct tm *gmtime(const time_t *timep);

时间结构体:

struct tm {
	int tm_sec;    /* Seconds (0-60) */
	int tm_min;    /* Minutes (0-59) */
	int tm_hour;   /* Hours (0-23) */
	int tm_mday;   /* Day of the month (1-31) */
	int tm_mon;    /* Month (0-11) */
	int tm_year;   /* Year - 1900 */
	int tm_wday;   /* Day of the week (0-6, Sunday = 0) */
	int tm_yday;   /* Day in the year (0-365, 1 Jan = 0) */
	int tm_isdst;  /* Daylight saving time */
};
6.char指针类型分析:

初始化字符串的时候:

1.char *a = "google";

此时当使用拼接strcat时会出现:段错误
段错误 (核心已转储)

//指针指向常字符串类型,不可对指向的类型进行修改

2.char a[] = "google";

此时系统会分配数组a :7个字节,当使用拼接strcat时会出现:栈溢出
*** stack smashing detected ***: terminated
//指定长度的char数组类型 系统会分配栈空间给a ,所以可以对a的内容进行修改。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值