C ++怀疑:我已经颠倒了一个字符数组。但是我显示了一些垃圾值。 [英] C++ doubt : I have reversed a character array. But I am getting some junk values displayed.

查看:72
本文介绍了C ++怀疑:我已经颠倒了一个字符数组。但是我显示了一些垃圾值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..

我已经颠倒了一个字符数组。但是我显示了一些垃圾值。

对不起,如果这似乎是一个愚蠢的怀疑..我刚刚开始使用c ++ ...

PFB我写的代码反转字符数组。



Hi..
I have reversed a character array. But I am getting some junk values displayed.
Sorry if this seems to be a dumb doubt..i am just starting c++...
PFB the code i have written to reverse a character array.

#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<string.h>

using namespace std;

int main()
{
   
    char a[10],b[10];
    int i,j,k,z;
    cout<<"enter a=";
    gets(a);
    cout<<"the array is-"<<a<<endl;
    i = strlen(a);
    k=i-1;
    for(j=0;j<i;j++)
    {

        b[j]=a[k];
        k--;
    }
    b[j]='/0';
    cout<<"reversed-"<<b<<endl
    return 0;
}





收到的输出如下:

数组是 - manc

反转 - cnam0'?& 9manc

尽可能看到数组是颠倒的,因为cnam是反转的字符,但后面是垃圾值。

这些垃圾值的原因是什么?如何避免它们?



我尝试过:



我能够通过简单地使用for循环显示我需要的东西。但我想知道为什么显示垃圾值及其意义。



Output received is as follows:
the array is-manc
reversed-cnam0'?&9manc
As you could see the array is reversed as cnam was the reversed characters but it is followed by junk values.
What is the reason for these junk values? How to avoid them?

What I have tried:

I was able to display what i needed by simply using a for loop. But I wanted to know why the junk values are displayed and what is its significance.

推荐答案

我看到两个问题:两者都应该阻止代码编译。 />
1)null是反斜杠零,而不是斜线零:
Two problems I see: both of which should prevent the code compiling.
1) null is backslash zero, not slash zero:
b[j]='/0';

应为:

b[j]='\0';



2)你在这一行上缺少一个分号:


2) You are missing a semicolon on this line:

cout<<"reversed-"<<b<<endl

应该是:

cout<<"reversed-"<<b<<endl;



由于该代码无法编译 - 或者不应该在任何合理的编译器中 - 您获得的结果运行代码是无关紧要的,因为它不是您正在查看的代码!

修复这两个,它应该编译,并且正常工作。


Since that code doesn't compile - or shouldn't in any sensible compiler - the results you get from running your code is irrelevant, as it isn't the code you are looking at!
Fix those two, and it should compile, and work correctly.


有一个工具可以让你看到你的代码在做什么,它的名字是调试器。

它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪个期望与现实相符。



使用调试器,你会发现自己

There is a tool that allow you to see what your code is doing, its name is debugger.
It is also a great learning tool because it show you reality and you can see which expectation match reality.

With the debugger, you would have found yourself that
b[j]='/0';



没有达到你的预期。



当你穿上不知道你的代码在做什么或为什么它做它做的事情,答案是调试器

使用调试器来查看你的代码在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里显示你的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


was not doing what you expected.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于C ++怀疑:我已经颠倒了一个字符数组。但是我显示了一些垃圾值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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