我正在创建一个xstrcpy函数(我自己的strcpy),但我没有使用for方法。请帮我在代码中找到错误。 [英] I am making a xstrcpy function (strcpy of my own), but I am not using the for method. Please help me find the error in my code.

查看:85
本文介绍了我正在创建一个xstrcpy函数(我自己的strcpy),但我没有使用for方法。请帮我在代码中找到错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include   <   stdio.h  >  
void xstrcpy( char * p, char * q);
main()
{
char word1 [ 25 ], word2 [ 25 ];
puts( 输入单词);
得到(word1);
xstrcpy(word1,word2);
printf( 这是你的word1:%s \ n,这是word2:%s \ n,word1 [ 25 ],word2 [ 25 ]);
}
void xstrcpy( char * p, char * q)
{
q = p;
}





我的尝试:



基本上我分配指针变量q,p中的值使两个字符串的基地址变得相同。由于字符串存储在连续的内存位置,因此其他地址也应该更改。但输出不支持。

解决方案

有三个错误。



首先是a strcpy 实现必须复制数据本身。您没有复制数据,而是将一个指针指向另一个。虽然这是一个有效的操作,但它不会给你预期的结果。



第二个错误是你使用作为函数参数传递的局部变量来执行赋值。这些是指向单词字符串的指针的副本,因此不会更改它们。你在做什么相当于:

  char  * p = word1; 
char * q = word2;
q = p;



如果您现在打印 q word1 将被打印,但单词本身的内容不变。



第三个错误出现在你的 printf 您传递字符而不是字符串并且具有越界访问权限的语句。使用 word1 [25] 访问字符串中第25位的字符。但 printf%s 格式参数需要一个指向charcaters的指针,而你的字符串只有25个字符宽,因此索引25无效(索引为零,因此有效的来自零到大小-1,在你的情况下为24)。它应该是:

 printf(这是你的word1:%s \ n,这是word2:%s \ n,word1,word2); 





您应该再次阅读有关C指针的课程笔记或书籍。或者参见指针 - C ++教程 [ ^ ](不要被C ++激怒,这里描述的基础也适用于palin C。)


#include<stdio.h>
void xstrcpy(char *p, char *q);
main()
{
	char word1[25], word2[25];
	puts("Enter a word");
	gets(word1);
	xstrcpy(word1,word2);
	printf("This is your word1: %s\n and this is word2: %s\n",word1[25],word2[25]);
}
void xstrcpy(char *p, char *q)
{
	q=p;
}



What I have tried:

Basically I am assigning the pointer variable q, the value in p so that the base addresses of the two strings become same. Since strings are stored in contiguous memory locations, the other addresses should change too. But the output is not supportive.

解决方案

There are three errors.

The first is that a strcpy implementation must copy the data itself. You are not copying the data but assigning one pointer to another. While this is a valid operation, it will not give you the expected results.

The second error is that you perform the assignment using local variables passed as function parameters. These are copies of the pointers to your word strings and will therefore not change them. What you are doing is equivalent to:

char *p = word1;
char *q = word2;
q = p;


If you would print q now, the content of word1 would be printed but the content of the words itself is unchanged.

The third error is in your printf statement where you pass characters instead of strings and have out of bound access. Using word1[25] accesses the character at position 25 in the string. But the printf %s format parameter expects a pointer to charcaters and your strings are only 25 characters wide so that index 25 is invalid (indexes are zero based so that valid ones are from zero to size-1 which is 24 in your case). It should be:

printf("This is your word1: %s\n and this is word2: %s\n",word1,word2);



You should read your course notes or books about C pointers again. Or see for example Pointers - C++ Tutorials[^] (don't be irritated by the C++, the basics described there apply to palin C too).


这篇关于我正在创建一个xstrcpy函数(我自己的strcpy),但我没有使用for方法。请帮我在代码中找到错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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