0

when any array(of int or char type) is made like this

char a[]="hello";
int b[20];

then is there two pointer variables made a and b which points to start of array?

and if yes then why this gives an error if i do as following

char a[];
a="hello";

but in case of following no error shows

string c;
c="hello";

please also explain how arrays of int and char are passed in functions.

what if a function returns a char *. does this mean it will be pointing to a character or it means it'll point to a char array?

bolov
  • 72,283
  • 15
  • 145
  • 224
  • 2
    `char[]` is a C array. There is no assignment operator for C arrays (neither in C nor in C++). `std::string` is a `class`. It has an overloaded assignment operator. The assignment operator for `char[]` (or `char*`) cannot be overloaded (even not by yourself) - the language simply prohibits this. TL;DR: `char[]` is inherited from C. `std::string` is the better replacement in C++ and should be preferred. FYI: [std::string::operator=](https://en.cppreference.com/w/cpp/string/basic_string/operator%3D) – Scheff's Cat Apr 11 '21 at 07:14
  • _what if a function returns a char *. does this mean it will be pointing to a character or it means it'll point to a char array?_ Either or - both is possible. Strictly speaking, it points to a character. Whether it's start of a sequence of multiple consecutive characters or not cannot be deduced from this. You have to inspect the function code (or read the documentation) to find out. – Scheff's Cat Apr 11 '21 at 07:18
  • The _usual convention_ is that a single character would be returned as `char`. If it returns a `char*` it's _usually_ a sequence of characters. You still have to determine the length somehow. Either it's known, or it's returned elsewhere, or it can be determined inspecting the returned `char*` -> read all consecutive `char`s until a special character value (like `'\0'`) is found. – Scheff's Cat Apr 11 '21 at 07:22
  • Btw. all these things should be mentioned in a valuable [C++ book](https://stackoverflow.com/a/388282/7478597)... ;-) – Scheff's Cat Apr 11 '21 at 07:24
  • @Scheff i understood. but can you please answer when any array(of int or char type) is made like this.. char a[]="hello"; int b[20]; then is there two pointer variables made a and b which points to start of array? – Anshul Pareek Apr 11 '21 at 14:23
  • and also sometimes i see that we pass char array or int array in a function like this.. void fun(int/char a[]) but sometimes we pass like this void fun(int/char *a)!!! why this is so and please explain whats the difference?? – Anshul Pareek Apr 11 '21 at 14:24
  • also what the meaning if a function returns const char* and this char* – Anshul Pareek Apr 11 '21 at 14:29
  • Neither C nor C++ supports array arguments in functions. Regardless whether you declare `void f(char[])` or `void f(char*)`, the compiler will read this in every case as pointer. (That's a special and exclusive rule for C arrays in function parameters.) – Scheff's Cat Apr 11 '21 at 15:34
  • Concerning `char a[] = "hello";`: That's a variable declaration with initialization (and not an assignment). This might be confusing (due to the similar looking syntax) but the compiler carefully distinguishes initialization and assignment. 1.) Array initialization _is_ supported (in contrast to array assignment). 2.) The compiler will determine the correct size of array `a` by evaluating the right side of the `=`. – Scheff's Cat Apr 11 '21 at 15:40
  • can i typecast const char * to char* – Anshul Pareek Apr 15 '21 at 07:24
  • You can but you shouldn't. If you have a `const char*` where you expect a `char*` this is an indication for bad design or something which is doomed to fail. A `const char*` indicates memory which may be read only. A `char*` indicates memory which might be subject of changes. If changes are applied to (possible) read-only memory, you introduce Undefined Behavior. Even if the `const char*` was a pessimization somewhere else it's still failed design. – Scheff's Cat Apr 15 '21 at 07:27
  • Concerning the above: [**Demo on coliru**](http://coliru.stacked-crooked.com/a/89882895db9b4163) – Scheff's Cat Apr 15 '21 at 07:32
  • actually i'm doing this in my code. string s="hi it's rainy"; char *p=strtok((char * )(s.c_str()," "); to use strtok function i have to first convert my string object into a char array but c_str method returns a const pointer to the underlying array in string object. and i can't pass the const * in strtok(). i can only pass char *. so i have to do typecasting – Anshul Pareek Apr 15 '21 at 07:39
  • A `std::string` isn't a `const char*`. It manages writable storage internally. So, if carefully used, this can be done: [**Demo on coliru**](http://coliru.stacked-crooked.com/a/6029038e46b5857e). But it's still bad code with potential pitfalls and the invitation for (later) trouble. (Please, note that I didn't need a conversion of `const char*` to `char*`. The `std::string::operator[]` provides a `char&` which permits write access.) – Scheff's Cat Apr 15 '21 at 07:45
  • `strtok()` is a function adopted from the C API. It's better to replace it by clean C++ code: [SO: Using strtok with a std::string](https://stackoverflow.com/a/289372/7478597) – Scheff's Cat Apr 15 '21 at 07:53

0 Answers0