[C] string, character, and byte (Chapter C and pointer)

Source: Internet
Author: User
Tags strcmp

No explicit data type in C language is string.

C-language storage string: String constant (cannot be modified); character array or dynamically allocated memory (can be modified)


**************************************** ************

9.1 string Basics

String concept: A string is a string of 0 or multiple characters ending with a single-digit NUL byte in full 0.

The NUL byte is the Terminator. The length of the string does not contain the NUL byte.

The header file string. h contains the prototype and declaration required to use the string function (which can be viewed in Linux and has many function declarations ).


**************************************** ************

9.2 String Length

Size_t is an unsigned integer. Definition and stddef. h header file

Unsigned integers may lead to unexpected results during arithmetic operations (Think About It, see 9.2)


**************************************** ************

9.3 unrestricted string functions

-- Copy string strcpy function: char * strcpy (char * DST, char const * SRC );

Note:

1. copying a string also copies NUL bytes;

2. if the destination DST character array space is insufficient to accommodate the string to be copied, strcpy occupies part of the memory space after the Array (because strcpy cannot determine the length of the destination character array, this is also the meaning of "unrestricted)

-- Concatenate the strcat function: char * strcat (char * DST, char const * SRC );

Note:

1. This function finds the end of DST (NUL byte?) and adds a copy of the SRC string to this position.

2. This function also has the strcpy feature. If the DST space is insufficient to accommodate the string to be copied, it will also occupy part of the space behind the array.

-- String comparison strcmp: int strcmp (char const * S1, char const * S2 );

Note:

1. The result of this function is in line with "dictionary comparison"


**************************************** ************

9.4 string functions with a length limit specified

No restricted function is used to determine the length of the NUL byte ending with a string. The function here specifies the number of characters to be copied or compared.

-- Strncpy function: char * strncpy (char * DST, char const * SRC, size_t Len );

Note:

1. Copy the specified number of characters in the source string SRC to the destination array DST. If SRC does not contain Len characters, it will be filled with NUL bytes to DST.

2. If strlen (SRC) is greater than Len, only Len characters are copied .! However, the result will not end with NUL bytes. (That is, the strncpy call result may not be a string)

-- Strncat function: char * strncat (char * DST, char const * SRC, size_t Len );

Note:

1. After the strncat connection is complete, it will automatically add a NUL after the result (so this is safer and will not cause problems in other functions because there is no NUL), such as the following program

#include <string.h>#include <stdio.h>int main(){char *src = "world";char dst[7] = "hello";strncat(dst, src, 5);printf("%s", dst);return 0;}

Because DST capacity (7) is not enough to hold helloworld (11), strncat encroaches on several bytes after DST, but after encroaching on, strncat adds a NUL byte. Therefore, there is both an insecure side (encroaching on subsequent bytes) and a secure side (automatically adding NUL bytes)

2. When SRC is not long enough, it will not continue to fill DST with NUL bytes

*************************** The following are some of the relevant analysis

#include <string.h>#include <stdio.h>int main(){char *src = "world";printf("length of src is %d\n", sizeof(src));printf("length of src is %d\n", strlen(src));char dst[7] = "hello";int mat[100];strncat(dst, src, 9);printf("%s\n", dst);printf("length of dst is %d\n", sizeof(dst));printf("length of src is %d\n", sizeof(src));printf("length of src is %d\n", strlen(src));printf("length of mat is %d\n", sizeof(mat));return 0;}
The running result is



This illustrates the following points:

1. the array name is generally used as a pointer, but in sizeof (array name), it is used to obtain the number of bytes occupied by the array;

2. After strncat and helloworld are output, the number of cell lines for the DST array is still 7. This indicates that the array length is recorded and will not change. However, as long as DST is output in % s format, helloworld will be output. Because '\ 0' is added to the backend ';

3. completely puzzled! Why is there 5 at the beginning of strlen (SRC), but after strncat is called, strlen (SRC) has only 1? Answers ~


Let's look at the following program.

#include <string.h>#include <stdio.h>int main(){//1        *src = "hello";printf("length of src is %d\n", sizeof(src));// 2printf("length of 'hello' is %d\n", sizeof("hello"));// 3char haha[] = "hello";printf("length of haha is %d\n", sizeof(haha));return 0;}


The running result is


This illustrates the following points:

1. combined with row 1 & 2, my analysis is that in column 1, SRC is an identifier that exists in the symbol table in the relocated target file, and is a pointer variable, initialize with the starting address of the constant string "hello" (runtime). In 2, "hello" is a constant string, which has been stored before running, place it in the relocated target file (after compilation, link the previously generated file ). in rodata segment (? No, I checked it with objdump. "hello" in Row 1 is indeed stored in the rodata segment, but "hello" in sizeof ("hello") is not stored in rodata, because if row 1st is deleted, there is no hello in rodata. If row 1st is deleted)


2. Combined with 2 & 3 analysis, the character array contains '\ 0'


[C] string, character, and byte (Chapter C and pointer)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.