当前位置:网站首页>【C】atoi和offsetof的介绍和模拟实现
【C】atoi和offsetof的介绍和模拟实现
2022-07-28 22:06:00 【XIN-XIANG荣】
博客主页: XIN-XIANG荣
系列专栏:【从0到1,C语言学习】
一句短话:你若盛开,蝴蝶自来!
博客说明:尽己所能,把每一篇博客写好,帮助自己熟悉所学知识,也希望自己的这些内容可以帮助到一些在学习路上的伙伴,文章中如果发现错误及不足之处,还望在评论区留言,我们一起交流进步!
前言
这里介绍atoi、offsetof以及它们的模拟实现,atoi这个库函数用来将一个字符串转化为一个数字;offsetof用来计算偏移量,长的像个函数,其实它是一个宏!
一. atoi库函数
1. 介绍
功能:
- 将一个数字字符串转化为其对应的整数
int atoi (const char * str);
头文件:
- <stdlib.h>
参数:
- str——指向要进行转化的字符串的指针
返回值:
- 成功时,函数将转换后的整数作为int值返回;
- 如果不能进行转换,返回0;
- 如果转换后的值超出int的可表示值范围,则行为未定义。
注意事项:
- atoi在使用时,如果要转化的字符串前面有空白字符,则会跳过这些空白字符,直到找到第一个非空白字符;
- 要被转化的字符串中开头可以带 + - 号这样的字符,会被识别为算数当中的 + - 号;
- 如果要转化的数字字符之间有非非数字字符,则非数字字符之后的数字字符无法被转化。
使用实例:

2. 模拟实现
atoi的模拟要尽可能模拟全面,要考虑到如下几点:
- 参数为空指针
- 参数为空字符串
- 字符串最开头空格
- 第一个数字字符前的 + - 号
- 转化后的值越界,超出int的范围
- 字符串中的非数字字符
#include<stdio.h>
#include<assert.h>
#include<limits.h>
#include<ctype.h>
enum Status
{
VALID,
INVALID
}sta = INVALID;//默认非法
int My_atoi(const char* str)
{
int flag = 1;
assert(str);
if (*str == '\0')
{
return 0;//返回的非法0
}
//跳过空白字符
while (isspace(*str))
{
str++;
}
//解决+-号
if (*str == '+')
{
flag = 1;
str++;
}
else if (*str == '-')
{
flag = -1;
str++;
}
long long ret = 0;
while (*str)
{
if (isdigit(*str))
{
ret = ret * 10 + flag * (*str - '0');
//判断越界
if (ret > INT_MAX || ret < INT_MIN)
{
return 0;
}
}
else
{
return (int)ret;
}
str++;
}
if (*str == '\0')
{
sta = VALID;
}
return (int)ret;
}
int main()
{
char arr[50] = "-1234";
int ret = My_atoi(arr);
if (sta == INVALID)
{
printf("非法返回:%d\n", ret);
}
else if(sta == VALID)
{
printf("合法转换:%d\n", ret);
}
return 0;
}
二. 宏offsetof
1. 介绍
功能:
- 计算结构成员相对于结构体首地址的偏移量
offsetof (type,member)
参数:
- type——结构或者联合类型
- member——类型中的成员
返回值:
- 返回成员相对于类型首地址的偏移量,一个size_t类型的值。
使用实例:

2. 模拟实现
去观察结构体的地址和结构成员的地址会发现,结构体成员的地址减去结构体的地址就是结构体成员相对于结构体首地址的偏移量;
假设0地址处为结构体的地址,那么结构体成员的地址就是其的相对于结构体首地址的偏移量
#include<stdio.h>
#define OFFSETOF(type, name) (int)&(((struct S*)0)->name)
struct S
{
int a;
char b;
double c;
};
int main()
{
printf("%d\n", OFFSETOF(struct S, a));
printf("%d\n", OFFSETOF(struct S, b));
printf("%d\n", OFFSETOF(struct S, c));
return 0;
}
各位小伙伴,看到这里就是缘分嘛,希望我的这些内容可以给你带来那么一丝丝帮助,可以的话三连支持一下呗!!! 感谢每一位走到这里的小伙伴,我们可以一起学习交流,一起进步!!!加油!!!

边栏推荐
- Jincang database kingbasees client programming interface guide ODBC (2. Overview)
- C language n*n matrix evaluation and inverse matrix [easy to understand]
- 新一代超安全蜂窝电池 思皓爱跑上市13.99万元起售
- [self] - question brushing - string
- npm更换最新淘宝镜像
- 深度剖析集成学习Xgboost(续)
- Deep analysis of integrated learning gbdt
- 深度之眼(十八)——偏导数
- 宝塔 phpmyadmin未授权访问漏洞
- 请简述list,set,map类型的集合的各自特点(简述三种不同的继承方式下)
猜你喜欢

多传感器融合定位(一)——3D激光里程计

Mongodb index add, view, export, delete

Pagoda phpMyAdmin unauthorized access vulnerability
![[self] - brush questions array](/img/a9/d12c41183df6961b2e9dd7cb49dfec.png)
[self] - brush questions array

RHCE first day

机器学习问题笔记

Codeforces Round #810 (Div. 2) A - C

2022 R2 mobile pressure vessel filling test question simulation test platform operation

Manufacturing steps of interactive slide screen in exhibition hall

搭载新一代超安全蜂窝电池,思皓爱跑上市13.99万元起售
随机推荐
Codeforces Round #474 (Div. 1 + Div. 2) - C, F
Multi sensor fusion positioning (II) -- map based positioning
通配符 SSL/TLS 证书
[self] - brush questions logic
Kingbasees client programming interface guide ODBC (4. Create data source)
利用递归和链表头插法实现链表成组的翻转——LeetCode25 K个一组翻转链表
Genomic DNA isolation Worthington ribonuclease A
有效供应链管理的八大绩效分析指标(上)
Codeforces Round #810 (Div. 2) A - C
Codeforces Round #810 (Div. 2) A - C
The classic dual stack implementation queue, pay attention to the modification of the judgment conditions of traversing the stack.
A new generation of ultra safe cellular battery, Sihao aipao, will be available from 139900 yuan
编译原理研究性学习专题 2——递归下降语法分析设计原理与实现
websocket心跳机制(保活机制)
[self] - question brushing - string
Multisensor fusion positioning (III) -- inertial technology
简述分组密码的加密分组链接模式的工作原理及其特点(密码学移位密码加密解密)
npm更换最新淘宝镜像
经典双栈实现队列,注意遍历栈的判定条件修改。
解决线程安全问题&&单例模式
