If the input sequence of the variable in the struct is incorrect, the execution result is incorrect.

Source: Internet
Author: User

If the input sequence of the variable in the struct is incorrect, the execution result is incorrect.

I wrote a program to create a one-way dynamic linked list consisting of three student data nodes and input student data to each node.

(Each student's data includes student ID, name, and score), and then outputs data in each node one by one.

The correct procedure is as follows:

# Include <stdio. h>
# Include <malloc. h>
# Define LEN sizeof (struct student)


Struct student
{
Int num;
Char name [20];
Float score;
Struct student * next;
};


Void main ()
{
Struct student * head, * p, * q;
Head = p = (struct student *) malloc (LEN );
Scanf ("% d, % f, % s", & p-> num, & p-> score, p-> name );
P = (struct student *) malloc (LEN );
Scanf ("% d, % f, % s", & p-> num, & p-> score, p-> name );
Q = (struct student *) malloc (LEN );
Scanf ("% d, % f, % s", & q-> num, & q-> score, q-> name );
Head-> next = p;
P-> next = q;

Q-> next = NULL;




P = head;
Printf ("\ n Node 1: % d, % 5.1f, % s", p-> num, p-> score, p-> name );
P = p-> next;
Printf ("\ n Node 2: % d, % 5.1f, % s", p-> num, p-> score, p-> name );
Q = p-> next;
Printf ("\ n Node 3: % d, % 5.1f, % s \ n", q-> num, q-> score, q-> name );


}

The running result is:

Input:

10101,98, li
10102,87, wang
10103,76, qi
Output:

Node, 98.0, li
Node, 87.0, wang
Node, 76.0, qi

The error code is as follows:

# Include <stdio. h>
# Include <malloc. h>
# Define LEN sizeof (struct student)


Struct student
{
Int num;
Char name [2];
Float score;
Struct student * next;
};


Void main ()
{
Struct student * head, * p, * q;
Head = p = (struct student *) malloc (LEN );
Scanf ("% d, % s, % f", & p-> num, p-> name, & p-> score );
P = (struct student *) malloc (LEN );
Scanf ("% d, % s, % f", & p-> num, p-> name, & p-> score );
Q = (struct student *) malloc (LEN );
Scanf ("% d, % s, % f", & q-> num, q-> name, & p-> score );
Head-> next = p;
P-> next = q;

Q-> next = NULL;




P = head;
Printf ("\ n Node 1: % d, % 5.1f, % s", p-> num, p-> score, p-> name );
P = p-> next;
Printf ("\ n Node 2: % d, % 5.1f, % s", p-> num, p-> score, p-> name );
Q = p-> next;
Printf ("\ n Node 3: % d, % 5.1f, % s \ n", q-> num, q-> score, q-> name );


}
The running result is:

Input:

10101, wang, 98
10102, wani, 87
10103, wangx, 76

Output:
Node, 0.0, wang, 98 p
Node, 0.0, wani, 878p
Node, 0.0, wangx, 7

The expected result is:

Node, 98.0, wang,
Node, 87.0, wani,
Node, 76.0, wangx,


If you place the string array you want to input in the middle of the input, it is very likely to cause

After the string is input, the following content is entered into the string,

Will lead to confusion of the output value and fail to get the desired result. Therefore, it is best to set the string

Put the input parameters at the end of the parameter list to avoid output errors.



I am very grateful for the Structure Input and Output problems and compilation errors.

What's the problem? When you output printf, pay attention to adding spaces between each output.
In addition, you must add a comma between the last three inputs.
As for # include "stdafx. h", it is estimated that the header file must be added when you select the console project.

What are the most common errors when running a program in C language?

I. Basic knowledge and data types, EXPRESSIONS 1, {}, [], (), '', and" "are not paired. The best way to solve this problem is to write a pair of characters each time you write these symbols, and then add content in the middle. 2. Forget to add points at the end of the statement, or add a semicolon after the pre-processing command. Remember: The extra points must be added to the back of each statement. The pre-processing command is not a statement. Therefore, no extra points must be added to each line. You cannot write multiple commands in one line. 3. obfuscation/and \; the symbol corresponding to the annotation is/**/, while the escape character starts with \, and the division sign is /. 4. The parameters of printf () and scanf () are incorrectly set, mainly in the following aspects: l Type Mismatch problem. (For example, if float a = 3.5, but printf ("a = % d", a) is output ); the screen displays a = 0.00000 or other running errors ). The basic principle is: float corresponds to % f, int corresponds to % d, char corresponds to % c. The number of l does not match. No matter which function is used, there can be n parameters. The first one is always the content enclosed by "", indicating the output format. The remaining n-1 values are output variables or input variable addresses. Note that if there are n-1 parameters, the front must correspond to n-1 format specifiers of the % f type. L in scanf (), I forgot to add & before the variable &. Remember: In scanf (), there must be & (but do not need to be added before the character array name and pointer) 5. When defining identifiers, invalid characters are often used. For example: the identifier cannot contain spaces, that is, it cannot be defined as int radium of circle. Generally, three words can be connected together with underscores. 6. undefined before using the variable, or not initialized. For example, if sum below is undefined, an error message will be prompted during compilation. If sum is not initialized to 0, the result of sum must be incorrect. Void main () {int I, a [10], sum = 0;/* This definition must be used below, generally, the initial value */for (I = 0; I <10; I ++) sum + = a [I]; printf ("% d ", sum);} 7. calculation error. Note: In addition to calculating the priority, you must consider the special meaning of the priority when operating with other operators; automatic conversion when data types are inconsistent may also lead to calculation errors. Note that the symbols of the modulus result are the same as those of the divisor. In some special cases, the lazy evaluate method is used. 8. Do not divide the value by 0. check validity. 9. type overflow. Remember the value range of each data type to ensure that the data is within the defined type range; 10. The format of the mathematical expression is incorrect. The following are common examples: (1) obfuscation between mathematics and C language computation expressions (for example, = indicates a value assignment, and = indicates equal relations in mathematics ). (2) The priority of the operation is ignored. The best way to solve this problem is to write a mathematical expression not from left to right, but in the order of priority. After writing an expression with a higher priority, add () and then write the expression at the next level. For example: when calculating the area of the trapezoid, it must be s = (a + B) * h)/2, not 1/2 * a + B * h. (3) the automatic conversion during calculation and assignment is ignored. For example, float half = 1/2; in this way, because the result of dividing the right side of = is an integer 0, 0.5 is not stored in half, which affects the calculation result below. If you don't want to keep your head off here, when calculating different types of data, be sure to pay attention to whether automatic conversion will cause errors. We recommend that you add force conversion. (4) the left side of the assignment number is not a variable. For example, if # define PI 3.14 exists, PI = 3.14159 appears again in the program. For example, f (n) = f (n-1) * n (this is a typical mathematical language. The product on the right side of C cannot be properly stored, and the left side is a function call ). 11. I forgot to add # include before using the library function <?. H> 2. Process Control 1. Discard the statement... the remaining full text>

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.