相关疑难解决方法(0)

C11 _Generic:如何处理字符串文字?

使用_GenericC11中的功能,您如何处理字符串文字?

例如:

#include <stdio.h>
#define foo(x) _Generic((x), char *: puts(x))

int main()
{
    foo("Hello, world!");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在clang上给出了这个错误:

controlling expression type 'char [14]' not compatible with any generic association type
Run Code Online (Sandbox Code Playgroud)

更换char *char[]给我

error: type 'char []' in generic association incomplete
Run Code Online (Sandbox Code Playgroud)

获得这个编译的唯一方法(据我所知)是:

  1. 将字符串文字转换为适当的类型.这是丑陋的(在我看来)首先打破了这一点_Generic.
  2. 使用char[14]的类型说明符.你一定在开玩笑吧......

我的假设是数组在传递时会衰减到指针_Generic,但显然不是.那么,如何我用_Generic用字符串文字?那是唯一的两种选择吗?

我在Debian上使用clang 3.2.不幸的是,它是我访问过的唯一支持此功能的编译器,所以我不知道它是否是编译器错误.

c clang c11

25
推荐指数
3
解决办法
2611
查看次数

C中的通用二叉搜索树

我已经实现了二叉搜索树,但我也想让它变得通用.代码如下:

typedef struct treeNode {
  int data;
  struct treeNode *left;
  struct treeNode *right;
} treeNode;
Run Code Online (Sandbox Code Playgroud)

和功能:

treeNode* FindMin(treeNode *node) {
  if(node==NULL) {
    /* There is no element in the tree */
    return NULL;
  }
  if(node->left) /* Go to the left sub tree to find the min element */
    return FindMin(node->left);
  else 
    return node;
}

treeNode * Insert(treeNode *node,int data) {
  if(node==NULL) {
    treeNode *temp;
    temp = (treeNode *)malloc(sizeof(treeNode));
    temp -> data = data;
    temp -> left = temp -> right …
Run Code Online (Sandbox Code Playgroud)

c generics binary-tree function-pointers binary-search-tree

3
推荐指数
1
解决办法
6631
查看次数

在C中使用模板功能的最短例子?

怎样运用一个功能echo_tpl,可以采取1个参数类型intstring,并打印出来?

c syntax

2
推荐指数
1
解决办法
184
查看次数