当前位置:网站首页>【C】 Introduction and Simulation Implementation of ATOI and offsetof
【C】 Introduction and Simulation Implementation of ATOI and offsetof
2022-07-29 00:00:00 【Xin-Xiang Rong】
Blog home page : XIN-XIANG Rong
Series column :【 from 0 To 1,C Language learning 】
A short sentence : If you are in full bloom , Butterflies come !
Blog description : Do what you can , Write every blog , Help yourself familiarize yourself with what you have learned , I also hope these contents can help some partners on the way of learning , If errors and deficiencies are found in the article , Also hope to leave a message in the comment area , We communicate progress together !
List of articles
Preface
Here are atoi、offsetof And their simulation implementation ,atoi This library function is used to convert a string into a number ;offsetof Used to calculate the offset , It looks like a function , In fact, it is a macro !
One . atoi Library function
1. Introduce
function :
- Convert a numeric string to its corresponding integer
int atoi (const char * str);
The header file :
- <stdlib.h>
Parameters :
- str—— Pointer to the string to be converted
Return value :
- success , The function takes the converted integer as int Value returns ;
- If conversion is not possible , return 0;
- If the converted value exceeds int Range of representable values of , Then act Undefined .
matters needing attention :
- atoi When use , If the string to be converted is preceded by a blank character , Will skip these white space characters , Until you find the first non white space character ;
- The string to be converted can start with + - Characters such as number , Will be recognized as one of the arithmetic + - Number ;
- If there are non numeric characters between the numeric characters to be converted , Then the numeric characters after non numeric characters cannot be converted .
Using examples :

2. Simulation Implementation
atoi The simulation of should be as comprehensive as possible , Consider the following points :
- The parameter is a null pointer
- The parameter is an empty string
- The space at the beginning of the string
- Before the first numeric character + - Number
- The converted value is out of bounds , beyond int The scope of the
- Non numeric characters in string
#include<stdio.h>
#include<assert.h>
#include<limits.h>
#include<ctype.h>
enum Status
{
VALID,
INVALID
}sta = INVALID;// Illegal default
int My_atoi(const char* str)
{
int flag = 1;
assert(str);
if (*str == '\0')
{
return 0;// Illegal return 0
}
// Skip white space
while (isspace(*str))
{
str++;
}
// solve +- Number
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');
// Judge out of bounds
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(" Illegal return :%d\n", ret);
}
else if(sta == VALID)
{
printf(" Legal conversion :%d\n", ret);
}
return 0;
}
Two . macro offsetof
1. Introduce
function :
- Calculate the offset of the structure member relative to the first address of the structure
offsetof (type,member)
Parameters :
- type—— Structure or union type
- member—— Members in types
Return value :
- Returns the offset of the member relative to the first address of the type , One size_t Type value .
Using examples :

2. Simulation Implementation
If you look at the address of the structure and the address of the structure member, you will find , The address of the structure member minus the address of the structure is the offset of the structure member from the first address of the structure ;
hypothesis 0 The address is the address of the structure , Then the address of the structure member is its offset from the first address of the structure
#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;
}
Dear friends , It's fate to see here , I hope my content can bring you a little help , If you can, support it for three times !!! Thank you for coming here , We can learn and communicate together , Progress together !!! come on. !!!

边栏推荐
- 失败率高达80%,数字化转型如何正确完成战略规划?
- laptop外接显示器
- Leetcode61. 旋转链表
- DoIP测试开发实践
- Hutool official website (is hutool easy to use)
- Best practices for migration of kingbasees v8.3 to v8.6 of Jincang database (3. Kingbasees migration capability support system)
- EN 12101-8:2011 smoke dampers for smoke and heat control systems - CE certification
- Machine learning problem notes
- 实时数仓:网易严选基于Flink的实时数仓实践
- EN 12101-8:2011烟雾和热量控制系统防烟挡板—CE认证
猜你喜欢

失败率高达80%,数字化转型如何正确完成战略规划?

Pycharm configuring the running environment

Leetcode61. 旋转链表

Powercli VMware vCenter deploys conventional new VMS in batch through self built PXE server with one click

Deep analysis of integrated learning AdaBoost

Jincang database kingbasees client programming interface guide ODBC (2. Overview)

多传感器融合定位(二)——基于地图的定位

GhostNets on Heterogeneous Devices via Cheap Operations

After SAP Oracle replicates a new instance, the remote connection of the database reports an error ora-01031

实时数仓:网易严选基于Flink的实时数仓实践
随机推荐
【TA-霜狼_may-《百人计划》】美术2.2 模型基础
Oracle create tablespaces and users
SAP oracle 复制新实例后数据库远程连接报错 ora-01031
PowerCLi 批量添加esxi到vCenter
ZABBIX 5.0 uses its own redis template for monitoring
基因组 DNA 分离丨Worthington核糖核酸酶A
Best practices for migration of kingbasees v8.3 to v8.6 of Jincang database (2. Compatibility of kingbasees v8.3 and v8.6)
多传感器融合定位(三)——惯性技术
Deep analysis of integrated learning xgboost
Compose 的声明式代码如此简洁?
连接池-归还连接详解(下)
请简述list,set,map类型的集合的各自特点(简述三种不同的继承方式下)
Is the declarative code of compose so concise?
Leetcode61. 旋转链表
失败率高达80%,数字化转型如何正确完成战略规划?
通配符 SSL/TLS 证书
EN 1935建筑五金.单轴铰链—CE认证
以JSP为视图解析器搭建SSM项目
CANoe应用案例之DoIP通信
Linux之yum安装MySQL
