problems with char array = char array

You can’t assign anything to an array variable in C. It’s not a ‘modifiable lvalue’. From the spec, ยง6.3.2.1 Lvalues, arrays, and function designators: An lvalue is an expression with an object type or an incomplete type other than void; if an lvalue does not designate an object when it is evaluated, the behavior is … Read more

Why does this code with ‘1234’ compile in C++?

C++ has something called “multicharacter literals”. ‘1234’ is an example of one. They have type int, and it is implementation-defined what value they have and how many characters they can contain. It’s nothing directly to do with the fact that characters are represented as integers, but chances are good that in your implementation the value … Read more

Does C have a string type? [closed]

C does not and never has had a native string type. By convention, the language uses arrays of char terminated with a null char, i.e., with ‘\0′. Functions and macros in the language’s standard libraries provide support for the null-terminated character arrays, e.g., strlen iterates over an array of char until it encounters a ‘\0’ … Read more

How do I apply the for-each loop to every character in a String?

The easiest way to for-each every char in a String is to use toCharArray(): for (char ch: “xyz”.toCharArray()) { } This gives you the conciseness of for-each construct, but unfortunately String (which is immutable) must perform a defensive copy to generate the char[] (which is mutable), so there is some cost penalty. From the documentation: … Read more

Hex to char array in C

You can’t fit 5 bytes worth of data into a 4 byte array; that leads to buffer overflows. If you have the hex digits in a string, you can use sscanf() and a loop: #include <stdio.h> #include <ctype.h> int main() { const char *src = “https://stackoverflow.com/questions/1557400/0011223344”; char buffer[5]; char *dst = buffer; char *end = … Read more

tech