簡體   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