c - 将指针(字符串)传递给 C 函数

标签 c

请读到最后再说:“哦不,又是这个问题……”

我现在正在参加 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, ...)什么时候stringchar*总是错的。不要取消引用该指针。

如果你坚持使用“宽”字符(即 questionable ),你将不得不改变一些东西:

  • 而不是使用 char , 你需要包括 <wchar.h>并使用 wchar_t反而。据我所知,uint16_tuint8_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/

相关文章:

python - 将数据从 C 发送到 python

Python 函数胶囊

c - 在c99中初始化 boolean 数组的方法

c++ - 在多大程度上允许将 C 和 C++ 结合使用?

c# - 为什么 `j=++i+++i;` 在 C# 和 C 中的输出不同?

c - 如何获取每行中的字符数?

c++ - C : advantages and disadvantages 中的另一种字符串实现

涉及 {} 的 C 代码

c - 使用相似代码时出现奇怪的编译器投诉

c++ - 退出全屏模式后清除屏幕?