如何从一个字符串初始化字符数组 [英] How to Initialize char array from a string

查看:332
本文介绍了如何从一个字符串初始化字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做到以下几点

 的char a [] = {'A​​','B','C','D'};

但我不想分开来写这些字符。我想是这样

 的#define SABCD烧焦一个[​​] = {S [0],S [1],S [2],S [3]};

但是,这不会编译(GCC说'初始元素不是常量')

我试着用替换的#define行

 为const char S [] =ABCD;

但是,这似乎并没有帮助。

我怎样才能做到这一点(或者类似的东西),让我写出了ABCD作为一个正常的字符串,而不是作为四个单独的字符呢?

P.S。人们似乎不正确读取的问题...

我不能让下面的code编译:

 为const char S [] =ABCD;
烧焦T [] = {S [0],S [1],S [2],S [3]};
烧焦U [] = {S [3],S [2],S [1],S [0]};


解决方案

您不能 - 在全局和局部静态变量的初始化Ç在C的设计使得编译器可以把静态值到可执行文件。它不能处理非恒定的前pressions的初始化。只有在C99中,可以使用非恒定的前pression合计初始化 - 不是这样的C89!

在你的情况,因为你的阵列是包含字符数组,每个元素必须是一个算术常量前pression。看看它说什么那些


  

算术常量前pression应有算术类型,并应只
  操作数是整型常量,浮动常量,枚举常量,字符
  常量和sizeof前pressions。


当然,这不是你的初始化,它采用指针类型的操作数满足。当然,另一种是使用字符串文本初始化您的数组,因为它解释太


  

在为具有静态存储期限为对象的初始化所有前pressions不断前pressions或字符串。


股价取出C99 TC3委员会草案。因此得出结论,你想做的事 - 使用非恒定的前pression - 无法与C.做您有几种选择:


  • 写你的东西多次 - 一次逆转,其他时间不予转回。

  • 更改语言 - C ++能做到所有

  • 如果您的真正的想要做的东西,使用字符常量数组* 而不是

下面是我的最后一个选项的意思

 字符常量C [] =ABCD;
字符常量* F [] = {和C [0],和C [1],和C [2],和C [3]};
字符常量* G [] = {和C [3],和C [2],和C [1],和C [0]};

这工作得很好,作为一个地址常量前pression用来初始化指针


  

这是地址常量是空指针,一个指向一个左指定静态存储持续时间,或指向一个函数标志的对象;应当明确使用一元和放大器上创建的;操作员或整型常量强制转换为指针类型,或隐式通过使用数组或函数类型的前pression的。该数组下标[]和成员访问。和 - >运营商,地址和放大器;和间接*一元运算符和指针的类型转换可能会在建立一个地址常量来使用,但一个对象的值不得使用这些操作符来访问。


您可能有运气调整您的编译器选项 - 另一句名言:


  

这是实现可能接受其它形式不断前pressions的。


I want to do the following

char a[] = { 'A', 'B', 'C', 'D'};

But I do not want to write these characters separately. I want something like

#define S "ABCD"

char a[] = { S[0], S[1], S[2], S[3] };

But this won't compile (gcc says 'initializer element is not constant').

I tried replacing the #define line with

const char S[] = "ABCD";

But that doesn't seem to help.

How can I do this (or something similar) that lets me write the "ABCD" as a normal 'string', and not as four separate characters?

P.S. It seems that people do not read the question correctly...

I can't get the following code to compile:

const char S[] = "ABCD";
char t[] = { S[0], S[1], S[2], S[3] };
char u[] = { S[3], S[2], S[1], S[0] };

解决方案

You can't - in C. In C initializing of global and local static variables are designed such that the compiler can put the values statically into the executable. It can't handle non-constant expressions as initializers. And only in C99, you can use non-constant expression in aggregate initializers - not so in C89!

In your case, since your array is an array containing characters, each element has to be an arithmetic constant expression. Look what it says about those

An arithmetic constant expression shall have arithmetic type and shall only have operands that are integer constants, floating constants, enumeration constants, character constants, and sizeof expressions.

Surely this is not satisfied by your initializer, which uses an operand of pointer type. Surely, the other way is to initialize your array using a string literal, as it explains too

All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.

All quotes are taken out of the C99 TC3 committee draft. So to conclude, what you want to do - using non-constant expression - can't be done with C. You have several options:

  • Write your stuff multiple times - one time reversed, and the other time not reversed.
  • Change the language - C++ can do that all.
  • If you really want to do that stuff, use an array of char const* instead

Here is what i mean by the last option

char const c[] = "ABCD";
char const *f[] = { &c[0], &c[1], &c[2], &c[3] };
char const *g[] = { &c[3], &c[2], &c[1], &c[0] };

That works fine, as an address constant expression is used to initialize the pointers

An address constant is a null pointer, a pointer to an lvalue designating an object of static storage duration, or a pointer to a function designator; it shall be created explicitly using the unary & operator or an integer constant cast to pointer type, or implicitly by the use of an expression of array or function type. The array-subscript [] and member-access . and -> operators, the address & and indirection * unary operators, and pointer casts may be used in the creation of an address constant, but the value of an object shall not be accessed by use of these operators.

You may have luck tweaking your compiler options - another quote:

An implementation may accept other forms of constant expressions.

这篇关于如何从一个字符串初始化字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆