This is an example to how check the dimension of an array of char
pointers:
这是如何检查char指针数组的维度的示例:
#include <stdio.h>
int main(void){
// prototype
int checkDim( char **array);
char *days[] = { //array of pointers
"Sunday", //days[0] = {'S','u','n','d','a','y','\0'}
"Monday", //days[1] = {....}
"Tuesday", //days[2] = {....}
"Wednesday", //days[3] = {....}
"Thursday", //days[4] = {....}
"Friday", //days[5] = {....}
"Saturday" //days[6] = {....}
};
int dim;
dim = checkDim( days );
printf("Number of days = %i \n", dim );
return 0;
}
int checkDim( char **array ){
char **ptr = array;
while(*ptr){
++ptr;
}
return ptr - array;
}
Respectively:
days[0]
is a pointer to char (another array),days[1]
is a pointer to char (another array)
...days[i]
is a pointer to char (another array)
days [0]是指向char(另一个数组)的指针,days [1]是指向char的指针(另一个数组)... days [i]是指向char的指针(另一个数组)
With a while
I can scan the array containing the pointers:
有一段时间我可以扫描包含指针的数组:
char **ptr = array; //
while(*ptr){
++ptr;
}
How I can can stop on the first element days[0]
and scan every letter to check the length of days[0]
(subsequently days[1]
, days[2]
etc...)?
I would like a printf
like this (for example):
我想像这样的printf(例如):
> Number of days 7.
>天数7。
> Sunday is long 6 letters
>星期天长6封信
> Monday is long 6 letters
>周一长6封信
> Tuesday is long 7 letters
>周二长7封信
etc..
1 个解决方案
#1
0
1st of all:
首先:
checkDim()
as it stands invokes undefined behaviour by accessing the pointer array days
out of bounds when looping past the pointer for "Saturday"
.
checkDim()因为它在循环超过“星期六”的指针时通过访问指针数组的日期来调用未定义的行为。
It's just bad luck the code seems to run.
代码似乎运行起来运气不好。
To fix this do explicitly introduce a NULL
value at the end of days
to functions as a sentinel:
要解决此问题,请在日期结束时显式引入NULL值作为标记:
char * days[] = { //array of pointers
"Sunday", //days[0] = {'S','u','n','d','a','y','\0'}
...
"Saturday", //days[6] = {....}
NULL
};
To answer your question:
回答你的问题:
To print the length of each "string" during iteration you code do this:
要在迭代期间打印每个“字符串”的长度,您可以执行以下操作:
while (*ptr)
{
printf("'%s' has length %zu.\n", *ptr, strlen(*ptr));
++ptr;
}
Please note that strlen()
returns size_t
(not int
) to using the conversion specifiers d
or u
is wrong here. If zu
is not defined by the compiler in use (VC) you want to use lu
instead.
请注意,strlen()返回size_t(不是int)以使用转换说明符d或u在这里是错误的。如果zu未被使用中的编译器(VC)定义,则需要使用lu。
Two more things:
还有两件事:
-
Placement of prototypes
原型的放置
Prototypes should be at global scope.
原型应该在全球范围内。
So change this:
所以改变这个:
int main(void) { // prototype int checkDim( char **array);
to be:
// prototype int checkDim( char ** array); int main(void) {
-
Qualify argument staying constant as
const
限定参数保持常量为常量
checkDim
does not changedays
, so help the compiler to create the best code by telling this to it.checkDim不会改变天数,因此请通过告诉它来帮助编译器创建最佳代码。
Change this:
int checkDim(char **array)
to be:
int checkDim(const char ** array)
#1
0
1st of all:
首先:
checkDim()
as it stands invokes undefined behaviour by accessing the pointer array days
out of bounds when looping past the pointer for "Saturday"
.
checkDim()因为它在循环超过“星期六”的指针时通过访问指针数组的日期来调用未定义的行为。
It's just bad luck the code seems to run.
代码似乎运行起来运气不好。
To fix this do explicitly introduce a NULL
value at the end of days
to functions as a sentinel:
要解决此问题,请在日期结束时显式引入NULL值作为标记:
char * days[] = { //array of pointers
"Sunday", //days[0] = {'S','u','n','d','a','y','\0'}
...
"Saturday", //days[6] = {....}
NULL
};
To answer your question:
回答你的问题:
To print the length of each "string" during iteration you code do this:
要在迭代期间打印每个“字符串”的长度,您可以执行以下操作:
while (*ptr)
{
printf("'%s' has length %zu.\n", *ptr, strlen(*ptr));
++ptr;
}
Please note that strlen()
returns size_t
(not int
) to using the conversion specifiers d
or u
is wrong here. If zu
is not defined by the compiler in use (VC) you want to use lu
instead.
请注意,strlen()返回size_t(不是int)以使用转换说明符d或u在这里是错误的。如果zu未被使用中的编译器(VC)定义,则需要使用lu。
Two more things:
还有两件事:
-
Placement of prototypes
原型的放置
Prototypes should be at global scope.
原型应该在全球范围内。
So change this:
所以改变这个:
int main(void) { // prototype int checkDim( char **array);
to be:
// prototype int checkDim( char ** array); int main(void) {
-
Qualify argument staying constant as
const
限定参数保持常量为常量
checkDim
does not changedays
, so help the compiler to create the best code by telling this to it.checkDim不会改变天数,因此请通过告诉它来帮助编译器创建最佳代码。
Change this:
int checkDim(char **array)
to be:
int checkDim(const char ** array)