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?
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.
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.
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);
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With