程序崩溃,指针试图使 strcpy 类似

发布于 2024-08-20 21:37:27 字数 601 浏览 11 评论 0原文

这是我今天的第二个问题,指针让我做噩梦。 我正在尝试制作一个与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

御弟哥哥 2024-08-27 21:37:27

好吧,你的 mycpy 几乎是正确的(尽管你可以使用括号而不是算术,即 a[i] 而不是 *(a+1)< /代码>)。为了正确打印复制的字符串,最后一个字符必须为零,但最后一个字符不会被您的函数复制。所以它应该像

void mycpy(char *b , char *a)
{
    int i;
    for(i = 0 ; *(a+i) ; i++) *(b+i) = *(a+i);
    *(b+i) = 0; // or "\0", doesn't matter    
}

此外,变量

char *copied = 0 ;

没有指向有效的内存,所以你从内存位置 0 读取,这是非法的。您可以将变量定义为数组

char copied[20];

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 like

void mycpy(char *b , char *a)
{
    int i;
    for(i = 0 ; *(a+i) ; i++) *(b+i) = *(a+i);
    *(b+i) = 0; // or "\0", doesn't matter    
}

Furthermore the variable

char *copied = 0 ;

doesn't point to valid memory, so you're reading from memory position 0 which is illegal. You could define the variable as array

char copied[20];
旧伤还要旧人安 2024-08-27 21:37:27

您必须为复制操作的结果分配一些内存。

在您的情况下,copied 尚未初始化,因此您尝试写入空指针。以下内容将为您分配足够的内存,将original复制到copied

char* copied = new char[strlen(original)+1];

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 copy original into copied

char* copied = new char[strlen(original)+1];
幸福不弃 2024-08-27 21:37:27

你有指针,但它们没有指向任何内存。您需要分配一些内存才能使其工作。

char *original;

那只是一个指向 char 类型内存的指针。您不能将其设置为“这是文本”,因为它只是一个指针。它没有任何空间来存储“这是文本”。

char original[ 15 ] = "this is a text";
char copied[ 15 ] = "this is a text";

会起作用,或者

char *original;
char * copied;

original = malloc( 15 );
copied = malloc( 15 );

mycpy( original, "this is a text" );
mycpy( copied, original );

这两种方法都会占用 15 个字节来存储文本。第一个示例使用堆栈中的 15 个字节。第二个示例使用堆中的 15 个字节。

You have pointers, but they aren't pointing to any memory. You need to allocate some memory for this to work.

char *original;

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".

char original[ 15 ] = "this is a text";
char copied[ 15 ] = "this is a text";

will work, or

char *original;
char * copied;

original = malloc( 15 );
copied = malloc( 15 );

mycpy( original, "this is a text" );
mycpy( copied, original );

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.

耶耶耶 2024-08-27 21:37:27

固定的:

 #include <iostream>
 using namespace std;

 void mycpy(char *b , char *a);

 int main()
 {

    char *original = "this is a text" ;
    char copied[30]; // you need to actualy allocate space 
    // (this is on stack, you could use new as well, for heap allocation)

    mycpy(copied , original);

    for(int i = 0 ; *(copied+i) ;i++) cout << *(copied+i) ;

    return 0;
}

void mycpy(char *b , char *a){
    int i =0;
    while (*(a+i)) {
        *(b+i) = *(a+i);
        ++i;
    }
    *(b+i) = '\0'; // null termination
}

Fixed:

 #include <iostream>
 using namespace std;

 void mycpy(char *b , char *a);

 int main()
 {

    char *original = "this is a text" ;
    char copied[30]; // you need to actualy allocate space 
    // (this is on stack, you could use new as well, for heap allocation)

    mycpy(copied , original);

    for(int i = 0 ; *(copied+i) ;i++) cout << *(copied+i) ;

    return 0;
}

void mycpy(char *b , char *a){
    int i =0;
    while (*(a+i)) {
        *(b+i) = *(a+i);
        ++i;
    }
    *(b+i) = '\0'; // null termination
}
满意归宿 2024-08-27 21:37:27

在这一行中:

for(int i = 0 ; *(a+i) ; i++) *(b+i) = *(a+i);

b+i 的取消引用导致了错误。您正在取消引用 0 (NULL),这是数字 1 指针错误。

In this line:

for(int i = 0 ; *(a+i) ; i++) *(b+i) = *(a+i);

it's the dereferencing of b+i that causes the error. You're dereferencing 0 (NULL), which is the number 1 pointer error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文