小编Joh*_*hnH的帖子

当我尝试打印char指针时,为什么我的程序崩溃了

所以我为我的C类创建了这个程序,它可以创建一个函数来告诉你字符串中某个数字位置的字符.

char charAt( char * string, int position );

int main( int argc, char * argv[ ] ) {

    int pos = atoi( argv[3] );

    if( strcmp( argv[1], "charAt" ) == 0 )
    {
        printf( "%s", charAt( argv[2], pos ) );
    }    
}



char charAt( char *string, int position ){
    int count = 0;
    while( count != position ){
        string++;
        count++;
    }

    return *string;
}
Run Code Online (Sandbox Code Playgroud)

当我编译它时,在我使用命令行运行它时没有显示错误

name charAt string 3
Run Code Online (Sandbox Code Playgroud)

它崩溃了

printf( "%s", charAt( argv[2], pos) );
Run Code Online (Sandbox Code Playgroud)

那一行为什么当我将char指针传递给printf函数时它会崩溃?

c

0
推荐指数
1
解决办法
90
查看次数

标签 统计

c ×1