c-strings

How to extract a substring from a string in C?

末鹿安然 提交于 2019-11-28 12:16:16
I tried using strncmp but it only works if I give it a specific number of bytes I want to extract. char line[256] = This "is" an example. //I want to extract "is" char line[256] = This is "also" an example. // I want to extract "also" char line[256] = This is the final "example". // I want to extract "example" char substring[256] How would I extract all the elements in between the ""? and put it in the variable substring? Floris Note: I edited this answer after I realized that as written the code would cause a problem as strtok doesn't like to operate on const char* variables. This was more an

strncpy or strlcpy in my case

微笑、不失礼 提交于 2019-11-28 11:57:14
what should I use when I want to copy src_str to dst_arr and why? char dst_arr[10]; char *src_str = "hello"; PS: my head is spinning faster than the disk of my computer after reading a lot of things on how good or bad is strncpy and strlcpy . Note: I know strlcpy is not available everywhere. That is not the concern here. strncpy is never the right answer when your destination string is zero-terminated. strncpy is a function intended to be used with non-terminated fixed-width strings. More precisely, its purpose is to convert a zero-terminated string to a non-terminated fixed-width string (by

Why can't I edit a char in a char*?

故事扮演 提交于 2019-11-28 04:00:17
问题 Below is an exceedingly simple example. It compiles fine using gcc on Mac OS X (Snow Leopard). At runtime it outputs Bus error: 10. What's happening here? char* a = "abc"; a[0] = 'c'; 回答1: Your code sets a to a pointer to "abc" , which is literal data that can't be modified. The Bus error occurs when your code violates this restriction, and tries to modify the value. try this instead: char a[] = "abc"; a[0] = 'c'; That creates a char array (in your program's normal data space), and copies the

How do strings and char arrays work in C?

旧城冷巷雨未停 提交于 2019-11-28 01:10:24
No guides I've seen seem to explain this very well. I mean, you can allocate memory for a char* , or write char[25] instead? What's the difference? And then there are literals, which can't be manipulated? What if you want to assign a fixed string to a variable? Like, stringVariable = "thisIsALiteral" , then how do you manipulate it afterwards? Can someone set the record straight here? And in the last case, with the literal, how do you take care of null-termination? I find this very confusing. EDIT: The real problem seems to be that as I understand it, you have to juggle these different

What is the reason for not being able to deduce array size from initializer-string in member variable?

白昼怎懂夜的黑 提交于 2019-11-28 00:44:05
Consider the code: struct Foo { const char str[] = "test"; }; int main() { Foo foo; } It fails to compile with both g++ and clang++, spitting out essentially error: array bound cannot be deduced from an in-class initializer I understand that this is what the standard probably says, but is there any particular good reason why? Since we have a string literal it seems that the compiler should be able to deduce the size without any problem, similarly to the case when you simply declare an out-of-class const C-like null terminated string. sbabbi The reason is that you always have the possibility to

Can a std::string contain embedded nulls?

╄→尐↘猪︶ㄣ 提交于 2019-11-27 22:40:15
For regular C strings, a null character '\0' signifies the end of data. What about std::string , can I have a string with embedded null characters? Yes you can have embedded nulls in your std::string . Example: std::string s; s.push_back('\0'); s.push_back('a'); assert(s.length() == 2); Note: std::string 's c_str() member will always append a null character to the returned char buffer; However, std::string 's data() member may or may not append a null character to the returned char buffer. Be careful of operator+= One thing to look out for is to not use operator+= with a char* on the RHS. It

Why should one use std::string over c-style strings in C++?

荒凉一梦 提交于 2019-11-27 16:18:11
问题 "One should always use std::string over c-style strings( char * )" is advice that comes up for almost every source code posted here. While the advice is no doubt good, the actual questions being addressed do not permit to elaborate on the why? aspect of the advice in detail. This question is to serve as a placeholder for the same. A good answer should cover the following aspects(in detail): Why should one use std::string over c-style strings in C++? What are the disadvantages (if any) of the

Returning 'c_str' from a function

白昼怎懂夜的黑 提交于 2019-11-27 15:10:47
This is from a small library that I found online: const char* GetHandStateBrief(const PostFlopState* state) { static std::ostringstream out; // ... rest of the function ... return out.str().c_str() } In my code I am doing this: const char *d = GetHandStateBrief(&post); std::cout<< d << std::endl; Now, at first d contained garbage. I then realized that the C string I am getting from the function is destroyed when the function returns because std::ostringstream is allocated on the stack. So I added: return strdup( out.str().c_str()); And now I can get the text I need from the function. I have

Print part of a string in C

我的未来我决定 提交于 2019-11-27 14:48:32
问题 Is there a way to only print part of a string? For example, if I have char *str = "hello there"; Is there a way to just print "hello" , keeping in mind that the substring I want to print is variable length, not always 5 chars? I know that I could use a for loop and putchar or that I could copy the array and then add a null-terminator but I'm wondering if there's a more elegant way? 回答1: Try this: int length = 5; printf("%*.*s", length, length, "hello there"); 回答2: This will work too: fwrite

Is sprintf(buffer, “%s […]”, buffer, […]) safe?

无人久伴 提交于 2019-11-27 13:35:42
I saw use of this pattern to concatenate onto a string in some code I was working on: sprintf(buffer, "%s <input type='file' name='%s' />\r\n", buffer, id); sprintf(buffer, "%s</td>", buffer); and I'm fairly certain it's not safe C. You'll notice that buffer is both the output and the first input. Apart from the obvious possibility of a buffer overflow , I believe there is no guarantee that buffer doesn't get changed between the start and the end of the function (i.e., there is no guarantee as to what the state of buffer will be during the execution of the function). The signature of sprintf