Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What makes a char * an array of chars?

Normally, if you do the following:

int * i = &someint;

It's just a pointer to a variable.

But, when you do

char * str = "somestring";

it automatically turns it into an array. Is it the pointer which is doing this, or is it just syntactic sugar for initialization syntax?

like image 335
zeboidlund Avatar asked Dec 12 '11 04:12

zeboidlund


People also ask

What does char * array mean?

A character array is a sequence of characters, just as a numeric array is a sequence of numbers. A typical use is to store a short piece of text as a row of characters in a character vector.

What is a char * array in C?

In C, an array of type char is used to represent a character string, the end of which is marked by a byte set to 0 (also known as a NUL character)

Are char * and char [] the same?

The main difference between them is that the first is an array and the other one is a pointer. The array owns its contents, which happen to be a copy of "Test" , while the pointer simply refers to the contents of the string (which in this case is immutable).

What is the difference between char * and char [] in C?

The fundamental difference is that in one char* you are assigning it to a pointer, which is a variable. In char[] you are assigning it to an array which is not a variable.


2 Answers

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.

like image 177
paxdiablo Avatar answered Oct 27 '22 00:10

paxdiablo


It is not a pointer to a variable. It is a pointer to a place in memory. You are creating a variable and storing it in some memory location, then pointing the pointer at that location. The reason it works for arrays is because the elements of the array are stored back to back in memory. The pointer points at the start of the array.

like image 34
cadrell0 Avatar answered Oct 26 '22 23:10

cadrell0