473,322 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Strings in C

Hi,

For a declaration such as:

char * mystring = "ABCDabcd123";

Is it a linker issue where such strings are stored in C, or is it
defined as part of the language definition?

Is there any difference between an array of strings, e.g.

char mystring[10];

and strings of type char *, in terms of where they're stored? If these
are compiler dependent, is there at least a general storage convention?

Thanks,
Bahadir

Dec 8 '05 #1
9 1743
Bi*************@gmail.com wrote:
Hi,

For a declaration such as:

char * mystring = "ABCDabcd123";

Is it a linker issue where such strings are stored in C, or is it
defined as part of the language definition?
It is defined as "static storage duration", which means it is available
from program startup to program shutdown. The actual location in memory
is not specified.
Is there any difference between an array of strings, e.g.

char mystring[10];
That's not an array of strings. It's an array of char, which may be used
to hold a string. In fact, it could hold anywhere from zero to ten
strings. For example,
char mystring[10] = {'m', 'y', 0, 's', 't', 'r', 'i', 'n', 'g', 0};
contains two strings: "my" at offset zero, and "string" at offset 3.

Its storage depends on where it is defined. If that definition occurs
outside of any function, then it has static storage duration (available
at all times) and external linkage (the symbol is visible from other
translation units).

However, if that definition occurs inside a function, then it has
automatic storage duration (only exists within the block it is defined
in), and internal linkage (the symbol is not visible from other
translation units).
and strings of type char *, in terms of where they're stored?
Any string can be pointed to by a 'char *'. The pointer type makes no
difference to the storage of the string.

There are three storage types defined in C:
static
automatic
allocated (ie. malloc, calloc, realloc)

String literals always have static storage, and last until the end of
the program. Objects defined outside of any function, or with the
'static' keyword, have static storage, and last until the end of the
program.

Objects defined within a function body, without the 'static' keyword,
have automatic storage, and last until the end of the block.

A memory block allocated by malloc, calloc or realloc lasts until the
base address is passed to free or realloc.
If these
are compiler dependent, is there at least a general storage convention?


Some platforms make additional constraints on memory layout, such as
dividing memory into "segments". That is not specified as part of the C
language. Ask in a group devoted to your particular platform or family
of platforms (for example comp.unix.programmer).

--
Simon.
Dec 8 '05 #2
Bi*************@gmail.com wrote:

Hi,

For a declaration such as:

char * mystring = "ABCDabcd123";

Is it a linker issue where such strings are stored in C, or is it
defined as part of the language definition?

Is there any difference between an array of strings, e.g.

char mystring[10];

and strings of type char *, in terms of where they're stored? If these
are compiler dependent,
is there at least a general storage convention?


Storage in C, is characterized by duration.
There are 3 kinds:
1 automatic
2 static
3 allocated

When a string literal converts to a pointer,
it points to the first element of an array with static duration.
Arrays defined outside of any function have static duration.
Arrays defined with the static keyword, have static duration.
Arrays and other variables defined inside of function definitions
without the static keyword, have automatic duration.
malloc and friends return pointers to objects
with allocated duration.
the static keyword

Automatic duration lasts within the block where
the object is defined.
static duration lasts from before program startup,
until the end of the program.
Allocated duration lasts until the pointer is freed
or the program ends, whichever is first.

--
pete
Dec 8 '05 #3
Simon Biber wrote:
Bi*************@gmail.com wrote:
char mystring[10];


That's not an array of strings. It's an array of char, which may be used
to hold a string. In fact, it could hold anywhere from zero to ten
strings. For example,
char mystring[10] = {'m', 'y', 0, 's', 't', 'r', 'i', 'n', 'g', 0};
contains two strings: "my" at offset zero, and "string" at offset 3.


You could also say that the array contains nine different strings:
"my" at offset 0
"y" at offset 1
"" at offsets 2 and 9
"string" at offset 3
"tring" at offset 4
"ring" at offset 5
"ing" at offset 6
"ng" at offset 7
"g" at offset 8

--
Simon.
Dec 8 '05 #4
>For a declaration such as:

char * mystring = "ABCDabcd123";

Is it a linker issue where such strings are stored in C, or is it
defined as part of the language definition?
As far as the language definition is concerned, there is no "where
strings are stored" (The Bronx?). The closest thing there is is
the issue that some things you can write on and some things you
might not be able to. There is no stack, heap, text smegment, data
smegment, bss smegment, etc.
and strings of type char *, in terms of where they're stored? If these
are compiler dependent, is there at least a general storage convention?


Writing on a string literal invokes the wrath of undefined behavior.
Writing on an array does not (unless it's const).

Gordon L. Burditt
Dec 9 '05 #5
Gordon Burditt wrote:
Writing on a string literal invokes the wrath of undefined behavior.
Writing on an array does not (unless it's const).


String literals and arrays are not mutually exclusive.

--
pete
Dec 9 '05 #6
I might have a clue as for where string literals are stored. From my
experience programming assembler code for PICs (microcontrollers), when
you need to bring a constant out of nowhere to the program, you store
it in the program memory. That is, program memory being the place for
where the actual code resides, the physical storage for the code, which
in this case is the compiled file or the executable file. That`s why
you can`t directly modify it, because modifying it means modifying the
actual file from which the code is being executed. But it is a
different case if you load the literal into a RAM-stored char array.

Dec 9 '05 #7
On 2005-12-08, Bi*************@gmail.com <Bi*************@gmail.com> wrote:
Hi,

For a declaration such as:

char * mystring = "ABCDabcd123";

Is it a linker issue where such strings are stored in C, or is it
defined as part of the language definition?
They are stored in externally-linked static-duration space to which it
is undefined to write. How that's done is of course the linker's
business, but that doesn't affect C per se
Is there any difference between an array of strings, e.g.

char mystring[10];

and strings of type char *, in terms of where they're stored?
Often.
If these are compiler dependent, is there at least a general storage
convention?
ISO/IEC 9899.
Thanks, Bahadir

Dec 9 '05 #8
le*****@gmail.com writes:
I might have a clue as for where string literals are stored. From my
experience programming assembler code for PICs (microcontrollers), when
you need to bring a constant out of nowhere to the program, you store
it in the program memory. That is, program memory being the place for
where the actual code resides, the physical storage for the code, which
in this case is the compiled file or the executable file. That`s why
you can`t directly modify it, because modifying it means modifying the
actual file from which the code is being executed. But it is a
different case if you load the literal into a RAM-stored char array.


First, please provide some context when you post a followup.
Read <http://cfaj.freeshell.org/google/> and follow its advice.

Second, what you describe is *extremely* system-specific. As far as
the C language is concerned, string literals are stored somewhere; as
long as they exist for the duration of the program's execution, it
doesn't matter where. Anything that depends on some particular scheme
is going to be non-portable.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 9 '05 #9
Bi*************@gmail.com writes:
For a declaration such as:

char * mystring = "ABCDabcd123";

Is it a linker issue where such strings are stored in C, or is it
defined as part of the language definition?
This defines a pointer to char and assigns to it the address of a
string literal. String literals are not writable, so { mystring[0] =
'X'; } triggers undefined behaviour.
Is there any difference between an array of strings, e.g.

char mystring[10];

and strings of type char *, in terms of where they're stored?
This defines an array of char which is implicitly initialized to
all-zeroes at program start (assuming none of this code is within a
function)

The following code:

char mystring[] = "ABCDabcd123";

defines an array of char and initializes it with a copy of the
provided string literal. Since no explicit size is provided, the
array will be precisely large enough to contain its initial value
(including the terminating null character). Unlike in the first
example, { mystring[0] = 'X'; } is well-defined.
If these are compiler dependent, is there at least a general storage
convention?


No, these things vary widely from system to system.

DES
--
Dag-Erling Smørgrav - de*@des.no
Dec 9 '05 #10

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

Similar topics

20
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT"...
17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
16
by: Paul Prescod | last post by:
I skimmed the tutorial and something alarmed me. "Strings are a powerful data type in Prothon. Unlike many languages, they can be of unlimited size (constrained only by memory size) and can hold...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
25
by: Rainmaker | last post by:
Hi, Can anyone tell me an efficient algorithm to sort an array of strings? Keep in mind that this array is HUGE and so the algorithm should me efficient enough to deal with it. Thanks
6
by: Broeisi | last post by:
Hello, I wrote the tiny progam below just to understand arrays and strings better. I have 2 questions about arrays and strings in C. 1. Why is it that when you want to assign a string to an...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
19
by: pkirk25 | last post by:
I wonder if anyone has time to write a small example program based on this data or to critique my own effort? A file called Realm List.html contains the following data: Bladefist-Horde...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.