How to copy a char array in C?

You can’t directly do array2 = array1, because in this case you manipulate the addresses of the arrays (char *) and not of their inner values (char). What you, conceptually, want is to do is iterate through all the chars of your source (array1) and copy them to the destination (array2). There are several ways to … Read more

Comparing the values of char arrays in C++

Assuming student::name is a char array or a pointer to char, the following expression compares pointers to char, after decaying sName from char[28] to char*. Given that you want to compare the strings container in these arrays, a simple option is to read the names into std::string and use bool operator==: This will work for … Read more

Returning an array using C

You can’t return arrays from functions in C. You also can’t (shouldn’t) do this: returned is created with automatic storage duration and references to it will become invalid once it leaves its declaring scope, i.e., when the function returns. You will need to dynamically allocate the memory inside of the function or fill a preallocated buffer … Read more

“Too many characters in character literal error”

I’m struggling with a piece of code and getting the error: Too many characters in character literal error Using C# and switch statement to iterate through a string buffer and reading tokens, but getting the error in this line: case ‘&&’: case ‘||’: case ‘==’: How can I keep the == and && as a … Read more

Escape Character in SQL Server

To escape ‘ you simly need to put another before: ” As the second answer shows it’s possible to escape single quote like this: result will be If you’re concatenating SQL into a VARCHAR to execute (i.e. dynamic SQL), then I’d recommend parameterising the SQL. This has the benefit of helping guard against SQL injection … Read more

Convert char array to a int number in C

I personally don’t like atoi function. I would suggest sscanf: It’s very standard, it’s in the stdio.h library 🙂 And in my opinion, it allows you much more freedom than atoi, arbitrary formatting of your number-string, and probably also allows for non-number characters at the end. EDIT I just found this wonderful question here on the site that explains and compares 3 different ways … Read more

Comparing chars in Java

If your input is a character and the characters you are checking against are mostly consecutive you could try this: However if your input is a string a more compact approach (but slower) is to use a regular expression with a character class: If you have a character you’ll first need to convert it to … Read more