当前位置:网站首页>【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. !!!

边栏推荐
- Arm-A53资料「建议收藏」
- Please briefly describe the respective characteristics of list, set and map type sets (briefly describe three different inheritance methods)
- Jincang database kingbasees client Programming Interface Guide - ODBC feature support constraints
- PHP poster QR code synthesis
- fastdfs工作原理(技术原理)
- Solve thread safety problems & singleton mode
- Briefly introduce the working principle and characteristics of block cipher encryption block link mode (cryptography shift cipher encryption and decryption)
- ISO 13400(DoIP)标准解读
- Deep analysis of integrated learning gbdt
- Zabbix 5.0 使用自带Redis模版监控
猜你喜欢

Leetcode60. 排列序列

实时数仓:美团基于Flink的实时数仓建设实施

Worthington核糖核酸测定详细攻略

pycharm配置运行环境

Doip test development practice

EN 1873 assembly accessories for roofing - plastic single roof lamps - CE certification

With the help of rpa+lcap, the enterprise treasurer management can be upgraded digitally

Create AP hotspots for imx6 development board QT system based on rtl8723 cross compile iptables

Explanation of history and chemical properties of Worthington ribonuclease B

Worthington丨Worthington胰蛋白酶化学性质及相关研究
随机推荐
Uricase - Characteristics of uricase in Worthington pig liver:
Multisensor fusion positioning (III) -- inertial technology
Compatibility description between kingbasees and Oracle (4. SQL)
1-8 basic use of props
Blocking queue
1-7 解决类中方法的this指向问题
DevOps在物联网解决方案中的应用
Please briefly describe the respective characteristics of list, set and map type sets (briefly describe three different inheritance methods)
Classification and determination method of Worthington stemxyme
Pycharm configuring the running environment
Doip test development practice
【C】逆序字符串(俩种递归思路)
Leetcode62. 不同路径
PMP考试倒计时,速看3A通关锦囊!
Websocket heartbeat mechanism (keep alive mechanism)
Machine learning problem notes
pycharm配置运行环境
Zabbix 5.0 使用自带Redis模版监控
EN 1873 assembly accessories for roofing - plastic single roof lamps - CE certification
【C】喝汽水,找单身狗问题
