Day 7: C-Based String operations and struct

Source: Internet
Author: User
Tags strtok

The seventh day of training, the ten days that come to Chengdu. All of you are tired. Maybe it's because of the weather, maybe it's because the content we are talking about is hard to understand.

In the morning, I mainly talked about the operations of two string-related functions. Strstr, strtok, and strstr have been in contact with each other before. They are used to search for substrings and return addresses. Strtok is a delimiter used to separate strings. The first program today is to separate the words in the string from the two-dimensional array without using these two functions. This can be done with one loop, and the space in loop traversal is replaced by '\ 0. The Code is as follows:

 1 #include<stdio.h> 2 #include<stdlib.h> 3  4 int main() 5 { 6     char *p ="The functions in the printf family" ; 7     char arr[6][20]={0};  8  9     int i,j,n=0;10     for(i=0,j=0;i<strlen(p);i++,j++){11         *(*(arr+n)+j) = *(p+i);12             if(*(p+i)==‘ ‘){13                 n++;14                 *(*(arr+n)+j)= ‘\0‘;15                 j=-1;16         17             }18     19         20     }21     printf("%s\n",*arr);22     printf("%s\n",*(arr+1));23     printf("%s\n",*(arr+2));24     printf("%s\n",*(arr+3));25     printf("%s\n",*(arr+4));26     printf("%s\n",*(arr+5));27 28 } 
Separate words

It takes a long time to use these two functions to complete the functions of the above program. Post Code first:

1 # include <stdio. h> 2 # include <stdlib. h> 3 # include <string. h> 4 5 Int main () 6 {7 char * P = "the functions in the printf family"; 8 char BRR [1024] = {0 }; 9 char * r = BRR; 10 char * STR = NULL; 11 strcpy (R, P); 12 13 // printf ("% s \ n", strtok (p, k); the string is placed in the read-only zone and the value cannot be changed. 14 15 char * TMP; 16 do17 {18 STR = strstr (R, ""); 19 TMP = strtok (R, ""); 20 R = STR + 1; 21 printf ("% s \ n", TMP); 22} while (STR); 23 24

The use of strtok and strstr is prone to segment errors (that is, memory allocation errors). In line 13 of the Code, the address saved in P is in the constant area, use the strtok function to replace the area that matches the CIDR Block with '\ 0. To change the value of a string, the value of the constant area cannot be changed. Therefore, a segment error occurs when you run 13 lines of code.

The loop body in the above program failed to find an error when I wrote it today, that is, I did not distinguish STR from r when I wrote it. It was implemented by R and the result logic was not clear, the first parameter of the 19th line strstr and the first parameter of the 20 line strtok are considered to be a value.

Today's focus is on the introduction and use of struct. There are two tips: byte alignment and bit domain

First, the byte alignment in the structure follows three principles. 1: The maximum byte cannot exceed the number of bytes occupied by the system. 2. The bytes to be allocated must be directly divided by the allocated one. 3. The maximum allocated bytes must be fully divided by the total allocation.
The role of the bit field is to greatly save space. For example, the char: 3 CHAR: 2 defined in the struct occupies one byte.

The key lies in the difference between. And->. If the memory is used in the front, if the address is used in the front, use->

 

Day 7: C-Based String operations and struct

Related Article

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.