从char *转换为char ** [英] char* to char**

查看:161
本文介绍了从char *转换为char **的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何将char*分配给char** ????

例如:

How can we assign char* to char**??

For example:

char ** strArray = (char **)malloc(N*sizeof(char));
char *str ;
std::string st ("Hi there how are you??");
str = new char[st.size()+1];
strcpy(str, st.c_str());

strArray[1] = ??? <-----str (copy str contents to strArray)


问候,

Abhishek Dey


Regards,

Abhishek Dey

推荐答案

您已将strArray定义为char*的数组(即,指向字符串的指针),但是分配的大小为N *单个字符.我怀疑你的意思是
You have defined strArray as an array of char* (i.e. pointers to character strings) but your allocation is of N * the size of a single character. I suspect what you meant was
char ** strArray = (char **)malloc(N * sizeof(char*));


然后,您可以使用诸如
的分配


Then you could use an assignment such as

strArray[1] = str;  // save the address of this string to strArray


是的....我在那里犯了一个可怕的错误.谢谢帕里尼(Thanx Pallini).
Yes....I made a terrible mistake there. Thanx Pallini.


这篇关于从char *转换为char **的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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