如何在c中打印指针变量 [英] How do i print a pointer variable in c

查看:205
本文介绍了如何在c中打印指针变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我知道这看起来像是早期查询的重复,但我已经搜索并尝试了所有内容。我无法打印双指针。我有以下代码



Now, I understand this would look like a repeat of earlier queries, but I have searched and tried everything. I am not able to print a double pointer. I have the following code

int main(void)
{

char *line;
  char **args;
  int status;

  do {
    //stuff
    args = split(&line);

    printf(*args);
}





现在一切正常,只是在最后的打印行中,我收到以下错误:





Now it is all executing fine, Just that in the final print line, I am getting the below error:

expected 'const char * __restrict__' but argument is of type 'char ***'
 extern int printf (const char *__restrict __format, ...);







现在,我知道为什么我收到这个错误,因为我试图非法打印一些东西。我只是不知道我需要修复到底是什么,如果我需要打印这些双指针,我们该怎么做呢。



谢谢



我尝试过:



我研究过类似的答案,但没有真正解决我的问题,我也尝试阅读关于指针的文献,但函数split()的返回值是一个指针,我不能改变它。




Now, I know why I am getting this error, because I am trying to print something illegally. I just don't know what exactly is it that I need to fix, also, if I ever need to print these double pointers, how do we go about it.

Thanks

What I have tried:

I have researched for similar answers, but nothing really solves my problem, I have also tried to read literature on pointers, but the return value of the function split() is a pointer and I cannot change that.

推荐答案

由于实际代码被'stuff'隐藏,我们无法获得更多信息。但是,由于 split 调用可能会产生一个字符串数组,您必须按顺序迭代数组项打印它。
Since the actual code is hidden by 'stuff' we can't get much info about. However, since the split call probably produces an array of strings, you have to iterate over the array items in order to print it.


有一些乐趣...你可以打印指针的物理内存地址,你也可以打印指针的值。



尝试一些很酷的东西



printf(%l,args); // args是一个指针







printf(%l,* args); // * args是另一个指针







printf(%c,** args ); //因为** args是一个char(你的字符串的第一个字符)



但是如果* args是一个字符串,那么



printf(%s,* args); //将打印你的字符串(第一个)。



RTFM printf上的手册。检查使用格式命令格式化数字的所有不同方法。



printf(formatString,val1,val2,....);



享受!
Have some fun...you can print the pointer's physical memory address, and you can also print the value of the pointer.

Try some cool stuff

printf("%l",args); // args is a pointer

and

printf("%l",*args); // *args is another pointer

and

printf("%c", **args); // since **args is a char (the first character of your string)

but if *args is a string, then

printf("%s", *args); // will print your string (the first one).

RTFM the manual on printf . Examine all the different ways you can format numbers using the "format" commands.

printf(formatString, val1, val2, ....);

Enjoy!


请阅读 - > 指针 - C ++教程 [ ^ ]并如何解析命令行参数。 - C ++文章 [ ^ ]
Please read -> Pointers - C++ Tutorials[^] And How to parse command line parameters. - C++ Articles[^]


这篇关于如何在c中打印指针变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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