请读到最后再说:“哦不,又是这个问题……”
我现在正在参加 C 类(class),课本中提供了以下示例:
#include <stdio.h>
#include <stdint.h>
void Terminal_PrintData(uint16_t * const Data);
int main(void){
uint16_t StringData[] = "MyData";
Terminal_PrintData(StringData);
}
void Terminal_PrintData(uint16_t * const Data)
{
printf("Value: %s", *Data);
}
当我编译这个杰作时,这就是我得到的:
F:\AVR Microcontroller>gcc -o test test.c
test.c: In function 'main':
test.c:7:26: error: wide character array initialized from non-wide string
uint16_t StringData[] = "MyData";
我的问题是:
- 用
uint16_t
声明一个字符串是否正确? - 将字符串传递给函数的推荐方法是什么?
最佳答案
您的直接问题:
- Is it correct to declare a string with uint16_t?
没有。
所有字符串总是char[]
.还有 wide strings(宽字符的字符串),类型为 wchar_t[]
, 并写有 L
前缀(例如 L"hello"
)。
- What is recommended way to pass a string to a function?
作为指向字符串中第一个字符的指针,所以 char *
或 wchar_t *
. const
限定符在这里没有意义;这意味着指针(不是字符串本身)是常量(参见 here )。我建议写 wchar_t const *
或 const wchar_t *
反而;它具有不同的含义(即字符串不能更改),但它是正确且有意义的。
您提到此代码来自类(class)书籍。我不确定你的意思是这些是你的教授分发的讲义,还是你买了一本出版的书。无论哪种情况,如果此片段代表了书籍/笔记的质量,获得退款。
现在,让我们让这段代码正常工作。
这段代码中有一个错误会导致程序崩溃:
-
printf("... %s ...", ..., *string, ...)
什么时候string
是char*
总是错的。不要取消引用该指针。
如果你坚持使用“宽”字符(即 questionable ),你将不得不改变一些东西:
- 而不是使用
char
, 你需要包括<wchar.h>
并使用wchar_t
反而。据我所知,uint16_t
和uint8_t
除非你在任何地方都使用显式转换,否则不会工作。那是因为char
没有 8 位,但是CHAR_BIT
bits . - 宽字 rune 字必须以
L
开头前缀。 - 而不是使用
printf
, 你需要使用wprintf
. - 在格式字符串中,使用
%ls
而不是%s
. ( Unless you use Microsoft's compiler .)
最后,一些不太严重的错误:
- 如前所述,
T * const
争论是无用的。作者的意思大概是T const *
(或等同于const T *
)。 - 您可以删除
<stdint.h>
和<stdio.h>
.我们不再使用uint16_t
, 和<wchar.h>
声明一些宽字符<stdio.h>
- 类似功能。
我最终得到以下代码:
#include <wchar.h>
void Terminal_PrintData(wchar_t const * Data);
int main(void){
wchar_t StringData[] = L"MyData";
Terminal_PrintData(StringData);
}
void Terminal_PrintData(wchar_t const * Data)
{
wprintf(L"Value: %ls", Data);
}
这可以在 Linux x86-64 上使用 GCC 和 Clang 按预期编译和运行。
关于c - 将指针(字符串)传递给 C 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33649743/