| |

VerySource

 Forgot password?
 Register
Search
View: 723|Reply: 9

Why is the commented line wrong? ~~~

[Copy link]

7

Threads

15

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 Australia

Post time: 2020-1-5 19:40:02
| Show all posts |Read mode
The purpose of the program is to create two linked lists, merge them, and then arrange them in ascending order. They can run correctly, but the line of output statements isolated by comments does not know what is wrong ~~~
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define NAMELEN 10
#define NULL 0
#define LEN sizeof (struct data)
struct data
{
long num;
char name [NAMELEN];
struct data * next;
};
struct data * Crt (void)
{
int count = 1;
struct data * head, * fwd, * bwd;
head = fwd = (struct data *) malloc (LEN);
head-> num = -1;
do
{
bwd = fwd;
fwd = bwd-> next = (struct data *) malloc (LEN);
printf ("Stu% -4d\n", count ++);
printf ("Num =>");
scanf ("% ld",&fwd-> num);
printf ("Name =>");
scanf ("% s",&fwd-> name);
}
while (fwd-> num);
bwd-> next = NULL;
return (head);
}
void link (struct data * A_head, struct data * B_head)
{
struct data * fwd = A_head;
fwd = fwd-> next;
while (fwd-> next! = NULL)
fwd = fwd-> next;
fwd-> next = B_head-> next;
}
void ord (struct data * head)
{
struct data * fwd, * flw, * ord;
ord = fwd = flw = head;
fwd = fwd-> next;
while (ord-> next! = NULL)
{
if (fwd-> num <ord-> next-> num)
{
flw-> next = fwd-> next;
fwd-> next = ord-> next;
flw = ord-> next = fwd;
fwd = fwd-> next;
}
else
{
flw = flw-> next;
if (fwd-> next! = NULL)
fwd = fwd-> next;
else
{
ord = ord-> next;
fwd = ord-> next;
flw = ord;
}
}
}
}
void print (struct data * fwd)
{
int count = 1;
fwd = fwd-> next;
while (fwd! = NULL)
{
printf ("Stu% -4d", count ++);
/ * printf ("Num% -4d Name% s\n", fwd-> num, fwd-> name); * /
/ * Why is the above sentence wrong? It should be able to replace the following two PRINTF, (but the actual situation is that the number is displayed normally, NAME is displayed as NULL, the pointer is wrong?) * /
printf ("Num% -4d", fwd-> num);
printf ("Name% s\n", fwd-> name);
fwd = fwd-> next;
}
}
void main ()
{
void link (struct data * A_head, struct data * B_head);
    void ord (struct data * head);
    void print (struct data * fwd);
struct data * A_head, * B_head;
printf ("Input infomations of Group A students\n");
A_head = Crt ();
printf ("Input infomations of Group B students\n");
B_head = Crt ();
printf ("Now link Group A and Group B\n");
link (A_head, B_head);
printf ("Finally, order the new Group\n");
ord (A_head);
printf ("Now check the result\n");
print (A_head);
}
Reply

Use magic Report

1

Threads

27

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 China

Post time: 2020-1-6 12:54:01
| Show all posts
printf ("Num% -4d Name% s\n", fwd-> num, fwd-> name);
I feel wrong:
printf output multiple variables should be the same type, you can long, one char, you can not pull.
It is recommended that my brothers write some notes, it took me a long time to finish the program.
Reply

Use magic Report

1

Threads

27

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 China

Post time: 2020-1-6 13:06:01
| Show all posts
printf ("Num% -4d Name% s\n", fwd-> num, fwd-> name);
Only% s is effective,% -4d has no effect.
Reply

Use magic Report

0

Threads

25

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-1-6 16:33:02
| Show all posts
printf ("Num% -4d Name% s\n", fwd-> num, fwd-> name);
There is no problem in itself.
Reply

Use magic Report

7

Threads

15

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-1-6 16:48:01
| Show all posts
Thank you in advance on the second floor ~~ I forgot to add a comment because I was in a hurry, SORRY ~~
that. . . What is meant by "no problem in itself"?
Reply

Use magic Report

0

Threads

25

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-1-6 17:24:01
| Show all posts
After running your program for a while, the problem you said did not appear.
gosh!
Reply

Use magic Report

1

Threads

27

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 China

Post time: 2020-1-7 22:27:01
| Show all posts
I ran it and it was true.
Reply

Use magic Report

1

Threads

27

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 China

Post time: 2020-1-8 12:36:01
| Show all posts
printf ("Num% -4d Name% s\n", fwd-> num, fwd-> name);
The change from% s\n to% 10s\n should work, I tried
Reply

Use magic Report

1

Threads

27

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 China

Post time: 2020-1-8 13:09:01
| Show all posts
% S is only used for the specified string
Reply

Use magic Report

7

Threads

15

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-1-10 19:54:01
| Show all posts
Thank you for your help ~ Also, how to use a PRINTF to output a variety of different formats, such as% ld,% d,% c% s, etc.? . . . .
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list