小编K.H*_*A.S的帖子

不能使结构的字段在C中保持不变

当我声明下面给出的结构时,它会抛出编译错误.

typedef struct{
    const char x; //it is throwing compilation error
    const char y;
}A;

void main()
{
    A *a1;
    a1 = (A*)malloc(2);
}
Run Code Online (Sandbox Code Playgroud)

如何使结构的字段(字符xy)保持不变?

c structure constants

5
推荐指数
1
解决办法
436
查看次数

在下面的C代码中获取分段错误?

为什么我在下面给出的代码中遇到分段错误?

char getstr()
{
  static char s[]="Hiii2015";
  return s;
}

int main()
{
  printf("%s",getstr());
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

虽然我知道我正在使用char返回类型返回一个地址,并且返回类型必须char*但仍然如果我这样做只有那时我在gcc编译器上获取null并且在另一个编译器上出现分段错误那么这是什么原因

c string segmentation-fault

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

标签 统计

c ×2

constants ×1

segmentation-fault ×1

string ×1

structure ×1