C语言 C 用指针遍历 char 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48367022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C iterate through char array with a pointer
提问by christopher westburry
I am very new in C and was wondering about how to get each element of an array using a pointer. Which is easy if and only if you know the size of the array. So let the code be:
我是 C 的新手,想知道如何使用指针获取数组的每个元素。当且仅当您知道数组的大小时,这很容易。所以让代码是:
#include <stdio.h>
#include <stdio.h>
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;
}
Now I want to determine the size of text. to do this, I found out, that the String will end with a '\0'character. So I wrote the following function:
现在我想确定text. 为此,我发现字符串将以'\0'字符结尾。所以我写了以下函数:
int getSize (char * s) {
char * t; // first copy the pointer to not change the original
int size = 0;
for (t = s; s != 'int getSize (char * s) {
char * t; // first copy the pointer to not change the original
int size = 0;
for (t = s; *t != 'int getSize (char * s) {
char * t;
for (t = s; *t != 'for (t = s; s != 'for (t = s; *t != '#include <string.h>
#include <stdio.h>
//...
char s[100] = "Hello christopher westburry";
printf( "sizeof( s ) = %zu\n", sizeof( s ) );
printf( "strlen( s ) = %zu\n", strlen( s ) + 1 );
'; t++) {
^^^^^^^^^
'; t++) {
^^^^^^^^^
'; t++)
;
return t - s;
}
'; t++) {
size++;
}
return size;
}
'; t++) {
size++;
}
return size;
}
This function however does not work as the loop seems to not terminate.
然而,这个函数不起作用,因为循环似乎没有终止。
So, is there a way to get the actual size of the chars the pointer points on?
那么,有没有办法获得char指针指向的s 的实际大小?
回答by mdatsev
Instead of checking the pointer you have to check the current value. You can do it like this:
您必须检查当前值而不是检查指针。你可以这样做:
size_t getSize ( const char * s )
{
size_t size = 0;
while ( *s++ ) ++size;
return size;
}
Or more concisely:
或者更简洁:
##代码##回答by Vlad from Moscow
There is a typo in this for loop
这个for循环有一个错字
##代码##I think you mean
我想你的意思
##代码##Nevertheless in general the function does not provide a value that is equivalent to the value returned by the operator sizeofeven if you will count also the terminating zero. Instead it provides a value equivalent to the value returned by the standard function strlen.
尽管如此,通常该函数不会提供与运算符返回的值等效的值,sizeof即使您还将计算终止零。相反,它提供了一个与标准函数返回的值等价的值strlen。
For example compare the output of this code snippet
例如比较此代码片段的输出
##代码##So your function just calculates the length of a string.
所以你的函数只是计算字符串的长度。
It would be more correctly to define it the following way (using pointers)
按以下方式定义它会更正确(使用指针)
##代码##
