为函数内的指针赋值

发布于 2024-12-17 15:26:30 字数 1365 浏览 1 评论 0原文

好的,我有一个问题,我猜是关于 C 字符串的。下面是我对一些代码的修改(在之前的 stackoverflow 问题的答案中给出!),以及函数调用和输出。该函数将输入的十六进制数(长度为 8)转换为二进制数(长度为 32)。

void htoi(const char *ptr, char *binAddr) {
char value[32] = "";
char ch = *ptr;
int i;
const char* quads[] = {"0000", "0001", "0010", "0011", "0100", "0101",
                     "0110", "0111", "1000", "1001", "1010", "1011",
                     "1100", "1101", "1110", "1111"};

while (ch == ' ' || ch == '\t')
    ch = *(++ptr);

for (i = 0; i < 8; i++) {
    if (ch >= '0' && ch <= '9')
        strncat(value, quads[ch - '0'], 4);
    if (ch >= 'A' && ch <= 'F')
        strncat(value, quads[10 + ch - 'A'], 4);
    if (ch >= 'a' && ch <= 'f')
        strncat(value, quads[10 + ch - 'a'], 4);

    ch = *(++ptr);
    printf("%s\n", value);
}

*binAddr = *value;
}

这是我的函数调用:

char line[11], hexAddr[8], binAddr[32];
htoi(hexAddr, binAddr);
printf("%s\n", binAddr);

这是输出(当输入 001133c0 时):

0000

00000000

000000000001

0000000000010001

00000000000100010011

000000000001000100110011

0000000000010001001100111100

00000000000100010011001111000000

0���

最后一行(带有特殊字符)是上面 main 函数中的 printf(binAddr)。从函数内部的 printf 语句可以清楚地看出,二进制代码的构造是正确的。

我做错了什么?

Okay, so I have a question, I guess about C strings. Below is my modification to some code (given in an answer to a previous stackoverflow question!), with the function call and the output. The function converts an input hex number (of length 8) to a binary number (of length 32).

void htoi(const char *ptr, char *binAddr) {
char value[32] = "";
char ch = *ptr;
int i;
const char* quads[] = {"0000", "0001", "0010", "0011", "0100", "0101",
                     "0110", "0111", "1000", "1001", "1010", "1011",
                     "1100", "1101", "1110", "1111"};

while (ch == ' ' || ch == '\t')
    ch = *(++ptr);

for (i = 0; i < 8; i++) {
    if (ch >= '0' && ch <= '9')
        strncat(value, quads[ch - '0'], 4);
    if (ch >= 'A' && ch <= 'F')
        strncat(value, quads[10 + ch - 'A'], 4);
    if (ch >= 'a' && ch <= 'f')
        strncat(value, quads[10 + ch - 'a'], 4);

    ch = *(++ptr);
    printf("%s\n", value);
}

*binAddr = *value;
}

Here is my function call:

char line[11], hexAddr[8], binAddr[32];
htoi(hexAddr, binAddr);
printf("%s\n", binAddr);

Here is the output (when input with 001133c0):

0000

00000000

000000000001

0000000000010001

00000000000100010011

000000000001000100110011

0000000000010001001100111100

00000000000100010011001111000000

0���

The last line (with the special characters) is the printf(binAddr) in the main function above. It is clear from the printf statements inside the function that the binary code is being constructed correctly.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

╰ゝ天使的微笑 2024-12-24 15:26:31
*binAddr = *value;

这部分功能没有任何意义。 *binAddr 被传递到函数中,一旦函数结束就会超出范围。您需要将要保存在 *binAddr 中的值保存在全局变量或其他不会超出范围的变量中。

事实上,您确实有一个全局 binAddr,这也使问题变得混乱,因为它被本地 binAddr 隐藏了。

*binAddr = *value;

This part of the function does not make any sense. *binAddr is passed into the function and will be out of scope once the function ends. You need to save the value you meant to save in *binAddr in a global variable or something else that won't go out of scope.

The fact that you do have a global binAddr, is also confusing the issue since it is being hidden by the local binAddr.

丑丑阿 2024-12-24 15:26:30

这一行:

*binAddr = *value;

你认为它有什么作用?在应用星号之前,这两个参数都引用字符数组或指向 char 的指针。因此,当取消引用时,它们各自引用一个字符。因此,此语句将 binAddr 中的第一个字符指定为 value 中的第一个字符,而您可能希望返回整个字符串。

This line:

*binAddr = *value;

What do you think it does? Both arguments, before the asterisks are applied, refer to character arrays, or pointers to char. So when dereferenced, they refer to one char each. So this statement assigns the first char in binAddr equal to the first char in value, whereas presumably you wanted to return the whole string.

回心转意 2024-12-24 15:26:30

您不会为终止值数组的 null 留出空间(或插入),该值数组的长度至少需要 33 个字符。

You're not leaving room for (or inserting) the null that terminates the value array, which needs to be at least 33 chars long.

我不是你的备胎 2024-12-24 15:26:30

您正在构造一个包含字符“0”和“1”的字符串。您需要创建的是一系列字节。您需要采用 unsigned int 或等效的 64 位类型并根据输入设置位。

you are constructing a character string with characters '0' and '1'. what you need to create is a sequence of BYTES. you will need to take an unsigned int or a an equivalent 64-bit type and set the bits according to the input.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文