gpt4 book ai didi

c - 两个代码在单独的程序中工作正常但是当代码在同一个程序中时程序崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 07:33:05 25 4
gpt4 key购买 nike

嘿,这段代码可以正常工作。该代码是关于获取名称输入并对其进行一些更改。

#include <stdio.h>
#include <stdlib.h>

/*
* An example of how to use strtol() to read a number
* and validate that one was entered correctly.
*
*/

int main(void)
{
char buf[BUFSIZ];
char *p;
long int i;

printf ("Enter a number: ");

if (fgets(buf, sizeof(buf), stdin) != NULL)
{
i = strtol(buf, &p, 10);

/*
* If the first character of the buffer is \n, the user
* pressed [Enter] with entering any text at all, which
* is therefore invalid.
*
* The pointer p has been updated by strtol() to point to
* the first invalid character after the number.
* If this character is \0 it means we reached the end of
* the array successfully, so we received a good number.
* If this character is \n it also means we reached the
* end of the input successfully. This is a symptom of
* using fgets() to obtain the string, but does not
* represent a problem.
* If this character is anything else, it means there was
* some additional characters entered after the number.
* In this sample program, I have deemed this situation
* to be invalid, however, in your program it may be
* valid, depending on what you're expecting from the user.
*
*/

if (buf[0] != '\n' && (*p == '\n' || *p == '\0'))
printf ("Valid number of %ld entered\n", i);
else printf ("Invalid number entered\n");
}

return(0);
}

此代码也有效!它接受一个字符串并将其转换为一个整数!

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int m;
char name[m],rename[m],c=0,j=0;
puts("enter your name\n");
gets(name);
m=strlen(name);
if(m>20){
while(m>20){
puts("shorter name pls\n");
gets(name);
m=strlen(name);
}
}
while(name[c]==' '){
c++;}
while(c<strlen(name)){
if(name[c]==' '&& name[c+1]==' ')
{c++;}
else{
rename[j]=name[c];
j++;
c++;}
}
rename[j]='\0';
puts(rename);
return 0;
}

但是在同一个程序中同时输入它们时,程序崩溃了!

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int m;
char name[m],rename[m],c=0,j=0;
puts("enter your name\n");
gets(name);
m=strlen(name);
if(m>20){
while(m>20){
puts("shorter name pls\n");
gets(name);
m=strlen(name);
}
}
while(name[c]==' '){
c++;}
while(c<strlen(name)){
if(name[c]==' '&& name[c+1]==' ')
{c++;}
else{
rename[j]=name[c];
j++;
c++;}
}
rename[j]='\0';
puts(rename);
char buf[BUFSIZ];
char *p;
long int i;

printf ("Enter a number: ");

if (fgets(buf, sizeof(buf), stdin) != NULL)
{
i = strtol(buf, &p, 10);

/*
* If the first character of the buffer is \n, the user
* pressed [Enter] with entering any text at all, which
* is therefore invalid.
*
* The pointer p has been updated by strtol() to point to
* the first invalid character after the number.
* If this character is \0 it means we reached the end of
* the array successfully, so we received a good number.
* If this character is \n it also means we reached the
* end of the input successfully. This is a symptom of
* using fgets() to obtain the string, but does not
* represent a problem.
* If this character is anything else, it means there was
* some additional characters entered after the number.
* In this sample program, I have deemed this situation
* to be invalid, however, in your program it may be
* valid, depending on what you're expecting from the user.
*
*/

if (buf[0] != '\n' && (*p == '\n' || *p == '\0'))
printf ("Valid number of %ld entered\n", i);
else printf ("Invalid number entered\n");
}
return 0;
}

为什么??

最佳答案

当然这是非常错误的:

int m;
char name[m],rename[m],c=0,j=0;

您正在声明长度为未初始化变量的数组。

关于c - 两个代码在单独的程序中工作正常但是当代码在同一个程序中时程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11583025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com