C language Pointer job

Source: Internet
Author: User

First, PTA laboratory work

Topic 1: Using functions to implement a partial copy of a string

1. PTA Submission List

2. Design Ideas

The length of the input T string is computed first with a loop, and if M is greater than T, *s is assigned to.

Otherwise, starting from m-1, to the length of T, the T loop is assigned to the latter one of S,s plus.

Flow chart:

3. Problems encountered in debugging process and PTA Submission List situation description.

The critical relationship is wrong, the IF condition is changed to M>i,i is the effective length of the T string, m=i the characters in T are all assigned to S,

So when the two are equal, S is not an empty string.

Topic 2: Find the Week

1. PTA Submission List

2. Design Ideas

Because you want to store seven strings, representing the day of the week, you define a pointer array p and assign values when you define them.

Define the variable I for the loop, and then use the strcmp function, if the string passed in the main function is equal to the p[i], return I,

otherwise return-1.

Flow chart:

3. Problems encountered in commissioning process and PTA submission List status note

There is no error in the subject, that is, the first time to define an array of pointers, at the beginning is not sure whether this is correct,

Nothing else is wrong.

Topic 3: Finding the specified element in an array

1. PTA Submission List

2. Design Ideas

Define REPEAT,N,X,Q, array a,i,j, input repeat,

Loop repeat operations, enter n, enter n integers into array A, enter an integer x,

Call the search (list, n, x) function, Q=search (list, n, x)

Finds the element x in the array list, returns the smallest subscript for the corresponding element if found, otherwise returns-1,

Q=-1 output not found, otherwise the output corresponds to subscript.

In the child function search, define m=0 as the flag, and G to save the subscript.

With a loop that satisfies x equals an element in the array list, M=1,g saves the subscript,

If m=0, returns 1, otherwise returns G.

Flow chart:

3. Problems encountered in commissioning process and PTA Submission List status Note

Format error because output is less line break

The run timeout is the second for loop in which the loop condition is incorrectly written, causing the program to fall into an infinite loop and run timeout.

I in the cyclic condition should be changed to J.

Second, peer code peer evaluation

My Code

Classmate Code

What's the difference between me and my classmate's code? What are the respective strengths? What style of code do you prefer?

My code first uses a double loop to find the character in s that is equal to the character of the substring T,

Two variables are used to verify that all characters after that character are equal in two strings,

Use N to record the number of equals, and if the equal number is equal to the length of the substring T, the position of the substring T in S is returned.

Yan Chan's code is a looping string s, and the substring T of the first word typeface comparison, if equal,

Then the loop verifies that the character after the substring T is typeface with the word after S.

I prefer the code style of Yan Chan classmate, more concise, my code double cycle of the second weight is not necessary,

Because the characters after the first character of a substring T are equal to s, there is no need to verify.

Third, this week's topic set PTA Final Ranking

Iv. Summary of this week's study

1. What have you learned?

1.1 Definition of pointer variables

The difference from the definition of a common variable is that the variable name is added * before the

The type of a pointer variable is the type of variable it points to.

1.2 Basic operation of pointers

Fetch address Operation:&

int *p=&a, which represents the address of the variable A, and the address value as the value of the pointer p, so that the pointer p points to a

Indirect access Operator: *

It has two functions: Define a pointer and access the variable that the pointer points to.

*p is the value of a

Assignment of pointers: only pointers to the same variable can be assigned.

1.3 The relationship between the pointer and the address

A pointer is a variable with an address as a value

Subtract two pointers of the same type, representing the number of array elements that are separated from each other.

The arithmetic operation of a pointer consists of only two pointer subtraction of the same type and pointer plus or minus an integer, and the other operations are illegal.

Adding 1 to the pointer adds a storage unit to the pointer, plus the object byte size it points to.

1.4 Using pointers for dynamic memory allocation

malloc () calculates the storage block size using sizeof.

The malloc () return value is (void *) type and requires a forced conversion.

Other functions:

Calloc () initializes the allocated memory.

Ralloc () Change the storage allocation before

Free () Frees allocated memory

2. This week's content, you are not what?

C language Pointer job

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.