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

6

Someone can explain to me what is the difference between:

char* name = "Gerardo";

or

char name[] = "Gerardo";

I would like to know the differences in terms of memory or performance.

    
asked by Gemasoft 09.09.2016 в 19:14
source

3 answers

7

A fundamental difference is that in the case of the pointer it is pointed to a literal string of characters. Trying to modify it is indefinite behavior.

While in the other case the array contains a copy of the string that can be modified.

The fact that a string literal is not modifiable allows compilers to make certain optimizations. If a string is repeated in the code and used as a literal the compiler can use the same copy of the string for all uses, reducing memory usage. It can also be used directly, which is faster.

For example, this program in C:

#include<stdio.h>
void f() {
  char* name = "Gerardo";
  puts(name);
}

int main(void) {
  char* name = "Gerardo";
  puts(name);
  return 0;
}

The compiled assembly code (leaving only the relevant parts):

.LC0:
        .string "Gerardo"     <----- Aparece solo una vez
        .section        .text.unlikely,"ax",@progbits

f:
.LFB23:
        .cfi_startproc
        movl    $.LC0, %edi <---- Ponemos la dirección de la cadena en edi
        jmp     puts        <---- Y puts usa la cadena desde ahí directamente
        .cfi_endproc

main:
.LFB24:
        .cfi_startproc
        subq    $8, %rsp
        .cfi_def_cfa_offset 16
        movl    $.LC0, %edi   <------- Main usa la misma cadena
        call    puts

However the same program using arrays:

void f() {
  char name[] = "Gerardo";
  puts(name);
}
int main(void) {
  char name[] = "Gerardo";
  puts(name);
  return 0;
}

When compiling it:

f:
.LFB23:
        .cfi_startproc
        subq    $24, %rsp          <----- Hay que reservar espacio en la pila
        .cfi_def_cfa_offset 32     
        movq    %fs:40, %rax
        movq    %rax, 8(%rsp)
        xorl    %eax, %eax
        movq    %rsp, %rdi
        movabsq $31354164838819143, %rax <--- Hay que copiar la cadena a la pila;
                                         <--- Esto lleva tiempo y ocupa espacio
        movq    %rax, (%rsp)
        call    puts

main:
.LFB24:
        .cfi_startproc
        subq    $24, %rsp           <------ Reservando espacio en la pila
        .cfi_def_cfa_offset 32
        movq    %fs:40, %rax
        movq    %rax, 8(%rsp)
        xorl    %eax, %eax
        movq    %rsp, %rdi
        movabsq $31354164838819143, %rax  <--- Gerardo está 2 veces en el binario
        movq    %rax, (%rsp)
        call    puts

To compile these examples I used gcc -c -S -O2 fichero.c (version 5.4.0)

Conclusion . If you are not going to modify the string it is faster and it uses less memory the string literal alternative assigned to a pointer. If you are going to modify the string you must use the array.

    
answered by 09.09.2016 / 22:35
source
3

Character pointers are typically used to access character strings, since they are constructed based on an address (the first character) and a end-of-string marker (the character '\ 0'). The declaration of a character pointer follows the usual rules:

char * puntero_caracter;

String Pointers: Definition, Declaration and Initiation. The declaration of a chain can be as follows:

char cadena[LONGITUD];

This is how space is reserved for a string of LENGTH characters.

In C, the name of the one-dimensional lists is equivalent to the address of the first element of the same. Once you have space reserved for the string, you can build a pointer to the string using the following statements:

char * p;
char cadena[LONGITUD];
p = cadena;/* Esto equivale, desde luego, a poner: */
p = &cadena[0];

Ref.

link

    
answered by 09.09.2016 в 19:26
2

The pointers to char access to strings of characters with the address of the 1st element of the chain, that string ends with '++' (end of string character). When you make * you move to the next address in the chain (next character).

Char fixes are a pointer to the 1st character of the string. Therefore, from the point of view of how they are stored in memory there is no difference. The difference is in the syntax, and there the use of array type syntax is better, because when using pointer syntax the compiler must resolve the operator overload %code% since it is also used for multiplications and that affects a bit the performance in the execution and compilation.

Also using arrangements is easier for the human eye. The syntax is more understandable with fixes than with pointers.

    
answered by 09.09.2016 в 19:40