Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我在使用malloc和字符串指针时遇到了分段错误_C_Pointers_Segmentation Fault_Malloc - Fatal编程技术网

我在使用malloc和字符串指针时遇到了分段错误

我在使用malloc和字符串指针时遇到了分段错误,c,pointers,segmentation-fault,malloc,C,Pointers,Segmentation Fault,Malloc,当我尝试使用malloc和字符串指针进行扫描时,它会导致分段错误 main(){ char *a; a = (char)malloc(50); printf("enter a string\t"); scanf("%s", a); printf("%s\n", a); } 这里,您的意思是将其类型转换为char*,而不是char 注意,最好不要强制转换malloc的返回类型。看 这里,您的意思是将其类型转换为char*,而不是char 注意,最好不要强制

当我尝试使用malloc和字符串指针进行扫描时,它会导致分段错误

main(){
    char *a;
    a = (char)malloc(50);
    printf("enter a string\t");
    scanf("%s", a);
    printf("%s\n", a);
}
这里,您的意思是将其类型转换为char*,而不是char

注意,最好不要强制转换malloc的返回类型。看

这里,您的意思是将其类型转换为char*,而不是char

注意,最好不要强制转换malloc的返回类型。请参见

除了使用scanf之外,您的主要问题是,您正在将32位或64位系统上大小分别为4或8的指针投射到一个字符,根据标准,该字符的大小保证为1。不要忽略编译器警告。你应该看到以下几点:

warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  a = (char) malloc(10);

warning: assignment makes pointer from integer without a cast [enabled by default]
  a = (char) malloc(50);
将指针类型强制转换为char,只将其分配给指针变量是没有意义的。如果您想绝对清楚地表明您正在分配足够的内存来容纳N个字符,那么您可以写:

但是一个字符的大小总是1,就像我之前说过的。无论如何:我开始修复你发布的代码:

如果输入的字符数不超过49个,则以下复制粘贴的代码可以正常工作:

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
    char *a = malloc(50);
    if (a == NULL) return EXIT_FAILURE;//<-- no memory available
    printf("enter a string\t");
    scanf("%s", a);
    printf("%s\n", a);
    free(a);//<== be tidy, free your memory, though here, it's a bit redundant
    return 0;
}
<>但是,C++中从不抛出Malc返回的指针是另一回事。 另外,为了安全起见,请检查空指针

最后:不要忽略编译器警告

您可以在页面的右下角提供stdin输入,它将打印您提供的字符串

除了使用scanf之外,您的主要问题是,您正在将32位或64位系统上大小分别为4或8的指针投射到一个字符,根据标准,该字符的大小保证为1。不要忽略编译器警告。你应该看到以下几点:

warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  a = (char) malloc(10);

warning: assignment makes pointer from integer without a cast [enabled by default]
  a = (char) malloc(50);
将指针类型强制转换为char,只将其分配给指针变量是没有意义的。如果您想绝对清楚地表明您正在分配足够的内存来容纳N个字符,那么您可以写:

但是一个字符的大小总是1,就像我之前说过的。无论如何:我开始修复你发布的代码:

如果输入的字符数不超过49个,则以下复制粘贴的代码可以正常工作:

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
    char *a = malloc(50);
    if (a == NULL) return EXIT_FAILURE;//<-- no memory available
    printf("enter a string\t");
    scanf("%s", a);
    printf("%s\n", a);
    free(a);//<== be tidy, free your memory, though here, it's a bit redundant
    return 0;
}
<>但是,C++中从不抛出Malc返回的指针是另一回事。 另外,为了安全起见,请检查空指针

最后:不要忽略编译器警告


您可以在页面右下角提供标准输入,它将打印您提供的字符串

1。指定main、2的返回类型。指定参数main void 3。卸下石膏!4.返回0;应该是main 5中的最后一个语句。考虑使用堆栈,而不是堆的东西,Trviali尝试了所有这些,并且仍然在进行分割。fault@user3248186Elias Van Ootegem的提示只是提高你的C的技巧。它们与你的问题无关。@pzaenger:如果OP移除了施法,并且输入少于50个字符,它确实解决了问题,代码工作我自己试过了。。。到OP:按照我的advise@EliasVanOotegem:哦,我明白了,你是对的。我没见过,那是一个错误的演员。对不起:1。指定main、2的返回类型。指定参数main void 3。卸下石膏!4.返回0;应该是main 5中的最后一个语句。考虑使用堆栈,而不是堆的东西,Trviali尝试了所有这些,并且仍然在进行分割。fault@user3248186Elias Van Ootegem的提示只是提高你的C的技巧。它们与你的问题无关。@pzaenger:如果OP移除了施法,并且输入少于50个字符,它确实解决了问题,代码工作我自己试过了。。。到OP:按照我的advise@EliasVanOotegem:哦,我明白了,你是对的。我没见过,那是一个错误的演员。抱歉:事实上这并不好:你只是不投malloc的结果。然而,OP应该在你的链接中阅读被接受的答案。事实上,这并不是更好的:你只是不投malloc的结果。但是,OP应该阅读给定链接中的已接受答案。