c语言 char * 遍历,使用指针遍历char数组
我是C語言的新手,想知道如何使用指針獲取數(shù)組的每個(gè)元素。當(dāng)且僅當(dāng)您知道數(shù)組的大小時(shí),這才容易。因此,讓代碼為:
#include
int main (int argc, string argv[]) {
char * text = "John Does Nothing";
char text2[] = "John Does Nothing";
int s_text = sizeof(text); // returns size of pointer. 8 in 64-bit machine
int s_text2 = sizeof(text2); //returns 18. the seeked size.
printf("first string: %s, size: %d\n second string: %s, size: %d\n", text, s_text, text2, s_text2);
return 0;
}
現(xiàn)在我要確定的大小text。為此,我發(fā)現(xiàn)字符串將以'\0'字符結(jié)尾。所以我寫了以下函數(shù):
int main (int argc, string argv[]) {
char * text = "John Does Nothing";
char text2[] = "John Does Nothing";
int s_text = sizeof(text); // returns size of pointer. 8 in 64-bit machine
int s_text2 = sizeof(text2); //returns 18. the seeked size.
printf("first string: %s, size: %d\n second string: %s, size: %d\n", text, s_text, text2, s_text2);
return 0;
}
但是,此功能不起作用,因?yàn)檠h(huán)似乎沒有終止。
那么,有沒有辦法獲取char指針指向的s的實(shí)際大小?
總結(jié)
以上是生活随笔為你收集整理的c语言 char * 遍历,使用指针遍历char数组的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自强不息系列之Python 线性查找
- 下一篇: 2020-09-03解决pip inst