如何输出字符** [英] How to output a char**

查看:148
本文介绍了如何输出字符**的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何输出从函数接收到的char **?



说我有:



< pre class = lang-cpp prettyprint-override> char ** foo = magicFunction();

Magic函数将返回一个char **,但我不知道该函数的工作方式或长度char **的长度以及它所容纳的char * s的长度。 (我正在使用执行此操作的库,并且将函数替换为magicFunction())



如果我尝试使用方括号来访问属性,

  std :: cout<< foo [0] [0]; 

我打印出来的全部是 0 。当我尝试增加第二个括号访问器时,出现奇怪的符号,并且可能是我不应该访问的内存。



我也尝试过取消引用: / p>

  std :: cout<< * foo; 
std :: cout<< ** foo;

但我仍然只打印出 0



当我只打印foo时:

  std :: cout<< foo; 

我得到了十六进制地址: 0x562e4699dda0 (尽管每次我运行程序时它都会波动)

解决方案

除非另有说明,否则指针数组将以NULL终止,并且字符串通常以 a0字符结尾。因此,对于打印数据,我会尝试:

  for(char ** ptr = foo; * ptr!= NULL; ptr ++ ){
std :: cout<< * ptr<< std :: endl;
}


How would one go about outputting a char** received from a function?

Say I have:

char** foo = magicFunction();

Magic function will return a char**, but I do not know how the function works or the length of the char**, and the length of the char*s that it holds. (I am using a library that does this, and I am substituting the function for magicFunction() )

If I try to access the properties with brackets:

std::cout << foo[0][0];

all I get printed out is 0. When I try to increase the second bracket accessor, I get strange symbols, and it is probably memory that I shouldn't be accessing.

I have also tried dereferencing:

std::cout << *foo;
std::cout << **foo;

but I still only get 0 printed out.

When I print just foo:

std::cout << foo;

I get the hex address: 0x562e4699dda0 (although it fluctuates every time I run the program)

解决方案

Unless specified otherwise, the array of pointers will be terminated with a NULL, and the strings are usually terminated with a '\0' character. So for printing your data I'd try:

for( char **ptr = foo; *ptr != NULL; ptr++ ) {
    std::cout << *ptr << std::endl;
}

这篇关于如何输出字符**的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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