程序崩溃,指针试图使 strcpy 类似
这是我今天的第二个问题,指针让我做噩梦。 我正在尝试制作一个与 strcpy() 函数执行相同操作的程序。 一旦我尝试它..它崩溃了,我 100% 确定这是我的代码中的指针问题。我想是因为有某种未初始化的指针(*复制)..但是我已经为它分配了 NULL ...所以有人能告诉我 Null 分配的确切用途吗?因为我认为我误解了它的用途。请告诉我可以进行哪些修改才能使程序正常运行。
#include <iostream>
using namespace std;
void mycpy(char *b , char *a);
int main()
{
char *original = "this is a text" ;
char *copied = 0 ;
mycpy(copied , original);
for(int i = 0 ; *(copied+i) ;i++) cout << *(copied+i) ;
return 0;
}
void mycpy(char *b , char *a){
for(int i = 0 ; *(a+i) ; i++) *(b+i) = *(a+i);
}
提前致谢 。
This is my second problem today, pointers are giving me nightmares .
I'm trying to make a program that do the same thing that strcpy() function do..
Once i try it..it crashes and i'm 100% sure that's a pointers issue in my code. I think because there is some sort of an unintiallized pointer(*copied) ..But i've assigned NULL to it ...so can anybody tell me what's Null assignment is exactly for ? because i think i misunderstand its use. and tell me please what corrections can be made to the program to run normally .
#include <iostream>
using namespace std;
void mycpy(char *b , char *a);
int main()
{
char *original = "this is a text" ;
char *copied = 0 ;
mycpy(copied , original);
for(int i = 0 ; *(copied+i) ;i++) cout << *(copied+i) ;
return 0;
}
void mycpy(char *b , char *a){
for(int i = 0 ; *(a+i) ; i++) *(b+i) = *(a+i);
}
Thanks in advance .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,你的
mycpy
几乎是正确的(尽管你可以使用括号而不是算术,即a[i]
而不是*(a+1)< /代码>)。为了正确打印复制的字符串,最后一个字符必须为零,但最后一个字符不会被您的函数复制。所以它应该像
此外,变量
没有指向有效的内存,所以你从内存位置 0 读取,这是非法的。您可以将变量定义为数组
Well, your
mycpy
is almost right (although you could've used brackets instead of arithmetic, i.e.a[i]
instead of*(a+1)
). In order to print the copied string correctly, the last character must be zero, but the last one is not copied by your function. So it should rather be likeFurthermore the variable
doesn't point to valid memory, so you're reading from memory position 0 which is illegal. You could define the variable as array
您必须为复制操作的结果分配一些内存。
在您的情况下,
copied
尚未初始化,因此您尝试写入空指针。以下内容将为您分配足够的内存,将original
复制到copied
You have to allocate some memory for the result of the copy operation.
In your case
copied
hasn't been initialised, so you're trying to write to a null pointer. The following will allocate enough memory for you to copyoriginal
intocopied
你有指针,但它们没有指向任何内存。您需要分配一些内存才能使其工作。
那只是一个指向 char 类型内存的指针。您不能将其设置为“这是文本”,因为它只是一个指针。它没有任何空间来存储“这是文本”。
会起作用,或者
这两种方法都会占用 15 个字节来存储文本。第一个示例使用堆栈中的 15 个字节。第二个示例使用堆中的 15 个字节。
You have pointers, but they aren't pointing to any memory. You need to allocate some memory for this to work.
That is just a pointer to some memory that is of type char. You can't set that to "this is a text" because it is just a pointer. It doesn't have any space to store "this is a text".
will work, or
Both of these methods grab 15 bytes for you to store your text. The first example uses 15 bytes from the stack. The second example uses 15 bytes from the heap.
固定的:
Fixed:
在这一行中:
b+i
的取消引用导致了错误。您正在取消引用0
(NULL),这是数字 1 指针错误。In this line:
it's the dereferencing of
b+i
that causes the error. You're dereferencing0
(NULL), which is the number 1 pointer error.