有人可以告诉我我的代码有什么问题吗? [英] Can someone please tell me what's wrong with my code?

查看:62
本文介绍了有人可以告诉我我的代码有什么问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Dev C ++ 5.5.3并且下面的代码编译得很好但是一旦我输入输入,我的程序的.exe崩溃。



< pre lang =xml> #include < stdio.h >
#include < stdlib.h >
#include < string.h >

main()
{
int length;
char * strng,* strng2;

printf(\ n请输入一个没有空格的简单短语:); //要求输入
得到(strng); //输入
length = strlen(strng); //获取输入字符串的长度减去空终止符
strng2 =(char *)malloc(length + 1); //在strng2内存块中分配完整字符串所需的空间,包括空终止符
strng--;

while(length!= 0)//按字符填充新数组,旧数组的字符反向排列
{
* strng2 = * strng;
strng--;
strng2 ++;
length--;
}

printf(\ n以下是您向后输入的短语:%s \ n,strng2); //显示新字符串

free(strng2);
}

解决方案

您想要一份清单吗?



查看scanf的作用: http://www.cplusplus.com/reference/cstdio/scanf/ [ ^ ]



从stdin读取数据并根据参数格式将它们存储到附加参数指向的位置。

这意味着男人你叫它:

 length = scanf( %s ,& strng); 

strng 参数必须包含可放入字符的内存区域的地址。

你的不是:

  char  * strng; 

你没有给它一个值,所以它包含一个随机值,可能是也可能不是一个内存位置,但即使它不是你可以的使用...



对于快速解决方案,试试这个:

  char  strng [ 80 ]; 

printf( \ n请输入一个没有空格的简单短语:);
length = scanf( %s,strng);

并从那里开始工作 - 最好不要将指针移动到起始地址...


除了解决方案1之外,还有一些错误:



1. scanf 返回填充的项目数,在您的示例中最多为1.如果您想要输入字符串的长度,请检查 scanf 确实返回1,然后使用 strlen()检查字符串的长度。



2.您无法在堆栈上定义具有可变长度的字符串:

  char  strng2 [k]; 



在编译时,k的值是未知的,因此编译器会发出错误!如果要保留大小合适的内存,则必须使用 malloc calloc 。另外,如上所述,您必须使用正确的长度。而且,此外,您必须在长度上加1,因为C中的字符串以0字节终止,您还必须为该字节分配内存!



3.你无法修改数组变量!你实际上设法在几行中添加了几个错误:

 strng = strng + i; 



a) strng 被定义为一个数组。数组是内存中的静态范围。该范围的位置是固定的,无法更改。因此,无法更改数组变量。编译器应该在这里发出至少一个,可能是几个错误消息!

b) i 从未初始化。这行会产生未定义的结果,如果它甚至可以编译!

c)下面的代码一次修改了太多的计数器,以及你正在查看的字符串的起始地址(如果它会甚至工作)。我不打算解释为什么你的数学错了,因为它总是基于不稳定的代码...

提示:不要使用任何计数器!只需使用指向源和目标字符串中的位置的指针(并为此目的使用单独的指针变量)


如果您仍然感兴趣,那么代码的实际错误就是这一行

 * strng2 = * strng; 



仔细考虑它的作用并查看它的位置。


I'm using Dev C++ 5.5.3 and the code below compiles just fine but as soon as I enter input, my program's .exe crashes.

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

main()
{
    int length;
    char *strng, *strng2;

    printf("\nPlease enter a simple phrase with no spaces: "); // ask for input
    gets(strng); // take input
    length = strlen(strng); // get length of input string minus the null terminator
    strng2 = (char*)malloc(length + 1); // allocate in the strng2 memory block the space needed for the complete string, including the null terminator
    strng--;

    while(length != 0) // fill in character by character the new array with the old array's characters in reverse order
    {
        *strng2 = *strng;
        strng--;
        strng2++;
        length--;
    }

    printf("\nThe following is the phrase you entered backward: %s\n", strng2); // displays new string

    free(strng2);
}

解决方案

Do you want a list?

Look at what scanf does: http://www.cplusplus.com/reference/cstdio/scanf/[^]

"Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments."
Which means that men you call it:

length = scanf("%s", &strng);

the strng parameter must contain the address of an area of memory into which characters can be put.
Your's doesn't:

char *strng;

You haven't given it a value, so it contains a "random" value which may or may not be a memory location, but even if it is won't be one you can use...

For a "quick" solution, try this:

char strng[80];

printf("\nPlease enter a simple phrase with no spaces: ");
length = scanf("%s", strng);

And work from there - preferably without moving the pointer to the start address...


In addition to solution 1, some more errors:

1. scanf returns the number of items filled, which in your example is at best 1. If you want the length of the input string, check that scanf does return 1, then check the length of the string using strlen().

2. You can't define a string on the stack with a variable length:

char strng2[k];


At the time of compilation, the value of k is not known, therefore the compiler will issue an error! If you want to reserve memory of just the right size, you must allocate that memory dynamically, on the heap, using malloc or calloc. Also, as pointed out above, you must use the correct length. And, in addition, you must add 1 to the length, because strings in C are terminated with a 0-Byte, and you must allocate memory for that byte as well!

3. you can't modify an array variable! You have actually managed to add several errors in just a few lines:

strng = strng + i;


a) strng is defined as an array. An array is a static range within memory. The location of that range is fixed and cannot be changed. Therefore the array variable cannot be changed. The compiler should issue at least one, possibly several error messages here!
b) i was never initialized. This line would produce undefined results, if it would even compile!
c) the following code modifies too many counters at once, as well as the starting address of the string you're looking at (if it would even work). I'm not going to bother to explain why your math is wrong, since it's based on erratic code anyway...
Hint: don't use any counters at all! Just use pointers to positions within the source and destination string (and do use separate pointer variables for that purpose)


The actual error with your code if you are still interested is this line

*strng2 = *strng;


Think very carefully about what that does and look where it is.


这篇关于有人可以告诉我我的代码有什么问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆