简体   繁体   中英

Is array of characters another way of representing String constant in C?

char a[ ]= "HELLO";

Can we say that the HELLO above is a string constant.I have a doubt in this, because we can change the string elements like a[0]='A' . So, can we still claim "HELLO" here as a string constant just because it contains a NULL at the end?

Yes, "HELLO" is still a string constant (more particularly, it is a string literal ).

However, the array a , whose contents were initialised to be a copy of that constant, is not.

"HELLO" is a string literal - it is stored as an array of char in such a way that it is available over the lifetime of the program. It may be stored in a read-only memory segment, depending on the implementation (on my system, it is). Attempting to modify the contents of a string literal leads to undefined behavior.

a is not a string literal - it's an array of char that's initialized with the contents of the string literal "HELLO" . You can change the contents of a as you wish (although you can't store a string longer than 5 characters to it).

Here's a short program to show the relationship between the string literal "HELLO" and the array a (along with a pointer thrown in for good measure):

#include <stdio.h>
#include "dumper.h"

int main( void )
{
  char a[] = "HELLO";
  char *p = "HELLO";

  char *names[] = { "\"HELLO\"", "a", "p" };
  void *addrs[] = { "HELLO", a, &p };
  size_t sizes[] = { sizeof "HELLO", sizeof a, sizeof p };

  dumper( names, addrs, sizes, 3, stdout );

  return 0;
}

dumper is a utility I wrote that displays the memory map for each object. When compiled and run, we get the following output:

       Item         Address   00   01   02   03
       ----         -------   --   --   --   --
    "HELLO"        0x400b30   48   45   4c   4c    HELL
                   0x400b34   4f   00   22   48    O."H

          a  0x7fff89062330   48   45   4c   4c    HELL
             0x7fff89062334   4f   00   00   00    O...

          p  0x7fff89062328   30   0b   40   00    0.@.
             0x7fff8906232c   00   00   00   00    ....

The string literal "HELLO" is stored as a sequence of ASCII (or UTF8) characters starting at address 0x400b30 .

The array a is stored starting at address 0x7fff89062330 - as you can see, it contains a copy of the string "HELLO" .

The pointer p is stored starting at address 0x7fff89062328 - it stores the address of the string literal "HELLO" (x86 is little-endian, so multi-byte values are stored starting with the least-significant byte - you'll need to read from right-to-left, bottom-to-top).

One thing you notice is the difference in addresses between "HELLO" and the variables a and p . On my system, "HELLO" is stored in the .rodata memory segment:

$ objdump -j .rodata -d strings2

strings2:     file format elf64-x86-64

Disassembly of section .rodata:

0000000000400b2c :
  400b2c:       01 00 02 00 48 45 4c 4c 4f 00 22 48 45 4c 4c 4f     ....HELLO."HELLO
  400b3c:       22 00 61 00 70 00 41 64 64 72 65 73 73 00 49 74     ".
  ...

a and p are allocated from the runtime stack, which starts at a high address and grows "downwards" towards lower addresses.

No.

A constant is a variable that doesn't change (or should not change) through the execution of a program. As long as you can do a[1] = 'd' it's not a constant.

To define a constant string in c, you could do it three ways:

#define WHATEVER = "HELLO";
const char WHATEVER[] = "HELLO";
static const char WHATEVER[] = "HELLO";

Other definitions won't create a constant string, but a mutable char array, which is a different thing.

I would say it's a string, but not a string constant. A string is a null-terminated array of characters. A string constant would have to be immutable. When a string appears in code as defined with double quotes (eg. "hello" ), it's considered a string literal. The actual character array in the memory would be a string.

No, that is not a string constant in C. By the way, any string in C language has to terminate with '\\0', example:

// '\0' explicitly included as the last element
char str[] = "HELLO";  

// '\0' has to be the last element in this case
char str2[] = {'H', 'E', 'L', 'L', 'O', '\0'}; 

If you want to add a string constant in your C code, here is an example:

const const_str[] = "HELLO";

Another example of constant string:

#define CONST_STR "HELLO"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM