The following code doesn't compile:
template <int N>
void f(char[N]) {}
int main() {
char buf[10];
f(buf);
}
If I change the char[N]
to char (&)[N]
, it works. So what the difference between them?
When to use what? If your column will store a fixed-length Unicode characters like French, Arabic and so on characters then go for NCHAR. If the data stored in a column is Unicode and can vary in length, then go for NVARCHAR. Querying to NCHAR or NVARCHAR is a bit slower then CHAR or VARCHAR.
Difference between CHAR and VARCHAR datatypes: In CHAR, If the length of the string is less than set or fixed-length then it is padded with extra memory space. In VARCHAR, If the length of the string is less than the set or fixed-length then it will store as it is without padded with extra memory spaces.
CHAR columns are fixed in size, while VARCHAR and VARCHAR(MAX) columns support variable-length data. CHAR columns should be used for columns that vary little in length. String values that vary significantly in length and are no longer than 8,000 bytes should be stored in a VARCHAR column.
The key difference between varchar and nvarchar is the way they are stored, varchar is stored as regular 8-bit data(1 byte per character) and nvarchar stores data at 2 bytes per character. Due to this reason, nvarchar can hold upto 4000 characters and it takes double the space as SQL varchar.
You have been bitten by backwards compatibility with C. When you declare a function like:
int f(char c[10]);
You declare a function whose argument is of type char *
. The compiler decays the argument type for you. The problem is:
int f(char c[5]);
declares the same function. This is the way C worked, and C++ retained it for compatability.
int f(char (&c)[10]);
Declares a function whose argument is of type "reference to array (length 10) of char". C didn't have references, so there is no need to maintain backwards compatibility.
int f(char (&c)[5]);
Declares a different function - with a different argument type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With