进程返回-1073741819 <0xC0000005>

时间:2019-01-27 11:40:08

标签: c windows

我是编码和学习c语言的初学者,这是我的第一步。 当我使用字符串函数时,程序返回的值不显示输出。使用minGW作为编译器

我尝试添加string.h标头文件夹 串 从下面跟随代码

''''
#include <stdio.h>
#include <conio.h>

int main()
{
    /*
    int strlen(string);
    */
    char name = { 's','i','l','a','m','\0' };

    int length;

    length = strlen(name);
    printf("the length of %s is %d\n", name, length);

    getch();
    return 0;
}
'''

代码在这里结束

预期打印字符名称的长度,但是崩溃 如在构建日志中 “进程终止,状态为-1073741819” 在构建消息中 警告:传递'strlen'的参数1会使指针从整数开始而不进行强制转换[-Wint-conversion] | 注意:预期为'const char *',但参数的类型为'char'|

感谢您的关注

2 个答案:

答案 0 :(得分:2)

您将name声明为char,但仍将其视为数组。要将name声明为char数组,请使用:

char name[] = { 's','i','l','a','m','\0' };

此外,由于您引用了函数strlen(),因此必须添加头文件:

#include <string.h>

答案 1 :(得分:0)

享受:

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

int main()
{
    // single char can't store that string
    // so we have to declare an array of chars
    char name[] = {'s', 'i', 'l', 'a', 'm', '\0'}; 
    // also we can use string literal syntax:
    // char name[] = "silam";
    int length = strlen(name);
    printf("the length of %s is %d\n", name, length);
    getc(stdin);
    // i don't have <conio.h> so i call 'getc' instead of 'getch'
    return 0;
}
相关问题