No, the string literal "somestring"
is already a character array, almost certainly created by your compiler.
What that statement is doing is setting str
to point to the first character. If you were to look at the underlying assembler code, it would probably look like:
str314159: db "somestring", 0 ; all string literals here.
: : : :
load r0, str314159 ; get address of string
stor r0, -24[sp] ; store it into local var str.
In a large number of cases, an array will decay to a pointer to the first element of that array (with some limited exceptions such as when doing sizeof
).
By way of example, the following C code:
#include <stdio.h>
int main (void) {
char *somestr = "Hello";
puts (somestr);
return 0;
}
when compiled with gcc -S
to generate x86 assembly, gives us (with irrelevant cruft removed):
.LC0:
.string "Hello"
.text
.globl main
.type main, @function
main:
pushl %ebp ; Standard set up of stack frame,
movl %esp, %ebp ; aligning and making
andl $-16, %esp ; room for
subl $32, %esp ; local variables.
movl $.LC0, 28(%esp) ; Load address of string in to somestr.
movl 28(%esp), %eax ; Call puts with that variable.
movl %eax, (%esp)
call puts
movl $0, %eax ; Set return code.
leave ; Tear down stack frame and return.
ret
You can see that the address of the first character, .LC0
, is indeed loaded into the somestr
variable. And, while it may not be immediately obvious .string
does create an array of characters terminated by the NUL character.