通过 C 中的指针打印 char 数组时出现分段错误

Segmentation Fault while printing char array through pointer in C

我正在复习一些有关嵌入式编程的 C 编码,但我不明白为什么下面会抛出分段错误。我认为通过检查指针是否指向 NULL 就足够了,但似乎并非如此。

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

char json[] = "Authentication:YERL90\nredLedBri:400\nredLedOn:true\n";
char inData[100];

void printBuffer(char * buf) {
    cout << "...printing buffer:" << endl;
    char * p = buf;
    while(p) {
        cout << *p << endl;
        p++;
    }
    cout << "...end printing" << endl;

}

void resetBuffer(char * buf)
{
  memset(&buf[0], 0, sizeof(buf));
}


int main()
{

   printBuffer(json);

   return 0;
}

知道哪里出了问题吗?

您正在检查地址而不是地址处的值。

while(p)替换为while(*p)