char * --Type that represents a string

To express string, use "char *" which is a pointer type of char.

char *

You can assign a string literal to the "char *" type.

char * name = "Perl";

In general, when using the "char *" type for assigning string literals, it is advisable to add a constant qualifier to prevent character changes.

const char * name = "Perl";

Keep in mind that a string in C is an array of characters ending in "\ 0". If you write the element, it will be as follows.

0 1 2 3 4
P e r l \ 0

Semantically, the assignment of a string literal is the same as the code below.

This is a sample that substitutes a string literal and outputs it.

#include <stdio.h>

int main (void) {
  const char * name = "Perl";
  printf("%s\n", name);
}

When generating a character string at runtime

To dynamically generate a string at run time, allocate memory with malloc. The character Perl is 4 characters, but 5 bytes are reserved because "\ 0" is required at the end. The size of char is 1 byte.

#include <stdlib.h>

char * name = malloc (sizeof (char) * 5);

// Perl
name [0] ='P';
name [1] ='e';
name [2] ='r';
name [3] ='l';
name [4] ='\ 0';

This is a sample that outputs a dynamically generated character string.

#include <stdio.h>
#include <stdlib.h>

int main (void) {

  char * name = malloc (sizeof (char) * 5);

  // Perl
  name [0] ='P';
  name [1] ='e';
  name [2] ='r';
  name [3] ='l';
  name [4] ='\ 0';

  printf("%s\n", name);
}

Is it possible to create a string containing \ 0 in the middle?

Although it can be included, C language functions for strings assume that the end of the string is "\ 0". If "\ 0" is included in the middle of the character string, that position is recognized as the end position of the character string. The expression of the character string constant is also automatically added with "\ 0" at the end. In C, it's best to assume that the grammar or function expects the string to end with "\ 0".

Does the C language string contain length information?

Yes. C language strings do not contain length information. Since "\ 0" is regarded as the end position, it is necessary to count the number of characters up to "\ 0" when acquiring the length. The strlen function that gets the length of the string counts the number of characters up to "\ 0".

C language string security risk

Since there is a specification that a character string in C language is regarded as a character string up to "\ 0" at the end, if you forget to include "\ 0" at the end of the character string, the character string function in C language (maximum) If the number of characters to be processed cannot be specified), it will be read to an unintended position. If you write at this position, a buffer overrun will occur.

This is a memory operation when writing C language, which can easily happen.

When dealing with C string functions, you should always consider whether you are reading an unintended area or not doing a buffer overrun.

You can see one of the reasons why it is difficult to handle strings in C language.

Associated Information