计算double dimentional char数组中的字符串文字数 [英] Counting number of strings literals in double dimentional char array

查看:65
本文介绍了计算double dimentional char数组中的字符串文字数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


下面是我用字符串文字在main中初始化双维数组

的代码。

现在我想使用一个函数调用显示字符串,我只是想要将数组作为参数传递,而没有其他信息,例如

字符串。


有没有办法实现这个目标?


现在我已经应用了一个解决方法,我保留了我的最后一个文字

""这充当了文字的结尾。


--------------------------


#include< stdio.h>

#define NUM 20


void display(char input [] [NUM])

{

char(* pc)[NUM];

int i = 0;


pc =输入;

for(i = 0; strcmp(* pc,"")!= 0; i ++,pc ++){

printf(" %s \ nn,* pc);

}


}

int main()

{


char input [] [NUM] = {" hi"," from"," ranjeet",""};


显示(输入);

}


---------------- ------

解决方案



ranjmis写道:

大家好,

下面是我用字符串文字在main里面初始化双维数组的代码。
现在我想用函数调用来显示字符串只是想要通过数组作为参数,没有其他信息,比如
字符串的数量。

有没有办法实现呢?

现在我已经应用了一个解决方法把我的最后一个文字保留为
""作为我的文字结尾。



< snip>


除非你知道数组的长度是不可能的指定函数的

长度或以某种方式标记它。

我不确定这是否在所有地方都有效但你可以声明主要

是这样的:

int main(int argc,char * argv [],char ** env)

其中env是一个字符串数组,列出每一个环境变量。

没有计数器告诉你有多少元素(比如

argc for argv)但是它的尺寸比一个大于

变量,最后一个变量设置为NULL。你可以逐行浏览

,直到你达到NULL。


-

Ioan - Ciprian Tandau

tandau _at_ freeshell _dot_ org(希望现在还不算太晚)

(......它仍然有效......)


在文章< 11 ********************** @ z34g2000cwc.googlegroups .com> ;,

Nelu< ta ******** @ gmail.com>写道:

我不确定这是否在所有地方都有效,但你可以像这样声明主要
int main(int argc,char * argv [],char ** env)
其中env是一个列出每个环境变量的字符串数组。




这不是C标准的一部分,但是可以通过实现提供

扩展名。


[但是,这似乎与用户的问题无关,因为

用户已经讨论过添加一个sentinal。]

-

编程就是在你忙于制定其他计划的时候发生的事情。 br />




Walter Roberson写道:

文章< 11 *********** ***********@z34g2000cwc.googlegroups .com> ;,
Nelu< ta ******** @ gmail.com>写道:

我不确定这是否在所有地方都有效,但你可以像这样声明主要
int main(int argc,char * argv [],char ** env)
其中env是一个列出每个环境变量的字符串数组。



这不是C标准的一部分,但可能会提供作为一个实现的扩展。

[这似乎与用户的问题无关,但是,因为用户已经讨论过添加一个sentinal。 ]




我刚刚告诉他选项。我也认为使用NULL比使用零长度字符串更好




-

Ioan - Ciprian Tandau

tandau _at_ freeshell _dot_ org(希望现在还不算太晚)

(......它仍然有效......)

>


Hi all,

Below is the code wherein I am initializing double dimentional array
inside main with string literals.
Now I want to display the strings using a function call to which I just
want to pass the array as argument with no other info like number of
strings.

Is there a way to achieve that?

Right now I have applied a workaround where I keep my last literal as
"" which acts as my end of literals.

--------------------------

#include<stdio.h>
#define NUM 20

void display(char input[][NUM])
{
char (*pc)[NUM];
int i=0;

pc=input;
for(i=0;strcmp(*pc,"")!=0;i++,pc++){
printf("%s\n",*pc);
}

}
int main()
{

char input[][NUM] = {"hi","from","ranjeet",""};

display(input);
}

----------------------

解决方案


ranjmis wrote:

Hi all,

Below is the code wherein I am initializing double dimentional array
inside main with string literals.
Now I want to display the strings using a function call to which I just
want to pass the array as argument with no other info like number of
strings.

Is there a way to achieve that?

Right now I have applied a workaround where I keep my last literal as
"" which acts as my end of literals.


<snip>

It''s impossible to know the length of your array unless you specify the
length to the function or mark it in a certain way.
I''m not sure that this is valid everywhere but you can declare main
like this:
int main(int argc, char *argv[], char **env)
where env is an array of string that lists every environment variable.
There is no counter that tells you how many elements there are (like
argc for argv) but it''s dimension is one larger than the number of
variables and the last one is set to NULL. You can go through the lines
one by one until you hit NULL.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it''s not too late)
(... and that it still works...)


In article <11**********************@z34g2000cwc.googlegroups .com>,
Nelu <ta********@gmail.com> wrote:

I''m not sure that this is valid everywhere but you can declare main
like this:
int main(int argc, char *argv[], char **env)
where env is an array of string that lists every environment variable.



That is not part of the C standard, but may be offered as an
extension by an implementation.

[It doesn''t seem relevant to the user''s question, though, since
the user already discussed adding a sentinal.]
--
Programming is what happens while you''re busy making other plans.



Walter Roberson wrote:

In article <11**********************@z34g2000cwc.googlegroups .com>,
Nelu <ta********@gmail.com> wrote:

I''m not sure that this is valid everywhere but you can declare main
like this:
int main(int argc, char *argv[], char **env)
where env is an array of string that lists every environment variable.



That is not part of the C standard, but may be offered as an
extension by an implementation.

[It doesn''t seem relevant to the user''s question, though, since
the user already discussed adding a sentinal.]



I just told him the options. I also think that using NULL is better
than using a zero length string.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it''s not too late)
(... and that it still works...)


这篇关于计算double dimentional char数组中的字符串文字数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆