Character pointer FAQs

Source: Internet
Author: User

We already know that string constants are character sequences enclosed by double quotation marks, for example:
"A string"
It is a String constant. Because character a is followed by a space character, it is composed of eight character sequences. If a String constant C Compilation Program appears in a program, a storage area is arranged for the String constant. This area is static and is always occupied during the entire program running, the length of a String constant is the number of characters in the string, the C compiler automatically adds an empty character '''' to the end of the string sequence to mark the end of the string, therefore, the number of bytes in the storage area occupied by a String constant is one more byte than the number of its characters.
The methods for operating a String constant in Turbo C include:
(1). Store string constants in a character array, for example:
Char s [] = "a string ";
The array s consists of nine elements. The content in s [8] Is ''''. In fact, during the definition of the character array, the compiler directly rewrites the string to the array, that is, initializing the array s.
(2). Use a character pointer to point to a string, and then use a character pointer to access the string storage area. When a String constant appears in an expression, it is converted to a character pointer according to the type conversion rules of the array. Therefore, if we define a one-character pointer cp:
Char * cp;
So available:
Cp = "a string ";
Point cp to character a, 7, of the String constant.
When there are too many threads, there are too many threads, too many threads.
When-then-→ when a packet has seconds t been r He I have n Gbit/s then
When there are too many threads, there are too many threads, too many threads.
Figure 7.
In the future, we can access this storage area through cp. For example, * cp or cp [0] Is Character a, and cp [I] Or * (cp + I) it is equivalent to character I of a string, but it does not make sense to attempt to modify a String constant through a pointer.

[Program 96]
Question: calculate the number of times a string neutron string appears
1. program analysis:
2. program source code:
# Include "string. h"
# Include "stdio. h"
Main ()
{Char str1 [20], str2 [20], * p1, * p2;
Int sum = 0;
Printf ("please input two stringsn ");
Scanf ("% s", str1, str2 );
P1 = str1; p2 = str2;
While (* p1! = '''')
If (* p1 = * p2)
{While (* p1 = * p2 & * p2! = '''')
{P1 ++;
P2 ++ ;}
P1 ++;
If (* p2 = '''')
Sum ++;
P2 = str2;
Printf ("% d", sum );
Getch ();}

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: 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.