这句话有可能吗? [英] Is it possible sentence?

查看:64
本文介绍了这句话有可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我对字符串变量有疑问。


请看下面的例子。

------------------------------

#define STR" ABC"


char * testStr

testStr = STR;

------------------ ------------


我认为上面的例子没有问题。

但我不知道在哪里 ABC"是否存在。

C编译器是否为STR创建内存?

-Alex-

解决方案



" Alex" <博士****** @ korea.com>在消息新闻中写道:7a ************************** @ posting.google.c om ...



我对字符串变量有疑问。

请看下面的例子。
------------- -----------------
#define STR" ABC"
char * testStr
testStr = STR;


如果它出现在函数内部,这是完全正确的。 C中的声明

必须始终在复合块内。

---------------------- --------

我认为上面的例子没有问题。
但我不知道ABC在哪里是否存在。
C编译器是否为STR创建内存?


一般来说,您的问题是关于字符串文字的存储。答案

在以下情况下有所不同:


*标准建议字符串文字是只读的;因此,

允许共享具有相同文本的字符串副本。在这个

的情况下,实现可以将sting存储在代码部分,

或任何其他只读部分。这种方法有助于执行一些

优化。


*许多实现提供的扩展通常是非便携式的b $ b。一个这样的扩展是可写字符串文字。然后,

字符串存储在程序的静态区域中。因此,

将是相同文本的字符串文字的不同副本。


许多编译器提供控制字符串行为的选项

文字。例如,gcc提供一个编译器开关-fwritable-strings,以便在可写数据部分中存储字符串
。默认情况下,GCC合并重复的

字符串,而Borland的Turbo C / C ++则不然。它提供了以下

开关:


-d合并重复字符串

-d-合并重复字符串关闭


许多编译器提供pragma来控制,但这些都是不可移植的。

标准(C99)只指定了几个可移植的pragma,并且不在

范围在这里。

-Alex -




-

Vijay Kumar R Zanvar

我的主页 - http://www.geocities.com/ vijoeyz /




" Vijay Kumar R Zanvar" < 6 ***** @ globaledgesoft.com>在消息新闻中写道:2j ************* @ uni-berlin.de ...


" Alex" <博士****** @ korea.com>在消息新闻中写道:7a ************************** @ posting.google.c om ...



我对字符串变量有疑问。

请看下面的例子。
------------- -----------------
#define STR" ABC"
char * testStr


啊......丢失分号。

testStr = STR;



Alex写道:
< blockquote class =post_quotes>我对字符串变量有疑问。

请看下面的例子。
---------------- --------------
#define STR" ABC"
char * testStr


(缺失;)

testStr = STR;


(在顶层非法;假设我们改写为


chat * testStr = STR;



------------------------------

我认为上面的例子有没问题。
但我不知道ABC在哪里是存在的。


某处。在某个地方它将对整个程序运行有效。
整个程序运行。你不需要知道在哪里。

C编译器是否为STR创建内存?




是的,但不相关; STR是一个预处理器符号,在运行时不可见

(并且不需要,通常也不在编译对象代码中)。


-

Chris" electric hedgehog" Dollin

C常见问题解答: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html

C欢迎: http://www.angelfire.com/ms3/bchambl...me_to_clc.html


Hi,

I have a question regarding to string variable.

Please look at below example.
------------------------------
#define STR "ABC"

char *testStr
testStr = STR;
------------------------------

I think above example have no problem.
But I don''t know where "ABC" is exist.
Does C compiler make a memory for STR?

-Alex-

解决方案


"Alex" <dr******@korea.com> wrote in message news:7a**************************@posting.google.c om...

Hi,

I have a question regarding to string variable.

Please look at below example.
------------------------------
#define STR "ABC"

char *testStr
testStr = STR;
This is perfectly correct if it appears inside a function. A statement
in C must always be within a compound block.
------------------------------

I think above example have no problem.
But I don''t know where "ABC" is exist.
Does C compiler make a memory for STR?

Your question, in general, is about storage of string literals. The answer
varies under the following situations:

* The Standard recommends the string literal to be a read-only; thus,
allows sharing copies of string with identical text. In this
case, the implementation may store the sting in the code section,
or any other read-only section. This method helps perform some
optimizations.

* Many implementation provide extensions which are, generally, non
portable. One such extension is writable string literals. The
string is, then, stored in the program''s static region. Thus, there
would be different copies of string literal of identical text.

Many compilers provide option to control the behaviour of character string
literals. For example, gcc provide a compiler switch, -fwritable-strings, to
store strings in writable data section. By default, GCC merges duplicate
strings, whereas, Borland''s Turbo C/C++ doesn''t. It provides the following
switches:

-d merge duplicate string on
-d- merge duplicate string off

Many compilers provide pragmas to control, but these are non portable.
The Standard(C99) specifies only few pragmas that are portable, and is out of
scope here.
-Alex-



--
Vijay Kumar R Zanvar
My Home Page - http://www.geocities.com/vijoeyz/



"Vijay Kumar R Zanvar" <vi*****@globaledgesoft.com> wrote in message news:2j*************@uni-berlin.de...


"Alex" <dr******@korea.com> wrote in message news:7a**************************@posting.google.c om...

Hi,

I have a question regarding to string variable.

Please look at below example.
------------------------------
#define STR "ABC"

char *testStr
Ah..missing semicolon.
testStr = STR;



Alex wrote:

I have a question regarding to string variable.

Please look at below example.
------------------------------
#define STR "ABC"

char *testStr
(missing ;)
testStr = STR;
(illegal at top level; assume we rewrite to

chat *testStr = STR;
)
------------------------------

I think above example have no problem.
But I don''t know where "ABC" is exist.
Somewhere. Somewhere where it will be valid for the
entire program run. You don''t need to know where.
Does C compiler make a memory for STR?



Yes, but irrelevant; STR is a preprocessor symbol and is not visible
at run-time (and need not and usually is not in compiled object code).

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html


这篇关于这句话有可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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