C 使用指针遍历 char 数组
C iterate through char array with a pointer
我是 C 的新手,想知道如何使用指针获取数组的每个元素。当且仅当您知道数组的大小时,这很容易。
所以让代码为:
#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;
}
现在我想确定 text
的大小。为此,我发现字符串将以 '[=14=]'
字符结尾。所以我写了下面的函数:
int getSize (char * s) {
char * t; // first copy the pointer to not change the original
int size = 0;
for (t = s; s != '[=11=]'; t++) {
size++;
}
return size;
}
但是这个函数不起作用,因为循环似乎没有终止。
那么,有没有办法获取指针指向的 char
的实际大小?
您不必检查指针,而是检查当前值。你可以这样做:
int getSize (char * s) {
char * t; // first copy the pointer to not change the original
int size = 0;
for (t = s; *t != '[=10=]'; t++) {
size++;
}
return size;
}
或者更简洁:
int getSize (char * s) {
char * t;
for (t = s; *t != '[=11=]'; t++)
;
return t - s;
}
这个 for 循环有错别字
for (t = s; s != '[=10=]'; t++) {
^^^^^^^^^
我想你是说
for (t = s; *t != '[=11=]'; t++) {
^^^^^^^^^
尽管如此,一般情况下,该函数不会提供与运算符 sizeof
返回的值等效的值,即使您也将终止零计算在内。相反,它提供的值等同于标准函数 strlen
.
返回的值
例如比较此代码片段的输出
#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 );
所以你的函数只是计算字符串的长度。
下面这样定义会更正确(使用指针)
size_t getSize ( const char * s )
{
size_t size = 0;
while ( *s++ ) ++size;
return size;
}
我是 C 的新手,想知道如何使用指针获取数组的每个元素。当且仅当您知道数组的大小时,这很容易。 所以让代码为:
#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;
}
现在我想确定 text
的大小。为此,我发现字符串将以 '[=14=]'
字符结尾。所以我写了下面的函数:
int getSize (char * s) {
char * t; // first copy the pointer to not change the original
int size = 0;
for (t = s; s != '[=11=]'; t++) {
size++;
}
return size;
}
但是这个函数不起作用,因为循环似乎没有终止。
那么,有没有办法获取指针指向的 char
的实际大小?
您不必检查指针,而是检查当前值。你可以这样做:
int getSize (char * s) {
char * t; // first copy the pointer to not change the original
int size = 0;
for (t = s; *t != '[=10=]'; t++) {
size++;
}
return size;
}
或者更简洁:
int getSize (char * s) {
char * t;
for (t = s; *t != '[=11=]'; t++)
;
return t - s;
}
这个 for 循环有错别字
for (t = s; s != '[=10=]'; t++) {
^^^^^^^^^
我想你是说
for (t = s; *t != '[=11=]'; t++) {
^^^^^^^^^
尽管如此,一般情况下,该函数不会提供与运算符 sizeof
返回的值等效的值,即使您也将终止零计算在内。相反,它提供的值等同于标准函数 strlen
.
例如比较此代码片段的输出
#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 );
所以你的函数只是计算字符串的长度。
下面这样定义会更正确(使用指针)
size_t getSize ( const char * s )
{
size_t size = 0;
while ( *s++ ) ++size;
return size;
}