使用函数中的指针反转字符串,main中的输出乱码 [英] Reverse a string using pointers in a function, output in main is garbled

查看:140
本文介绍了使用函数中的指针反转字符串,main中的输出乱码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 C斯蒂芬·普拉塔(Stephen Prata)的Primer Plus 和本章最后的练习之一是编写一个函数,该函数用反向的字符串替换字符串的内容."这是一章有关字符串的章节,其中包含大量的指针.我正在尝试尽可能多地使用指针,以使我更好地理解,但我被卡住了.

I'm trying to self-study C using C Primer Plus from Stephen Prata and one of the end-of-chapter exercises is to "Write a function that replaces the contents of a string with the string reversed.". This is a chapter on character strings with a good dose of pointers. I'm trying to use pointers as much as possible so I can better understand, but I'm stuck.

我的问题是,当我在main中打印返回指针的值时,它会出现乱码.

My problem is that when I print the value of the return pointer in main, it is garbled.

当我使用gdb时(也只是学习如何使用它),我可以看到我的函数返回的内存地址与该函数中使用的地址相同,并且已经分配给我的指针了据我所知.

When I use gdb(just learning how to use that too), I can see that the memory address returned from my function is the same address that was used in the function and it's getting assigned to my pointer in main okay as far as I can tell.

我尝试了很多事情,我想念什么? FWIW我还没有在书中了解malloc,尽管我经常在试图更好地理解C的各种www页面上看到它.

I've tried so many things, what am I missing? FWIW I have not learned about malloc yet in the book, though I see it referenced on various www pages I've frequented trying to better understand C.


$ cc -o exercise8 exercise8.c && ./exercise8
This is s1 before: abcd
This is s2 in function: dcba
This is s3 after: d`!


/*   A function that replaces the contents of a string with the string reversed. */
#include <stdio.h>
#include <string.h>
char *str_rev(char * string);
int main(void)
{
   char * s1  = "abcd";
   char * s3;
   printf("This is s1 before: %s\n", s1);

   s3 = str_rev(s1);
   printf("This is s3 after: %s\n", s3);

}

char *str_rev(char * string)
{
   char ar3[5];
   char * s2;
   int len = 0;

   s2 = ar3;

   len = (strlen(string) - 1);
   string = string + len;

   while ( len >= 0 )
   {
      *s2 = *string;
      len--;
      string--;
      s2++;
   }
   s2++;
   *s2 = 0;
   s2 = s2 - 5;

   printf("This is s2 in function: %s\n", s2);
   return s2;
}


$ gdb exercise8
GNU gdb (GDB) 7.1-ubuntu

Reading symbols from exercise8...done.
(gdb) break 12
Breakpoint 1 at 0x804844a: file exercise8.c, line 12.
(gdb) break 40
Breakpoint 2 at 0x80484d9: file exercise8.c, line 40.
(gdb) run
Starting program: exercise8
This is s1 before: abcd         // My original string.
This is s2 in function: dcba        // Good, my reversed string while in the function.

Breakpoint 2, str_rev (string=0xbffff043 "dcba") at exercise8.c:40
40               return s2;
(gdb) print s2
$1 = 0xbffff043 "dcba"          // Location of pointer s2.
(gdb) continue
Continuing.

Breakpoint 1, main () at exercise8.c:12
12               printf("This is s3 after: %s\n", s3);
(gdb) print s3
$2 = 0xbffff043 "dcba"          // Back in main same pointer as s2 from function.
(gdb) step
This is s3 after: d`Q           // Line 12 executed.  Output garbled.
14      }
(gdb)

推荐答案

char ar3[5];
char * s2 = ar3;

上面的代码将使s2指向堆栈上的一个字符串.函数完成后,该ar3变量将被删除.

The above code will make s2 points to a character string on the stack. This ar3 variable will be deleted once your function finishes.

您应该输出到一些已经预先分配的变量.对其进行如下修改

You should output to some variable that you have pre-allocated. Modify it as follow

int main(void)
{
   char * s1  = "abcd";
   char s3[5];
   printf("This is s1 before: %s\n", s1);

   str_rev(s1, s3);
   printf("This is s3 after: %s\n", s3);

}

void str_rev(char * string, char * s2)
{
   ........

   // don't return

   // Also assign the last character with the NULL terminator
   ar2[strlen(string)] = '\0';
}

当然,一旦进入有关malloc的章节,就可以根据s1的长度为s3分配必要的内存.在此之前,请继续阅读并获得乐趣.

Of course, once you get to the chapter regarding malloc, you can allocate the necessary memory for s3 depending on the length of s1. Until then, read on and have fun.

这篇关于使用函数中的指针反转字符串,main中的输出乱码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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