Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

What's the difference between char * and char [ ]?


Asked by Jabari Cuevas on Nov 30, 2021 FAQ



char* ptr = "A string"; makes a pointer which points to a globaly allocated string (which may or may not be pooled with other identical strings by the compiler). No string copy takes place with this code, but editing the string data is not safe, as changes to it may change some strings that it's pooled with.
Additionally,
The s [] is an array, but *s is a pointer. For an example, if two declarations are like char s [20], and char *s respectively, then by using sizeof () we will get 20, and 4. The first one will be 20 as it is showing that there are 20 bytes of data.
Consequently, char *str = "Test"; is a pointer to the literal (const) string "Test". The main difference between them is that the first is an array and the other one is a pointer. The array owns its contents, which happen to be a copy of "Test", while the pointer simply refers to the contents of the string (which in this case is immutable).
Just so,
Difference between CHAR and VARCHAR dataypes. 1. 2. In CHAR, If the length of string is less than set or fixed length then it is padded with extra memory space. In VARCHAR, If the length of string is less than set or fixed length then it will store as it is without padded with extra memory spaces.
Indeed,
With char arr[10], arrdoes not "hold" the address; rather, it is implicitly converted into a pointer in a variety of perhaps-unexpected ways. char* ptr = "HELLO"will work basically the same as char *ptr; ptr = "HELLO"as in the answer, except that the pointer is initialized to point at the string literal instead of being assigned afterward.