Questions tagged [c-strings]

A string in the programming language C is represented as a sequence of characters followed by a null terminator (represented as \0).

2715 questions
31
votes
5 answers

Converting String to Cstring in C++

I have a string to convert, string = "apple" and want to put that into a C string of this style, char *c, that holds {a, p, p, l, e, '\0'}. Which predefined method should I be using?
teamaster
  • 403
  • 2
  • 6
  • 16
29
votes
4 answers

How to use memset while handling strings in C++?

I am from Python background and recently learning C++. I was learning a C/C++ function called memset and following the online example from website https://www.geeksforgeeks.org/memset-in-cpp/ where I got some compilation errors: /** * @author …
BhishanPoudel
  • 15,974
  • 21
  • 108
  • 169
29
votes
5 answers

Determine #defined string length at compile time

I have a C-program (an Apache module, i.e. the program runs often), which is going to write() a 0-terminated string over a socket, so I need to know its length. The string is #defined as: #define POLICY "\n" \ "
Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
28
votes
4 answers

Strip first and last character from C string

I have a C string that looks like "Nmy stringP", where N and P can be any character. How can I edit it into "my string" in C?
igul222
  • 8,557
  • 14
  • 52
  • 60
25
votes
6 answers

strncpy or strlcpy in my case

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.…
hari
  • 9,439
  • 27
  • 76
  • 110
25
votes
3 answers

Is there anyway to create null terminated string in Go?

Is there anyway to create null terminated string in Go? What I'm currently trying is a:="golang\0" but it is showing compilation error: non-octal character in escape sequence: "
Yash Srivastava
  • 850
  • 2
  • 9
  • 15
25
votes
3 answers

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

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…
vsoftco
  • 55,410
  • 12
  • 139
  • 252
25
votes
3 answers

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

I saw use of this pattern to concatenate onto a string in some code I was working on: sprintf(buffer, "%s \r\n", buffer, id); sprintf(buffer, "%s", buffer); and I'm fairly certain it's not safe C. You'll notice…
Paul Fisher
  • 9,618
  • 5
  • 37
  • 53
24
votes
4 answers

Where does const char* get the pointer to a memory address?

This may be simple question, but why does a const char* not need a memory address to point to? Example: const char* a = "Anthony"; and not: const char *a = // Address to const char like any other types do?
Weidelix
  • 401
  • 3
  • 6
23
votes
2 answers

Why Doesn't string::data() Provide a Mutable char*?

In c++11 array, string, and vector all got the data method which: Returns pointer to the underlying array serving as element storage. The pointer is such that range [data(); data() + size()) is always a valid range, even if the container is empty. …
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
22
votes
9 answers

'\0' and printf() in C

In an introductory course of C, I have learned that while storing the strings are stored with null character \0 at the end of it. But what if I wanted to print a string, say printf("hello") although I've found that that it doesn't end with \0 by…
xrfxlp
  • 421
  • 5
  • 15
22
votes
9 answers

If char*s are read only, why can I overwrite them?

My course taught me that char*s are static/read only so I thought that would mean you can't edit them after you have defined them. But when I run: char* fruit = "banana"; printf("fruit is %s\n", fruit); fruit = "apple"; printf("fruit is %s\n",…
sally2000
  • 788
  • 4
  • 14
21
votes
6 answers

Selecting only the first few characters in a string C++

I want to select the first 8 characters of a string using C++. Right now I create a temporary string which is 8 characters long, and fill it with the first 8 characters of another string. However, if the other string is not 8 characters long, I am…
user3483203
  • 50,081
  • 9
  • 65
  • 94
21
votes
4 answers

C string to uppercase in C and C++

While I was putting together a to-uppercase function in C++ I noticed that I did not receive the expected output in C. C++ function #include #include #include void strupp(char* beg) { while (*beg++ =…
Alex Koukoulas
  • 998
  • 1
  • 7
  • 21
20
votes
4 answers

Proper way to copy C strings

Is there an easy way to copy C strings? I have const char *stringA, and I want char *stringB to take the value (note that stringB is not const). I tried stringB=(char*) stringA, but that makes stringB still point to the same memory location, so when…
Cannoliopsida
  • 3,044
  • 5
  • 36
  • 61