Explanation of the use of conditional operators, computed string lengths, and operator strings in C language and pointer

Source: Internet
Author: User

Explanation of the use of conditional operators, computed string lengths, and operator strings in C language and pointer
1. Conditional Operators

For example:

A> 5? B-6: c/2

Can be read as "is a greater than 5? If yes, execute the B-6; otherwise execute c/2

In what scenarios is it better to use it?

For example:

If (a> 5)

B [2 * c + d (e/5)] = 3;

Else

B [2 * c + d (e/5)] = 20;

It is very convenient to use conditional operators here.

B [[2 * c + d (e/5)] = a> 5? 3:20

2. Calculate the string length

Although the library already exists, you can compile a learning method.

Int strlen (char * string)

{

Int length = 0;

/* Access the string content in sequence and calculate the number of characters until the NUL Terminator is met */

While (* string ++! = '\ 0'

Length + = 1;

Return length;

}

3. Recursion

For example, you need to convert an integer to a printable character form. Generally, You need to divide the integer by 10 and print the remainder, but there is a simple way to convert it into a character.

'0' + 0 = '0 ';

'0' + 1 = '1 ';

'0' + 2 = '2 ';

In this way, the remainder plus '0' can be converted, but the output value is reversed, so it can be solved by recursive thinking.

Void binary2ascii (unsigned int value)

{

Unsigned int quo;

Quo = value/10;

If (quo! = 0)

Binary2ascii (quo );

Putchar (value % 10 + '0 ');

}

4. character array Initialization

In general, you may think that the initialization of character arrays is like this.

Char message [] = {'h', 'E', 'l', 'l', 'O', 0 };

This method looks clumsy and has a higher level of writing

Char message [] = "Hello ";

Although it looks like a string, not the following is actually the initialization string

Char * message = "Hello ";

5. Multi-dimensional array as function parameters

Int matrix [3] [10]

...

Func2 (marix)

/There are two methods to declare a function:/

Void func2 (int (* mat) [10]);

Void func2 (int mat [] []);

In addition, it is worth noting that the storage order of multi-dimensional arrays is determined based on the rightmost subscript priority change principle.

6. query a character string by 6.1

The simplest way to search for a character in a string is to use the strchr and strrchr functions. The function prototype is as follows:

char *strchr(char const *str,int ch);char *strrchr(char const *str,int ch);

Note that the second parameter is an integer value. The position where strchr appears for the first time in the string 'str'. After finding it, the function returns a pointer pointing to the changed position. If the character does not exist in the string, the function returns a NULL pointer. The str function is similar to this function, but it returns the position at which the character appears at the end of the string. For example:

char string[20]="Hello there";char *check;check=strchr(string,'h');

Check points to the string + 7 position, because the first h appears at this position. Note that the case sensitivity is different here.

6.2 search for people and strings

Strpbrk is a more common function. Instead of looking for a specific string, it searches for the position where any group of characters appear in the string for the first time. Its prototype is as follows:

char *strpbrk(char const *str ,cahr const *group);

This function returns a character location pointing to any character in the First Matching group in str. If no matching is found, the function returns a NULL pointer. For example:

char string[20]="Hello there";char *check;check=strpbrk(string , "aeiou");

Check points to the string + 1 position, because this position is the first occurrence of the character in the second parameter. It is also case sensitive like the previous one.

7. Character classification functions

The following function returns true if the parameter meets the conditions (including in ctype. h)

1. isspace -------- blank characters: space, page feed, line feed, press enter, etc. 2. isdigit -------- decimal number 0-93.islower -------- lowercase letter ~ Z4.isupper -------- uppercase letter ~ Z5.isalpha -------- letter ~ Z or letter ~ Z6.ispunct -------- punctuation

For example:

If (ch> 'A' & ch <'Z'), this statement can only run on the ASCII machine. if (isupper (ch ))
8. About struct

A good trick to use when declaring a struct is to use typedef to create a new type, for example:

typedef struct{    int a;    char b;    float c;}Simple;...Simple x;Simple y[20],*z;

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.