[英]Can someone explain me this code? What does it mean (unsigned char)c? and ft_strlen((char *)s)?
在声明一些变量时,括号是什么意思。 带括号的 * 和 & 符号在这里也会造成混淆。 请解释这段代码。
#include "libft.h"
char *ft_strrchr(const char *s, int c)
{
int len;
char ch;
ch = (unsigned char)c;
len = ft_strlen((char *)s);
if (ch == '\0')
return ((char *)(&s[len]));
while (--len >= 0)
{
if (s[len] == ch)
return ((char *)(&s[len]));
}
return (NULL);
}
括号(type)variable
将变量转换为不同的类型。
&
就在变量获取该变量的 memory 地址之前。
因此,例如:
int x = 1;
char *ptr = (char *)&x; // gets the address and because it is int *, we cast it to char *
/* the above is significant, because usually int is 4 bytes and char is 1 byte, so
* incrementing a pointer, we can go to the next byte's address if we are using a 1 byte type
* or, it jumps 4 bytes if we are usinig int pointer. This is a reason why this is sometimes used
* on memcpy, so that memcpy can copy all bytes to the destination
*/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.