问题:为什么不能将char [10] [10]自动转换为char ** [英] PROBLEM: why can't auto-covert char [10][10] into char **

查看:105
本文介绍了问题:为什么不能将char [10] [10]自动转换为char **的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


char [10] [10]到char **是编译错误。为什么?


如果我有一个字符串数组。是。这是不安全的,最好使用

vector< stringinstead。


但我的观点是语言功能。


char sex [2] [128] = {" Male"," Female" };


void print(char ** p,int len){

//打印所有性别

}


print(性别,128); //< ---这会导致编译错误。


是否有任何问题要使这个转换成语言?


问候

-Wisdo

Hi All,

char [10][10] to char ** is compile error. why?

if i hava a string array. yes. it''s not safe and it''s better to use
vector<stringinstead.

but my point is the language feature.

char sex[2][128] = {"Male", "Female" };

void print(char **p, int len) {
// print all sex
}

print(sex, 128); // <--- this cause compile error.

Is any issue to make the language forbiden this covertion?

Regards
-Wisdo

推荐答案



void print(char(* p)[128],int len)


定义应解决你的问题。具有2维

数组的函数

作为参数需要提示能够正确地偏移数组。这个



,因为它们实际上是阵列数组。


希望这会有所帮助,


Tolga Ceylan


void print(char (*p)[128], int len)

definition should fix your problem. Functions that have 2 dimensional
arrays
as arguments need a hint to be able to offset the arrays properly. This
is
because these are arrays of arrays actually.

Hope this helps,

Tolga Ceylan


Wisdo< wi *** @ hf.webex.comwrote:
Wisdo <wi***@hf.webex.comwrote:

大家好,


char [10] [10]到char **是编译错误。为什么?


如果我有一个字符串数组。是。这是不安全的,最好使用

vector< stringinstead。


但我的观点是语言功能。


char sex [2] [128] = {" Male"," Female" };


void print(char ** p,int len){

//打印所有性别

}


print(性别,128); //< ---这导致编译错误。


是否有任何问题要使这个转换语言禁止?
Hi All,

char [10][10] to char ** is compile error. why?

if i hava a string array. yes. it''s not safe and it''s better to use
vector<stringinstead.

but my point is the language feature.

char sex[2][128] = {"Male", "Female" };

void print(char **p, int len) {
// print all sex
}

print(sex, 128); // <--- this cause compile error.

Is any issue to make the language forbiden this covertion?



是的,char [] []与char **不同。 ''sex''不是指向指向char的

指针。

Yes, char[][] is not the same as a char**. ''sex'' isn''t a pointer to a
pointer to a char.




" Wisdo" < wi *** @ hf.webex.com写信息

news:ec ********** @ news.yaako.com ...

"Wisdo" <wi***@hf.webex.comwrote in message
news:ec**********@news.yaako.com...

大家好,

char [10] [10]到char **是编译错误。为什么?


如果我有一个字符串数组。是。这是不安全的,最好使用

vector< stringinstead。


但我的观点是语言功能。


char sex [2] [128] = {" Male"," Female" };


void print(char ** p,int len){

//打印所有性别

}


print(性别,128); //< ---这会导致编译错误。


是否有任何问题要使这个转换成语言?


问候

-Wisdo
Hi All,

char [10][10] to char ** is compile error. why?

if i hava a string array. yes. it''s not safe and it''s better to use
vector<stringinstead.

but my point is the language feature.

char sex[2][128] = {"Male", "Female" };

void print(char **p, int len) {
// print all sex
}

print(sex, 128); // <--- this cause compile error.

Is any issue to make the language forbiden this covertion?

Regards
-Wisdo



因为char [10] [10]不是一个指针数组,所以它是一个二维的

数组字符。

内存的分配和使用方式与char [100]相同,你可以自己测试一下。


这两次打印出男性和女性。选择你最喜欢的那个。


void print(const char p [] [128],const int length)

{

for(int i = 0; i< length; ++ i)

std :: cout<< & p [i] [0]<< std :: endl;

}


void print2(const char * p,const int width,const int length)

{

for(int i = 0; i< length; ++ i)

std :: cout<< & p [width * i]<< std :: endl;

}


int main()

{

char sex [ 2] [128] = {"男性"," Female" };

打印(性别,2);

print2(reinterpret_cast< const char *>(性别),128,2);

}

Because char [10][10] is not an array of pointers, it is a two dimentional
array of characters.
The memory is allocated and used about the same way as char [100] and you
can test that yourself.

This prints out Male and Female twice. Pick the one you like best.

void print( const char p[][128], const int length )
{
for ( int i = 0; i < length; ++i )
std::cout << &p[i][0] << std::endl;
}

void print2( const char* p, const int width, const int length )
{
for ( int i = 0; i < length; ++i )
std::cout << &p[width * i] << std::endl;
}

int main()
{
char sex[2][128] = {"Male", "Female" };
print( sex, 2 );
print2( reinterpret_cast<const char *>( sex ), 128, 2 );
}


这篇关于问题:为什么不能将char [10] [10]自动转换为char **的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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