我在这里设置指针,一个指向名称,另一个指向名称,但获取长度。怎么当我使用的时候cout << strlen(tail);
它一直告诉我长度是3?即使我输入的是 12?
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
char name[0];
cout << "Please enter your name: ";
cin.getline(name, 256);
cout << "Your name: " << name << endl;
char* head = name;
cout << head[6] << endl;
char* tail = name;
cout << strlen(tail);
return 0;
}
With
char name[0];
您正在分配大小的缓冲区0
其中存储数据。您需要使其足够大以容纳要输入的最长字符串(对于 NUL 终止符加 1),在本例中为 256(因为您正在读取 255 个字符和一个带有 NUL 的 NUL)。cin.get(name, 256)
):
char name[256];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)