Questions tagged [const-char]

68 questions
127
votes
10 answers

how to convert from int to char*?

The only way I know is: #include #include using namespace std; int main() { int number=33; stringstream strs; strs << number; string temp_str = strs.str(); char* char_type = (char*) temp_str.c_str(); } But is there…
rsk82
  • 28,217
  • 50
  • 150
  • 240
92
votes
7 answers

How to convert const char* to char* in C?

In my project there is a method which only returns a const char*, whereas I need a char* string, as the API doesn't accept const char*. Any idea how to convert between const char* to char*?
Morpheus
  • 3,285
  • 4
  • 27
  • 57
18
votes
4 answers

Are strtol, strtod unsafe?

It seems that strtol() and strtod() effectively allow (and force) you to cast away constness in a string: #include #include int main() { const char *foo = "Hello, world!"; char *bar; strtol(foo, &bar, 10); // or…
user102008
  • 30,736
  • 10
  • 83
  • 104
17
votes
3 answers

Is it appropriate to set a value to a "const char *" in the header file

I have seen people using 2 methods to declare and define char *. Medhod 1: The header file has the below extern const char* COUNTRY_NAME_USA = "USA"; Medhod 2: The header file has the below declaration: extern const char* COUNTRY_NAME_USA; The…
sud
  • 385
  • 1
  • 3
  • 11
8
votes
1 answer

Returning const char* from a string literal in C++?

Normally, I would return a std::string from a function because returning a const char* would require the caller to provide an output memory buffer, and that buffer is not resizable. But is returning a const char* valid if its from a string…
Bernard
  • 5,209
  • 1
  • 34
  • 64
8
votes
2 answers

Python to C/C++ const char question

I am extending Python with some C++ code. One of the functions I'm using has the following signature: int PyArg_ParseTupleAndKeywords(PyObject *arg, PyObject *kwdict, char *format, char **kwlist, ...); (link:…
Eugene
  • 93
  • 6
7
votes
2 answers

Using char* as a key in std::map, how does it work

This question relates directly to using char as a key in stdmap. I understand what the compare function passed in does and why its required for char * types as a key. However, I'm uncertain as how the updating actually works. I'm curious as to the…
travis
  • 8,055
  • 1
  • 17
  • 19
6
votes
3 answers

printf question with a const char* variable

I am stuck in a printf problem. I would appreciate if I can get some help here: In the below code, I can see the font family get displaced correctly in first printf(), but if I set it to variable, i only get an empty string. How can I put it in a…
michael
  • 106,540
  • 116
  • 246
  • 346
5
votes
5 answers

Creating file names automatically C++

I'm trying to write a program in C++, which creates some files (.txt) and writes down the result in them. The problem is that an amount of these files is not fixed at the beginning and only appears near the end of the program. I would like to name…
Tatiana
  • 113
  • 1
  • 1
  • 9
5
votes
2 answers

C++: passing a string-literal of Type const char* to a string-parameter

i'm new to c++ and have a lack of understanding why this code works well: string GetString(string promt) { cout << promt << ": "; string temp; getline(cin, temp); return temp; } int main() { string firstName = GetString("Enter…
Grundkurs
  • 199
  • 1
  • 8
4
votes
3 answers

Constant character pointer using unique_ptr

Let's say that I have a vector input and I need to pass this to a function that takes a const char** argument. My thought had been to use a unique_ptr like this: const auto output = make_unique(size(input)); But I can't seem to turn…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
4
votes
3 answers

Dynamically allocating memory for const char string using malloc()

I am writing a program that reads a value from an .ini file, then passes the value into a function that accepts a PCSTR (i.e. const char *). The function is getaddrinfo(). So, I want to write PCSTR ReadFromIni(). To return a constant string, I plan…
paperduck
  • 1,175
  • 1
  • 16
  • 26
4
votes
4 answers

GetLogicalDriveStrings() and char - Where am I doing wrongly

I want to search a file which may be present in any drives such as C:\, D:\ etc. Using GetLogicalDriveStrings I can able to get the list of drives but when I add anything extra for the output, I am getting a null in the output prompt. Here is my…
highlander141
  • 1,683
  • 3
  • 23
  • 48
3
votes
2 answers

C++ const char with .begin() and .end()

So I have a program that looks something like this: const char *Argv[] = {"stuff", "stuff1", "stuff3"}; bool pass = xxxxx::yyyyy(Argv.begin(), Argv.end(), Tri); I think this is illegal because const char * is not a user-defined type. However, I…
grizzleKat456
  • 127
  • 1
  • 7
3
votes
6 answers

Pass contents of stringstream to function taking char* as argument

I have a function for writing ppm files (a picture format) to disk. It takes the filename as a char* array. In my main function, I put together a filename using a stringstream and the << operator. Then, I want to pass the results of this to my ppm…
SSilk
  • 2,433
  • 7
  • 29
  • 44
1
2 3 4 5