这个程序的错误是什么?它显示了一般保护故障错误 [英] what is the mistake in this program? it shows general protection fault error

查看:58
本文介绍了这个程序的错误是什么?它显示了一般保护故障错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
void main()
{

    char name[30];
    int i;

    printf("Enter ur name:\n");
    for(i=0;i<30;i++)
    scanf("%s",&name[i]);



    for(i=0;i<30;i++)
    printf("The name is :%s",name[i]);
}

推荐答案

你的printf语句是使用%s,用于ASCIIZ字符串,你打印的是单个字符name [i] - 被视为指向谁知道你的O / S在哪里字符串的指针



printf(%s, name);

或printf(%c,name [i]); //为你的循环





You're printf statement is using %s, which is for ASCIIZ strings and you are printing individual characters name[i] - which are being treated as pointers to a string in the "who knows where" of your O/S

either printf("%s", name);
or printf("%c", name[i]); // for your loop



// wf-protection-fault-error.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<stdio.h>

const int max_namecount   =5;   // 5 names
const int max_namelength  =50;  // each name has space for 50 chars

int main(int argc, char *argv[]) 
{
    char name[max_namecount][max_namelength+3];
    int i;

    printf("Enter your name:\n");
    for(i=0;i<max_namecount;i++)  scanf("%50s",name[i]); // 50=max_namelength:  makes sure, that you do not read more chars than you have space in name

    getc(stdin); // only for pause
    for(i=0;i<max_namecount;i++)  printf("\nThe name is :%s",name[i]);

    getc(stdin); // only for pause
      return 0;
}


这篇关于这个程序的错误是什么?它显示了一般保护故障错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆