printf在for循环之外工作正常,但在for循环中保持运行时会导致一些错误

时间:2017-12-16 10:42:22

标签: c++ c++11

#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
    int b;
    printf("hello");
    for(b=1;b<=100;++b)
    {
        if(b%10==1){
          cout << "\n";
          for(int l=0;l<=100;++l)
            cout << "-" ;
            cout << endl;           
        }    
        printf("|%s|",b);
    }
  return 0;
}

enter image description here

printf放在循环体外面工作正常,但放在循环体中的那个会在运行时出现某种错误!看看图片!

1 个答案:

答案 0 :(得分:1)

您的bint 你给b,其中printf()需要一个指向char的指针,并试图取消引用你给出的值。
由于您通过b提供的值不是指向任何内容的有效指针,因此您的程序存在一些访问问题。