473,804 Members | 2,296 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is it possible sentence?

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-
Nov 14 '05 #1
14 1421

"Alex" <dr******@korea .com> wrote in message news:7a******** *************** ***@posting.goo gle.com...
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/
Nov 14 '05 #2

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

"Alex" <dr******@korea .com> wrote in message news:7a******** *************** ***@posting.goo gle.com...
Hi,

I have a question regarding to string variable.

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

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

Nov 14 '05 #3
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
Nov 14 '05 #4
dr******@korea. com (Alex) wrote:
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?


No. First, every occurrence of STR is literally replaced with "ABC"
by the C preprocessor. Later in translation "ABC", like every source
code string literal, is translated into an anonymous array object
(in C the term 'object' means 'region of data storage which can hold
values').

Regards
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #5
In <7a************ **************@ posting.google. com> dr******@korea. com (Alex) writes:
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.
Indeed, it is correct (after fixing an obvious typo).
But I don't know where "ABC" is exist.
It is typically stored in either the data segment or the text segment
of the program. If you can change it at runtime without crashing the
program, then it's probably in the data segment. If the program crashes,
it's in the text segment. Let's see:

fangorn:~/tmp 192> cat test.c
#define STR "ABC"

int main()
{
char *testStr;
testStr = STR;
*testStr = 0;
return *testStr;
}
fangorn:~/tmp 193> gcc test.c
fangorn:~/tmp 194> ./a.out
Segmentation fault

So, it was in the text segment on my system/compiler combo.
Some compilers allow the user to control this aspect:

fangorn:~/tmp 195> gcc -fwritable-strings test.c
fangorn:~/tmp 196> ./a.out
fangorn:~/tmp 197> echo $status
0
Does C compiler make a memory for STR?


The STR identifier exists only during the preprocessing phase. By the
time the proper compilation starts, it has already been replaced by the
"ABC" string literal, so the compiler doesn't even get to "see" it.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #6
"Dan Pop" <Da*****@cern.c h> wrote:
dr******@korea. com (Alex) writes:
#define STR "ABC"

char *testStr;


Note: the semicolon in the above line was added by Dan, and was not in
Alex's original post.

Dan, lest anyone accuse you of turning into another ERT, please strictly
uphold the rules of quoting. The most one should do to quoted text is to
snip out parts or to reflow the text for a consistent line length. It is not
acceptable to silently make additions, changes or improvements to someone's
code. At the least, the quote symbol on that line (>) should be removed to
indicate changes were made.

--
Simon.
Nov 14 '05 #7
On Tue, 22 Jun 2004 17:30:17 +0530, "Vijay Kumar R Zanvar"
<vi*****@global edgesoft.com> wrote in comp.lang.c:

"Alex" <dr******@korea .com> wrote in message news:7a******** *************** ***@posting.goo gle.com...
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,


The Standard makes no recommendations at all as to whether string
literals should be read-only or not. C++ defines string literals as
having type array of const char, but C does not. A C string literal
has the type of array of char, no const.

Modifying a string literal in C is undefined behavior because the
standard specifically states that it is, not because the characters
may or may not be read-only.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #8
On 22 Jun 2004 13:25:48 GMT, Da*****@cern.ch (Dan Pop) wrote in
comp.lang.c:
In <7a************ **************@ posting.google. com> dr******@korea. com (Alex) writes:
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.
Indeed, it is correct (after fixing an obvious typo).
But I don't know where "ABC" is exist.


It is typically stored in either the data segment or the text segment


Who says C programs have data segments and text segments? Chapter and
verse.
of the program. If you can change it at runtime without crashing the
program, then it's probably in the data segment. If the program crashes,
it's in the text segment. Let's see:


Let's not. A strictly conforming program can't tell what happens when
a string literal is changed, since it ceases to be strictly conforming
when it tries to perform the change.

In most of the environments where the code I get paid to write
executes, the undefined behavior of attempting to modify a string
literal is ... nothing at all. The EPROM or Flash device in which the
string literal is stored is totally unaffected by anything the
processor can do to it in normal execution.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #9
In <db************ *************** ***@news.terane ws.com> "Ralmin" <ne**@ralminNOS PAM.cc> writes:
"Dan Pop" <Da*****@cern.c h> wrote:
dr******@korea. com (Alex) writes:
> #define STR "ABC"
>
> char *testStr;

Note: the semicolon in the above line was added by Dan, and was not in
Alex's original post.


Why would anyone bother to note such an irrelevant detail? Does it make
any difference to the OP question or to my reply?
Dan, lest anyone accuse you of turning into another ERT, please strictly
uphold the rules of quoting. The most one should do to quoted text is to
snip out parts or to reflow the text for a consistent line length. It is not
acceptable to silently make additions, changes or improvements to someone's
code. At the least, the quote symbol on that line (>) should be removed to
indicate changes were made.
Did you read the rest of my post with your brain engaged? Here's the
relevant part:
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.


Indeed, it is correct (after fixing an obvious typo).
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
In your humble opinion, what was the purpose of the parenthetical
remark underlined above?

The intent of the OP was perfectly clear, despite the missing semicolon
in the original code. If I had any good reason to suspect that the OP
omitted it *by ignorance*, I would have pointed out the syntax error.
In the absence of any such reason, it was more constructive to fix the
code AND mention that I've fixed it, rather than waste time uselessly
pointing out the missing semicolon. I keep forgetting semicolons myself,
on a regular basis, because my brain is focused on more important things
when typing C code.

It is not the first time I'm correcting trivial typos in posted code
and expect a much harsher reply the next time you choose to make a
fuss out of it.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2921
by: Christian Buck | last post by:
Hi, i'm writing a regexp that matches complete sentences in a german text, and correctly ignores abbrevations. Here is a very simplified version of it, as soon as it works i could post the complete regexp if anyone is interested (acually 11 kb): (?:+|(?:\.|\d+\.|a\.?A \.)){3,}+(?!\s)
10
2100
by: Sidhu | last post by:
Hoe to relplace the word in sentence? Can any one send program?
6
1974
by: mike | last post by:
Hello, I am trying to write some code to parse a sentence and hyperlink just the words in it. I used Aaron's code from an earlier question as a start. So far, all the code does below is hyperlink everything separated by a space, which means stuff like "work." "happy." "Well;" "not." from the sentence become hyperlinks (whereas im interested in just the words themselves becoming hyperlinks and the punctuation staying nonhyperlinks). ...
8
2494
by: Scott | last post by:
I would like to automatically change the first letter to upper case and keep the rest intact of each sentence on a control of a form, i.e., i am going to school. see you in the afternoon. -I am going to school. See you in the afternoon. Is there any functions to do this job? Thanks, Scott
3
5615
by: dalearyous | last post by:
ok basically i need to write a program that will replace normal words in a sentence with pirate words. the trick is it needs to be able to take two word phrases. i went about this two different ways: 2d array and hashmap. both have the problem of translating a phrase with two words. im running out of time and need help bad. HASHMAP WAY: import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.awt.*; import...
4
6563
by: wohast | last post by:
Hi, I'm stuck at the very beginning of my current assignment. I just need a little help getting started, and then I know how to do everything else. ask user: "Please enter your name followed by a positive integer between 1 and 9999 enclosed in parentheses, such as Cupid(1442)." how do i use substring to take out just the name that they enter, so that it starts at the first letter they enter, and ends at the (? also how do i use the...
12
4006
by: jackson.rayne | last post by:
Hello, I am a javascript newbie and I'm stick at one place. I have a requirement where I will get a sentence in a variable example var v1 ="This is a sentence"
3
5041
by: dmalhotr2001 | last post by:
Hi, For string extraction function in vb, if I feed in a paragraph, how do I extract the first sentence of that paragraph. Thanks :D
19
66230
by: fellya | last post by:
Hi, i don't have enough experience in writing codes in Python but now i'm trying to see how i can start using Python. I've tried to write a simple program that can display a sentence. now my problem is how to write a code using split function to split that sentence into words then print out each word separately. let me give u an example: >>>sentence=" My question is to know how to write a code in Python" then the output of this...
1
4357
by: fellya | last post by:
Hi, i don't have enough experience in writing codes in Python but now i'm trying to see how i can start using Python. I've tried to write a simple program that can display a sentence. now my problem is how to write a code using split function to split that sentence into words then print out each word separately. let me give u an example: >>>sentence=" My question is to know how to write a code in Python" then the output of this...
0
9594
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10599
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10346
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10090
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7635
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6863
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5531
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.