15

I have 2 questions...(I am learning C and this might be silly questions. Apologies)

  • As per How to declare strings in C and in most of the books, they always say declaring a string even though you are allocating memory by saying

    char p2[] = "String";
    

My question is, Is there anyway to declare a string?

are placed in read only area and then copied to array. Is it valid in C to print the address of the string like this?

printf("%p\n", &"Hello There"); // I tried, it prints some address

and by doing this

printf("%p\n", &"Hello There");
printf("%p\n", &"Hello There");

it is printing the same address. what is feel is, it should print different address. Is compiler doing some optimization here?

Community
  • 1
  • 1
StackIT
  • 1,172
  • 2
  • 13
  • 25

4 Answers4

14

C standard, §6.4.5 String literals, says:

It is unspecified whether these arrays are distinct provided their elements have the appropriate values.

So two strings literals with the same content may denote the same array and printing their addresses gives the same pointer value twice. It's up to the compiler and linker to decide this; when I compile the following program as two separate modules...

// main.c
#include <stdio.h>

extern void print_foo_addr(void);

int main()
{
    printf("%p\n", &"foo");
    print_foo_addr();
    return 0;
}

and

// printfoo.c
#include <stdio.h>

void print_foo_addr()
{
    printf("%p\n", &"foo");
}

... then I get two different pointer values (GCC 4.7.3 on Linux), but when I put the definition for print_foo_addr in main.c, I get the same value twice. So yes, it's an optimization that is explicitly allowed by the Standard, but GCC at least only performs this optimization on a per-module basis.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • Is there anyway to declare a string variable? – StackIT Sep 26 '13 at 12:41
  • I don't think it is possible to declare a string variable. A string variable is always defined. Am I correct? – StackIT Sep 26 '13 at 12:45
  • 1
    @Patil: what do you mean by "string variable"? You can have an array of char, or a pointer to a char. Those can each be declared. If the array contains a null character and all value preceding that null are values in some particular character set (or if the same holds for the value pointed at by the char *), then you can call the variable a "string". – William Pursell Sep 26 '13 at 13:09
  • @WilliamPursell: Ya, got that. Thanks. – StackIT Sep 26 '13 at 13:11
3

This is specifically allowed by the C Standard.

6.4.5p5-6:

In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals. The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence. ...

It is unspecified whether these arrays are distinct provided their elements have the appropriate values.

Community
  • 1
  • 1
aschepler
  • 70,891
  • 9
  • 107
  • 161
0

The "Hello There" is string literal and it will be available in string table (The reason behind getting same address for "Hello There" in printf).

When you do

char p2[] = "String";

memory will be allocated and "String" will be copied to newly created memory. But

char * p3 = "String"; will point to read-only section. i.e string table.

In this case, if you print p3 and &"String" will be same.

If you print p2 and &"String", different address will be printed. Because p2 is stack address.

Jeyaram
  • 9,158
  • 7
  • 41
  • 63
0

Literal strings are placed in a special section, which should be read only. Additionally, if you are using the same string literal in multiple places, then the compiler usually can create only one instance of that string. This is why you see the same adress in this example:

printf("%p\n", &"Hello There");
printf("%p\n", &"Hello There");

However, you can not rely on this, because it is a compiler optimization which can or can not happen.

Devolus
  • 21,661
  • 13
  • 66
  • 113
  • BSS is normally used for static data that is initialized to zero. Constant data often goes in the text section with the code. – Dipstick Sep 26 '13 at 12:22