const-char

Dynamically allocating memory for const char string using malloc()

[亡魂溺海] 提交于 2020-01-13 11:07:06
问题 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 on allocating memory using malloc() and casting the memory to a constant string. I will be able to get the exact number of characters that were read from the .ini file. Is that technique okay? I don't really know what else to do. The following

Initialize const char* by concatenating another char*

本秂侑毒 提交于 2019-12-20 04:38:44
问题 I want to refactor: const char* arr = "The " "quick " "brown"; into something like: const char* quick = "quick "; const char* arr = "The " quick "brown"; because the string "quick" is used is many other places. Ideally I need to be able to do this with just const primitive types, so no string. What is the best way to do this? 回答1: Compiling the comments in the form of an answer: Use a macro. #define QUICK "quick " char const* arr = "The " QUICK "brown"; Use std:string . std::string quick =

Are strtol, strtod unsafe?

余生颓废 提交于 2019-12-18 07:41:06
问题 It seems that strtol() and strtod() effectively allow (and force) you to cast away constness in a string: #include <stdlib.h> #include <stdio.h> int main() { const char *foo = "Hello, world!"; char *bar; strtol(foo, &bar, 10); // or strtod(foo, &bar); printf("%d\n", foo == bar); // prints "1"! they're equal *bar = 'X'; // segmentation fault return 0; } Above, I did not perform any casts myself. However, strtol() basically cast my const char * into a char * for me, without any warnings or

const char * changing value during loop

耗尽温柔 提交于 2019-12-11 11:52:35
问题 I have a function that iterates through a const char * and uses the character to add objects to an instance of std::map if it is one of series of recognized characters. #define CHARSEQ const char* void compile(CHARSEQ s) throw (BFCompilationError) { std::cout << "@Receive call " << s << std::endl; for(int i = 0; s[i] != '\0'; i++) { if (std::string("<>-+.,[]").find_first_of(s[i]) == std::string::npos) { throw BFCompilationError("Unknown operator",*s,i); } std::cout << "@Compiling: " << s[i] <

Dynamically allocating memory for const char string using malloc()

半世苍凉 提交于 2019-12-05 15:21:20
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 on allocating memory using malloc() and casting the memory to a constant string. I will be able to get the exact number of characters that were read from the .ini file. Is that technique okay? I don't really know what else to do. The following example runs fine in Visual Studio 2013, and prints out "hello" as desired. const char * m() { char * c =

Creating file names automatically C++

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 09:40:00
问题 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 these files as "file_1.txt", "file_2.txt", ..., "file_n.txt", where n is an integer. I can't use concatenation because the file name requires type "const char*", and I didn't find any way to convert "string" to this type. I haven't found any answer

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

牧云@^-^@ 提交于 2019-12-03 11:37:59
问题 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 cpp file has the below definition: extern const char* COUNTRY_NAME_USA = "USA"; Is method 1 wrong in some way ? What is the difference between the two ? I understand the difference between " const char * const var " , and " const char * var ". If in

Initialize const char* by concatenating another char*

北战南征 提交于 2019-12-02 05:30:13
I want to refactor: const char* arr = "The " "quick " "brown"; into something like: const char* quick = "quick "; const char* arr = "The " quick "brown"; because the string "quick" is used is many other places. Ideally I need to be able to do this with just const primitive types, so no string. What is the best way to do this? Compiling the comments in the form of an answer: Use a macro. #define QUICK "quick " char const* arr = "The " QUICK "brown"; Use std:string . std::string quick = "quick "; std::string arr = std::string("The ") + quick + "brown"; Working code: #include <iostream> #include

Are strtol, strtod unsafe?

£可爱£侵袭症+ 提交于 2019-11-29 13:28:56
It seems that strtol() and strtod() effectively allow (and force) you to cast away constness in a string: #include <stdlib.h> #include <stdio.h> int main() { const char *foo = "Hello, world!"; char *bar; strtol(foo, &bar, 10); // or strtod(foo, &bar); printf("%d\n", foo == bar); // prints "1"! they're equal *bar = 'X'; // segmentation fault return 0; } Above, I did not perform any casts myself. However, strtol() basically cast my const char * into a char * for me, without any warnings or anything. (In fact, it wouldn't allow you to type bar as a const char * , and so forces the unsafe change

GetLogicalDriveStrings() and char - Where am I doing wrongly

 ̄綄美尐妖づ 提交于 2019-11-28 04:45:00
问题 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 code: #include "StdAfx.h" #include <windows.h> #include <stdio.h> #include <conio.h> // Buffer length DWORD mydrives = 100; // Buffer for drive string storage char lpBuffer[100]; const char *extFile = "text.ext"; // You may want to try the wmain()