Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it valid to print the address of string in C

Tags:

arrays

c

string

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?

  • As per https://stackoverflow.com/a/1704433/1814023, in an example like this,

    char s[]="hello";
    

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?

like image 936
StackIT Avatar asked Sep 26 '13 11:09

StackIT


People also ask

How do I print a string address?

To print the memory address, we use '%p' format specifier in C. To print the address of a variable, we use "%p" specifier in C programming language.

What is the address of a string in C?

String is a data type that stores the sequence of characters in an array. A string in C always end with a null character (\0), which indicates the termination of the string. Pointer to string in C can be used to point to the starting address of the array, the first character in the array.

How do you print a string in C?

using printf() If we want to do a string output in C stored in memory and we want to output it as it is, then we can use the printf() function. This function, like scanf() uses the access specifier %s to output strings. The complete syntax for this method is: printf("%s", char *s);

Is string allowed in C?

Overview. The C language does not have a specific "String" data type, the way some other languages such as C++ and Java do. Instead C stores strings of characters as arrays of chars, terminated by a null byte.


2 Answers

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.

like image 134
Fred Foo Avatar answered Oct 24 '22 21:10

Fred Foo


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.

like image 22
aschepler Avatar answered Oct 24 '22 20:10

aschepler