Re: difference between string literal declarations



Daniel Molina Wegener wrote:
What is main difference between:
char *s = "hello world";
and:
char t[] = "hello world";

As answered previously, the first declaration 's' is a pointer
and the second declaration (renamed to 't' for clarity) is
an array of characters. Both behave as null-terminated strings.


What is optimal declaration for literal strings according
to the C99 standard semantics?

The only optimizations to consider depend entirely on the
compiler and machine you are using. However, some things
to consider:

1. The definition for 's' allocates a pointer in addition to the
storage of the character string "hello, world". The definition of
't' only allocates the string characters. Your memory requirements
for 's' are slightly higher.

2. There may be differences in access times between 's' and 't'
because of paging. In principle, accessing 's' requires accessing
the pointer variable 's' and then using its value to access the
character of the string. Accessing the characters in 't' might be
more efficient since (on most systems) the address of 't' is a
runtime constant (computed by the linker) instead of a variable,
which could mean one less memory access.

3. Optimizing compilers might eliminate these differences, so
that using 's' or 't' yields exactly the same performance.

These issues are not addressed by the ISO C standard, though,
because it deals with an idealized abstract machine that executes
C code, which may not reflect your actual implementation.

FWIW, I tend to prefer the second form ('t') for allocating large
amounts of string constants (or arrays of strings) to avoid the
overhead of the extra pointers. But that's based on my experience
with the particular compilers I use. Your mileage may vary.

Unless you're constrained on memory (e.g., embedded applications),
the time/space differences are probably not noticeable enough
to matter.

-drt
.



Relevant Pages

  • Re: "Mastering C Pointers"....
    ... A pointer is a kind of variable that can "point to" some object. ... has a type (pointer to int), and a value of some kind. ... You may know that you can access these integers by using array notation ... The function will take one argument, a string, and will return the length ...
    (comp.lang.c)
  • Re: copy a string into a 2d array of chars
    ... This split function should allocate a 2D array of chars ... >focus the program the string is not actually split. ... later) is an array of char containing the original contents of the ... The i-th pointer will contain the starting address of the ...
    (comp.lang.c)
  • Re: chars
    ... A string is a pointer to an array of ... A string is, by definition, "a contiguous sequence of characters ... terminated by and including the first null character", and a "pointer ...
    (comp.lang.c)
  • Re: problem with function
    ... Yes I did miss type the psz example, it is a pointer to char (not pointer to ... companies using Hungarian, and when I looked into one standard, I found ... >> If you intended copying the string, you need to use the strcpy function. ... > Isn't 'psz' a pointer a an array of characters containing a string? ...
    (alt.comp.lang.learn.c-cpp)
  • Re: How to convert Infix notation to postfix notation
    ... A string is not "equivalent to a pointer to character". ... I feel that an array really can't be used as a constant pointer, ... I think they want "array of arrays of characters". ...
    (comp.lang.c)