Hướng dẫn này giới thiệu cách lấy một chuỗi con từ giá trị ký tự trong C. Có nhiều phương pháp khác nhau để lấy chuỗi con từ ký tự như void *memcpy(void *destination_string, const void *source_string, size_t number); 6 và void *memcpy(void *destination_string, const void *source_string, size_t number); 7
void *memcpy(void *destination_string, const void *source_string, size_t number); 6 Hàm Lấy Chuỗi Con trong C
Hàm void *memcpy(void *destination_string, const void *source_string, size_t number); 6 sao chép số ký tự từ nguồn sang vùng nhớ của đích. Chức năng này có sẵn trong tệp tiêu đề #include #include int main(void) { char *text = "The test string is here"; char subtext[7]; memcpy(subtext,&text[9],6); subtext[6] = '\0'; printf("The original string is: %s\n",text); printf("Substring is: %s",subtext); return 0; } 0
Chức năng này tạo ra sự cố khi địa chỉ của nguồn và đích trùng nhau. Chức năng này không kiểm tra địa chỉ rỗng hay tràn dữ liệu có xảy ra hay không
Hàm void *memcpy(void *destination_string, const void *source_string, size_t number); 6 trả về một con trỏ tới chuỗi đích. Không có giá trị trả về để hiển thị lỗi
Cú pháp của void *memcpy(void *destination_string, const void *source_string, size_t number); 6
void *memcpy(void *destination_string, const void *source_string, size_t number);
- #include #include int main(void) { char *text = "The test string is here"; char subtext[7]; memcpy(subtext,&text[9],6); subtext[6] = '\0'; printf("The original string is: %s\n",text); printf("Substring is: %s",subtext); return 0; } 3 là một con trỏ tới chuỗi đích
- #include #include int main(void) { char *text = "The test string is here"; char subtext[7]; memcpy(subtext,&text[9],6); subtext[6] = '\0'; printf("The original string is: %s\n",text); printf("Substring is: %s",subtext); return 0; } 4 là một con trỏ tới một mảng có kiểu giá trị ký tự gốc
- #include #include int main(void) { char *text = "The test string is here"; char subtext[7]; memcpy(subtext,&text[9],6); subtext[6] = '\0'; printf("The original string is: %s\n",text); printf("Substring is: %s",subtext); return 0; } 5 là số ký tự
#include #include int main(void) { char *text = "The test string is here"; char subtext[7]; memcpy(subtext,&text[9],6); subtext[6] = '\0'; printf("The original string is: %s\n",text); printf("Substring is: %s",subtext); return 0; }
đầu ra
The original string is: The test string is here Substring is: string
void *memcpy(void *destination_string, const void *source_string, size_t number); 7 Hàm Lấy Chuỗi Con trong C
Hàm void *memcpy(void *destination_string, const void *source_string, size_t number); 7 giống như hàm #include #include int main(void) { char *text = "The test string is here"; char subtext[7]; memcpy(subtext,&text[9],6); subtext[6] = '\0'; printf("The original string is: %s\n",text); printf("Substring is: %s",subtext); return 0; } 8. Sự khác biệt duy nhất là hàm void *memcpy(void *destination_string, const void *source_string, size_t number); 7 sao chép số ký tự đã cho từ chuỗi nguồn sang chuỗi đích. Hàm void *memcpy(void *destination_string, const void *source_string, size_t number); 7 có sẵn trong tệp tiêu đề #include #include int main(void) { char *text = "The test string is here"; char subtext[7]; memcpy(subtext,&text[9],6); subtext[6] = '\0'; printf("The original string is: %s\n",text); printf("Substring is: %s",subtext); return 0; } 0
Hàm này trả về một con trỏ tới chuỗi đích sau khi sao chép chuỗi nguồn
Cú pháp của void *memcpy(void *destination_string, const void *source_string, size_t number); 7
void *strncpy(void *destination_string, const void * source_string, size_t number);
- #include #include int main(void) { char *text = "The test string is here"; char subtext[7]; memcpy(subtext,&text[9],6); subtext[6] = '\0'; printf("The original string is: %s\n",text); printf("Substring is: %s",subtext); return 0; } 3 là một con trỏ tới chuỗi đích
- #include #include int main(void) { char *text = "The test string is here"; char subtext[7]; memcpy(subtext,&text[9],6); subtext[6] = '\0'; printf("The original string is: %s\n",text); printf("Substring is: %s",subtext); return 0; } 4 là một con trỏ tới một mảng các giá trị gốc của The original string is: The test string is here Substring is: string 5
- #include #include int main(void) { char *text = "The test string is here"; char subtext[7]; memcpy(subtext,&text[9],6); subtext[6] = '\0'; printf("The original string is: %s\n",text); printf("Substring is: %s",subtext); return 0; } 5 là số ký tự
Nếu hàm void *memcpy(void *destination_string, const void *source_string, size_t number); 7 gặp ký tự null trong #include #include int main(void) { char *text = "The test string is here"; char subtext[7]; memcpy(subtext,&text[9],6); subtext[6] = '\0'; printf("The original string is: %s\n",text); printf("Substring is: %s",subtext); return 0; } 4, hàm sẽ thêm ký tự null vào #include #include int main(void) { char *text = "The test string is here"; char subtext[7]; memcpy(subtext,&text[9],6); subtext[6] = '\0'; printf("The original string is: %s\n",text); printf("Substring is: %s",subtext); return 0; } 3