繁体   English   中英

有人可以解释一下这段代码吗? 它是什么意思(无符号字符)c? 和 ft_strlen((char *)s)?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM