c语言 char * 遍历,使用指针遍历char数组
我是C語言的新手,想知道如何使用指針獲取數組的每個元素。當且僅當您知道數組的大小時,這才容易。因此,讓代碼為:
#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;
}
現在我要確定的大小text。為此,我發現字符串將以'\0'字符結尾。所以我寫了以下函數:
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;
}
但是,此功能不起作用,因為循環似乎沒有終止。
那么,有沒有辦法獲取char指針指向的s的實際大小?
總結
以上是生活随笔為你收集整理的c语言 char * 遍历,使用指针遍历char数组的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自强不息系列之Python 线性查找
- 下一篇: java中undefined如何解决_处