c-strings

Convert char** (c) of unknown length to vector<string> (c++) [duplicate]

扶醉桌前 提交于 2019-12-12 09:39:53
问题 This question already has answers here : converting an array of null terminated const char* strings to a std::vector< std::string > (4 answers) copying c array of strings into vector of std::string (4 answers) Closed 6 years ago . How would one go about converting a C char** to a C++ vector? Is there some built-in functionality one can utilize to do this, or is it better to accomplish it through a series of iterative steps? EDIT: For various reasons, the number of elements in the C array is

How to declare and init two dimensional array of strings?

半世苍凉 提交于 2019-12-11 19:27:15
问题 i need something like this const char **nodeNames[] = { {"RootNode", "Reference", "Node_1", "Node_2", "Node_3"}, {"RootNode", "Hips", "Node_1", "Node_2", "Node_3"}, {"RootNode", "Heviest", "Node_1", "Node_2", "Node_3"}, }; but with previous declaration, i got an error. And how can i reference to it in code? 回答1: Looks like you want a two dimensional array of const char* : const char *nodeNames[][5] = { // ^^ this dimension can be deduced by the compiler, the rest not {"RootNode", "Reference",

Shuffle a string input by user

那年仲夏 提交于 2019-12-11 15:35:37
问题 #include <stdio.h> #include <stdlib.h> #include <string.h> char rand(char x); int main() { char input[80] = {0}; char rando[80] = {0}; char choice = 0; char rando2[80] = {0}; if(strlen(input) >= 10 && strlen(input) <= 80); { printf("Please enter a string with 10-80 characters: "); scanf("%s", input); printf("Orginal string: %s\n", input); rando = my_rand(input); printf("New string: %s\n", rando); } else{ return 0; } printf("Would you like to shuffle this string again?(y or n): "); scanf("%c\n

Stange behavior with my C string reverse function

柔情痞子 提交于 2019-12-11 14:43:22
问题 I'm just an amateur programmer... And when reading, for the second time, and more than two years apart, kochan's "Programming in Objective-C", now the 6th ed., reaching the pointer chapter i tried to revive the old days when i started programming with C... So, i tried to program a reverse C string function, using char pointers... At the end i got the desired result, but... got also a very strange behavior, i cannot explain with my little programming experience... First the code: This is a .m

Why is my std::string obtained via stream being overwritten?

孤街浪徒 提交于 2019-12-11 13:17:19
问题 Assume I have a function like so: std::string get_shader(std::string path) { std::string fullpath = "./resources/shaders/" + path; std::ifstream vertexShaderFile(fullpath); std::ostringstream vertexBuffer; vertexBuffer << vertexShaderFile.rdbuf(); return vertexBuffer.str(); } And then some code like this: GLuint vertex_shader; GLuint fragment_shader; GLuint program; const GLchar * vertex_shader_source = get_shader("triangle_vertex.vs").c_str(); // At this point vertex_shader_source is correct

Calculating the resistor value with its color bands as input

天大地大妈咪最大 提交于 2019-12-11 12:38:07
问题 I'm trying to develop a C program that calculates the resistor values by inputting the colour bands marked on the resistor. Ignoring the resistor tolerance. e.g. Enter the colours of the resistor’s three bands, beginning with the band nearest the end. Type the colours in lowercase letters only, NO CAPS Band 1 => green Band 2 => black Band 3 => yellow Resistance value: 500 000 -ohms Do you want to decode another resistor (Y/N)? => Y Displaying the required format of the resistor value. For

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] <

Is c_str() on a concatenated string literal safe?

为君一笑 提交于 2019-12-11 10:14:25
问题 I know from this answer that string literals are allocated statically. But is the following string concatenation also safe to use? void someFunction(std::string& foo) { functionTakingCString(("start " + foo + " end").c_str()); } Follow Up Question : As stated in the comments, this would be indeed unsafe when functionTakingCString would store that pointer. In this case, would the following be valid: void someFunction(std::string& foo) { std::string bar = "start " + foo + " end";

Sorting Strings in a static array based on their length crashes? |wrong allocation/access|

主宰稳场 提交于 2019-12-11 09:14:32
问题 I wanted to make an array of strings and sort them in order based on their length (smallest -> biggest) but the program crashes after all inputs. And also it bypasses Element 0 (starts directly from element 1 during input) #include <stdio.h> #include <stdlib.h> main() { int i,j,N; printf("\nInput amount of alphanumericals: "); scanf("%d",&N); { int min; char *swap=(char*)malloc(sizeof(char)*150); char *A[N],**temp; for(i=0;i<N;i++) *(A+i)=malloc(sizeof(char)*N);//Error Here temp=A; for(i=0;i

Pascal strings in C#

孤人 提交于 2019-12-11 06:32:25
问题 I'm working on an audio library in C# and am working on the WAVE file format ATM. According to the very informative website that I am using, WAVE using Pascal style strings. I have been Googling for over 20 minutes and have come up only with a C to Pascal converter. I know how Pascal and C strings compare (C code): char *cStr = "Test"; // C - produces {'T', 'e', 's', 't', '\0'} char pascal[] = {4, 'T', 'e', 's', 't'}; // Pascal If it helps, I am using System.IO.BinaryReader to parse the file.