| |

VerySource

 Forgot password?
 Register
Search
View: 913|Reply: 6

The confusion of puts()

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-9-28 19:00:01
| Show all posts |Read mode
#include<stdio.h>
char *read(char *p,int n);
int main(void)
{
char s[10];
char *p;
int q;
p=read(s,10);
   return 0;
}

char *read(char *p,int n)
{
int m=n;
   
while(n--)
{p[m-n]=getchar();
printf ("%c",p[m-n]);}//print 1
printf ("\n");
puts(p);//print 2

          return p;
}
For such a function, there is no problem with printing 1 sentence, why is there a problem with puts(), and it displays garbled characters? Does puts() must be used with gets()?
Reply

Use magic Report

2

Threads

20

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 Cambodia

Post time: 2020-9-28 22:45:01
| Show all posts
C stipulates that the end of the string is '\0', and all the elements of the string array in your program are characters input by getchar. If the system cannot find '\0', it will consider the string to be unfinished, so there is Garbled. You can check whether the first few characters of garbled are correct.
Reply

Use magic Report

0

Threads

63

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 China

Post time: 2020-9-29 07:30:01
| Show all posts
memset(s, 0, sizeof(s)) before read();
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-9-29 07:45:01
| Show all posts
Will it work?
#include<stdio.h>
#include<stdlib.h>
char *read(char *p,int n);
int main(void)
{
char s[10];
char *p;
// int q;
p=read(s,10);
puts(p);
         printf ("\n");
         system( "pause" );
         return 0;
}

char *read(char *p,int n)
{
// int m=n;
         char *q=p;
while(n--)
{
// p[m-n]=getchar();
*p = getchar();
p++;
// printf ("%c",p[m-n]); //print 1
}
*p = '\0';
// puts(p);
// printf ("\n");
// puts(p); //print 2

   return q;
}
Reply

Use magic Report

2

Threads

20

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-9-29 09:30:01
| Show all posts
No, p is the first address of the array, how can you make p just ++?
As long as in your first program, let p[9] = '\0' in the read function.
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-9-30 00:00:01
| Show all posts
No, p is the first address of the array, how can you make p just ++?
_______________________________________________________________
See it clearly..

As long as in your first program, let p[9] = '\0' in the read function.
________________________________________________________________
Run first and enter 10 characters to see the result..
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-9-30 08:00:01
| Show all posts
#include<stdio.h>
char *read(char *p,int n);
int main(void)
{
char s[11];
s[10]='\0';//My LCC does not support direct definition. . trouble. .
           //PS: I think undefined is a null character. . Mistaken. . Sorry
char *p;
p=read(s,10);
puts(p);
   return 0;
}

char *read(char *p,int n)
{
int m=n;

while(n--)
{p[m-n-1]=getchar();}//It should be m-n-1
     //printf ("%c",p[m-n]);}//print 1
//printf ("\n");
//puts(p);//print 2

          return p;
}
Two errors. . The modification is OK. .
Thank you all
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