为什么以下指针赋值失败? [英] Why do the following pointer assignment fail?

查看:109
本文介绍了为什么以下指针赋值失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在对K& R中的反向函数进行编码时,我试图将去引用的指针(s1)分配给另一个去引用的指针(s2)。我已经检查过(有很多printf和putchar)* s1确实包含了我想要的字符,但是在s2中指向字符变化的数组。为什么会这样?谢谢!



我尝试过:



In coding the reverse function in K&R, I tried to assign a de-referenced pointer (s1) to another de-referenced pointer (s2). I have checked (with a lot of printf and putchar) that *s1 indeed hold the character I want but in the array that s2 points to the character changes. Why does this happen? Thanks!

What I have tried:

void reverse(char *s1){
	char a[100];
	char *s2;
	int i=0;
	s2=&a[0];
	while(*s1!='\0'){
		s1++;
		i++;		
	}
	s1--;
	i--;
	while(i>=0){
		*s2=*s1;
	        s2++;
		s1--;
		i--;
	}		
}

推荐答案

如果你试试:

If you try it:
void reverse(char *s1){
	char a[100];
	char *s2;
	int i=0;
	s2=&a[0];
	while(*s1!='\0'){
		s1++;
		i++;		
	}
	s1--;
	i--;
	while(i>=0){
		*s2=*s1;
	        s2++;
		s1--;
		i--;
	}
	printf("%s\n", a);
}



int main()
{
    char s[] = "Hello World!";
    printf("%s\n", s);
    reverse(s);
    return 0;
}

然后你得到结果:

Then you get the result:

Hello World!                                                                                                                   
!dlroW olleH



但是...这可能更多的是运气而不是判断。问题是你没有将字符串终止字符\ 0从输入复制到输出,这很容易证明,通过预先填充具有可识别垃圾的数组:


But ... that's probably more by luck than by judgement. The problem is that you aren't copying the string termination character '\0' from your input to your output, as is easy to prove, by "pre filling" the array with identifiable rubbish:

char *s2;
	int i=0;
	for (i = 0; i < 100; i++)a[i] = 'X';
	i = 0;



现在当你打印时,你得到:


Now when you print you get:

Hello World!                                                                                                                   
!dlroW olleHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX����l7n��



,因为没有任何输出字符串告诉printf的时候停止!

要么在反向字符串的末尾设置一个空值,要么将a预先填充到所有空值。



我这样做的方式不同,只是一个循环:


Because there is nothing in the output string to tell printf when to stop!
Either set a null at the end of the reversed string, or prefill "a" to all nulls.

I'd do this differently though, with just a single loop:

int *inp = s1;
int *outp = &(a[99]);
do
   {
   copy from inp to outp
   if (inp was a null or you run out of space) exit the loop
   inc inp, dec outp
   } while (true);

然后直接从outp打印反向字符串



请注意,除非你返回反向字符串,否则这个函数是无用的 - 但是你可以;返回一个或任何一个指向它的一部分的指针 - 它在堆栈上会导致重大问题你做。您需要使用malloc来分配空间,而不是在堆栈上声明数组 - 然后安排释放它。将字符串和输出区域都传递给函数会更好。

You then print the reversed string directly from outp

Be aware that this function is useless unless you return the reversed string - but you can;t return a or any pointer to a part of it - it's on the stack and will cause major problems if you do. You would need to use malloc to allocate the space, not declare the array on the stack - and then arrange to free it. It would be a better idea to pass both the string and the output area to the function.


为了帮助您理解程序,使用调试器,它会准确地显示它是什么这样做。您可能必须切换到机器代码级别以查看C代码的编译方式。

-----

当您不理解代码的作用或为什么它做它做的,答案是调试器

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



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

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



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

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
To help you to understand the program, use the debugger, it will show you exactly what it is doing. You may have to switch to machine code level to see how C code is compiled.
-----
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[^]

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.


这篇关于为什么以下指针赋值失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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