C 中的简单字符串运行时错误?

发布于 2024-08-01 18:07:21 字数 265 浏览 2 评论 0原文

这段代码编译得很好,但运行时出现分段错误错误? 谁能告诉我为什么吗?

#include <stdio.h>
#include <string.h>
#include <math.h>

int main() {
    const char s2[] = "asdfasdf";
    char* s1;

    strcpy(s1, s2);
    printf("%s", s1);

    return 0;
}

This code compiles fine but give segmentation fault error while running? Can anyone tell why?

#include <stdio.h>
#include <string.h>
#include <math.h>

int main() {
    const char s2[] = "asdfasdf";
    char* s1;

    strcpy(s1, s2);
    printf("%s", s1);

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

天生の放荡 2024-08-08 18:07:21

您没有为 s1 分配内存。 您有一个指向 s1 的指针,但没有为 strcpy 分配内存来将 s2 的值复制到其中。

char *s1 = malloc( strlen(s2) + 1 );

strcpy( s1, s2 );

You didn't allocate memory for s1. You have a pointer to s1 but no memory allocated for the strcpy to copy the value of s2 into.

char *s1 = malloc( strlen(s2) + 1 );

strcpy( s1, s2 );
再可℃爱ぅ一点好了 2024-08-08 18:07:21

您尚未为 s1 分配任何内存。 它是一个空无一物的指针。

char* s1 = malloc(sizeof(s2));
strcpy(s1, s2);
printf("%s", s1);
free(s1);

You have not allocated any memory to s1. It is a pointer to nothing.

char* s1 = malloc(sizeof(s2));
strcpy(s1, s2);
printf("%s", s1);
free(s1);
情释 2024-08-08 18:07:21

问题是 s1 没有任何与之关联的内存。 strcpy 不会调用 malloc()

您可以这样做:

char s1[10];

char *s1 = malloc(10);

The problem is that s1 does not have any memory associated with it. strcpy does not call malloc().

You could either do:

char s1[10];

or

char *s1 = malloc(10);

冰雪梦之恋 2024-08-08 18:07:21

您为单个指针 s1 分配了空间,但没有为s1 指向的字节分配空间。

解决方案是为 s1 动态分配内存:

s1 = (char *)malloc(strlen(s2) + 1);
strcpy(s1, s2);

请记住,您需要比数字多分配一个字节的内存(调用 malloc 时的 +1) s2 中的字符数,因为末尾有一个隐式的 NULL 字节。

有关详细信息,请参阅 C 内存管理(堆栈溢出)

You allocated space for a single pointer, s1, but not the bytes pointed at by s1.

A solution is to dynamically allocate memory for s1:

s1 = (char *)malloc(strlen(s2) + 1);
strcpy(s1, s2);

Keep in mind that you need to allocate one more byte of memory (the +1 in the call to malloc) than the number of characters in s2 because there is an implicit NULL byte at the end.

See C Memory Management (Stack Overflow) for more information.

翻了热茶 2024-08-08 18:07:21

他们都说,你需要为 s1 分配空间。 其他人发布的内容都可以正常工作,但是,如果您想要一种更简单的方法来为现有字符串分配空间并将其复制到新指针中,请像这样使用 strdup :

#include <stdio.h>
#include <string.h>
#include <math.h>

using namespace std;

int main() {
    const char s2[] = "asdfasdf";
    char* s1;

    s1 = strdup(s2);
    printf("%s", s1);

    return 0;
}

有人之前提到过 strdup,这将是一种使用方法它。 大多数系统应该支持它,因为它位于标准 C 库中。 但显然有些人不这么认为。 因此,如果它返回错误,请使用已经提到的方法编写自己的错误,或者仅使用已经提到的方法;)

What they all said, you need to allocate the space for s1. What everyone else has posted will work just fine, however, if you want a simpler way to allocate space for an existing string and copy it into a new pointer then use strdup like this:

#include <stdio.h>
#include <string.h>
#include <math.h>

using namespace std;

int main() {
    const char s2[] = "asdfasdf";
    char* s1;

    s1 = strdup(s2);
    printf("%s", s1);

    return 0;
}

Someone mentioned strdup earlier, that would be a way to use it. Most systems should support it since it is in the standard C libaries. But apparently some don't. So if it returns an error either write your own using the method already mentioned, or just use the method already mentioned ;)

很糊涂小朋友 2024-08-08 18:07:21

目前还没有人指出 strdup(字符串重复)解决这一问题的潜力。

#include <stdio.h>
#include <string.h>
#include <math.h>

using namespace std;

int main() {
    const char s2[] = "asdfasdf";
    char* s1;

    s1 = strdup(s2);  // Allocates memory, must be freed later.
    printf("%s", s1);

    free(s1);         // Allocated in strdup, 2 lines above
    return 0;
}

No one yet has pointed out the potential of strdup (String Duplicate) to address this problem.

#include <stdio.h>
#include <string.h>
#include <math.h>

using namespace std;

int main() {
    const char s2[] = "asdfasdf";
    char* s1;

    s1 = strdup(s2);  // Allocates memory, must be freed later.
    printf("%s", s1);

    free(s1);         // Allocated in strdup, 2 lines above
    return 0;
}
半城柳色半声笛 2024-08-08 18:07:21

您需要分配目标(并且 using namespace std; 不是 C,而是 C++,其余代码是 C)。

You need to allocate the destination (and using namespace std; isn't C but C++, the rest of the code is C).

淡紫姑娘! 2024-08-08 18:07:21

您必须为指针 s1 分配内存。 如果你不这样做,它将指向未知的地方,从而出现分段错误。 正确的代码应该是:

#include <stdio.h>
#include <string.h>
#include <math.h>

int main() {
    const char s2[] = "asdfasdf";
    char* s1 = malloc(21 * sizeof(s2[0]));
    strcpy(s1,s2);
    printf("%s",s1);
    return 0;
}

You have to allocate memory to the pointer s1. If you don't do that, it will be pointing somewhere unknown, and thus arriving at the segmentation fault. The correct code should be:

#include <stdio.h>
#include <string.h>
#include <math.h>

int main() {
    const char s2[] = "asdfasdf";
    char* s1 = malloc(21 * sizeof(s2[0]));
    strcpy(s1,s2);
    printf("%s",s1);
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文