我如何处理const char *? [英] How do I handle a const char * ?

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

问题描述

这听起来像是一个愚蠢的问题,但我不能完全确定如何正确地问它,这是我第一次询问编程相关的问题(通常我可以找到我正在寻找的东西)搜索,但是嘿)。

所以我正在使用allegro,我有很多行代码,有大量的数学运算,我需要调试它,所以我'我试图发送mid计算int(floar)s(正确转换为字符串不用担心)。我的问题是我总是遇到试图用allegro输出变量值的问题,因为textout函数需要'const char *'类型,我真的不知道如何使用它。我很确定它指向一个/ 0终止字符数组(不管是什么,我假设像[0] ='H',[1] =i,[2] = !,[3] =/ 0)并且是一个常数,所以我宣布后不能更改它(这对于这种情况很烦人)。

我的主要问题是:如何在不造成崩溃的情况下正确使用此类型?

并且我可能做了其他错误的事情......如果是这样,请告诉我。

这个子程序有时会起作用,有时会在向屏幕输出任何内容之前崩溃,但我很确定我只能使用它一次。在我再次使用之前,是否必须删除const char *?我不确定该怎么做,我已经尝试删除它,但我很确定我一定是使用了错误的语法或其他东西。 我只需要一些教学,我一直在使用快板作为限制,我可能已经做了一些事情,但我不记得了......如果我得到一个好的答案,我肯定会写在安全的地方:)



this may sound like a stupid question but I'm not entirely sure as to how to ask it properly and this is my first time asking a programming related question (normally I can find what I'm looking for by search but hey).
So I'm using allegro and I've got quite a few lines of code with a lot of trig math in and I need to debug it so I'm trying to send the mid calculation int(floar)s (properly converted to strings don't worry). my problem is that I always run into issues trying to output variable values with allegro because the textout function requires a 'const char *' type and I don't really know how to use it. I'm pretty sure its a pointer to a /0 terminating character array (whatever that is, I'm assuming something like [0]='H',[1]="i",[2]="!",[3]="/0") and is a constant so I can't change it after it's declared (which is annoying, for this situation).
my main question being: how do I use this type properly without causing crashes?
and perhaps I've done something else wrong.. if so, please let me know.
this subroutine sometimes works and sometimes crashes before outputing anything to the screen but I'm pretty sure I can only use it once. Do I have to delete the const char * before I use it again? I'm not really sure what to do, I've tried deleting it but I'm pretty sure I must've used the wrong syntax or something. I just need some teaching on this, I run into this as a limitation all the time using allegro, I may have done something right before but I can't remember.. If I get a good answer I'll definitely write it down somewhere safe :)

#include <allegro.h>
#include <string>

void debugOut(std::string outstr)
{
     const char * c = outstr.c_str();
     clear_to_color(screen, 0x32324B);
     textout_centre_ex(screen, font, c, resolution.x/2, resolution.y/2, 0xFFFFFF, 0x333333);
     while(!key[KEY_ENTER]){}
     rest(500);
}

推荐答案

是的,你是对的 - 它是一个指向空终止字符数组的指针。

const部分只是意味着你不能通过指针修改它 - 而不是它不能被修改。您可以传递真正的成本价值:

Yes, you are right - a it's a pointer to a null terminated array of characters.
The const part just means that you can't modify it via the pointer - not that it can't be modified. You can either pass a genuine cost value:
const char * c = "hi!";

或者你可以传递一个非const数组:

Or you can pass it a non-const array:

char data[6];
const char * c;
strcpy(data, "hi!");
c = data;
printf(c);



,两者都有效。重要的是,一旦将它分配给const指针,就不能通过该指针进行更改 - 因此您可以安全地传递一个真正常量的字符串,它将正常工作。


and either will work. The important thing is that once it is assigned to the const pointer, you can't change it via that pointer - so you can safely pass a genuinely constant string and it'll work fine.


当你在某处看到const char *时,这意味着代码想要查看char字符串,并且不会修改它。在这里给出的代码示例中,字符串的内存管理在std :: string中处理。您的代码获取一个const char *查看std :: string并相应地使用它。您的代码应该在范围退出时删除值'c'(正如您在此处所做的那样)。对于此代码段,请勿在完成后尝试删除c;内存由'outstr'拥有,而不是'c'。
When you see a const char* somewhere, it means that the code wants to see a char string, and will not be modifying it. In the code example you give here, the memory management for the string is handled in the std::string. Your code gets a const char* look at the std::string and uses it accordingly. Your code should just drop value 'c' at scope exit (as you have done here). For this code segment, do not attempt to delete 'c' when you are done with it; the memory is owned by 'outstr', not by 'c'.


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

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