4

Why is this right ?

#include<iostream>
using namespace std;
int main()
{
    char *s="raman";
    char *t="rawan";
    s=t;
    cout<<s;

return 0;
}

But this is wrong?

#include<iostream>
using namespace std;
int main()
{
    char s[]="raman";
    char t[]="rawan";
    s=t;
    cout<<s;

return 0;
}
Joe
  • 56,979
  • 9
  • 128
  • 135
Gautam Kumar
  • 1,162
  • 3
  • 14
  • 29
  • 14
    Define "right" and "wrong". – Michael Todd Aug 03 '11 at 17:50
  • 3
    You can not assign an array after initialization but you can assign a pointer. – Joe Aug 03 '11 at 17:51
  • [How do I use arrays in C++?](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c) is a good read. – R. Martinho Fernandes Aug 03 '11 at 18:00
  • 4
    The first is not right — string literals are `const char*`, not `char*`. Assigning them to `char*` is dangerous. – Cat Plus Plus Aug 03 '11 at 18:07
  • As @Cat Plus Plus, rightly says it is dangerous and compiler gives you a warning for doing that, which you ignored. **NEVER** ignore compiler warnings they are a indication of problems you might face in future. – Alok Save Aug 03 '11 at 18:13
  • 1
    Save yourself a lot of potential headaches and use `std::string`. It exists for good reasons. – hammar Aug 03 '11 at 18:19
  • 2
    @Cat Plus Plus: No, string literals are `const char[N]`, where N is the length of the literal plus 1 (to allow for the terminating `'\0'`). In most contexts, though, they decay to `const char*`, with the value being a pointer to the string's first character. For example, `sizeof "hello, world"` yields 13, *not* the size of a pointer. – Keith Thompson Aug 03 '11 at 18:23
  • @KeithThompson: Details. The point is they're `const`. – Cat Plus Plus Aug 03 '11 at 18:34
  • @Cat Plus Plus: Yes, the fact that they're `const` is important, and it's why the first program gets a warning. But the distinction between pointers and arrays is critically important, not a mere detail. – Keith Thompson Aug 03 '11 at 18:44
  • As a side note: If you read your warnigns you will see that `char *s="raman";` is depricated it should actually be `char const *s="raman";` – Martin York Aug 03 '11 at 18:59
  • In short, neither is right. The problem with the first is `char *s="raman";` This gets a warning but more importantly, it is undefined behavior. The problem with the second is `s=t;`, which is out-and-out illegal. – David Hammen Aug 03 '11 at 19:29
  • Just a reminder, you haven't accepted an answer to this question. – Keith Thompson Aug 09 '11 at 20:26
  • @gautam: You haven't accepted an answer to this question. – Keith Thompson Aug 19 '11 at 15:15

5 Answers5

15

In the first example, s=t does a pointer assignment. In the second, s=t tries to assign a pointer value (resulting from the implicit conversion, or "decay", of the array expression t) to an array object. C++ doesn't permit array assignments.

C and C++ happen to be very similar in this area; section 6 of the comp.lang.c FAQ covers the relationship between arrays and pointers very well.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • Just an FYI: I meant to decline your last flag (the one that says an answer is deliberately deceptive), but I accidentally marked it helpful. Flags should not be used to indicate technical inaccuracies, or an altogether wrong answer. – NullUserException Dec 08 '11 at 00:06
  • @NullUserException: Understood, thanks for the notice. I think the situation has been resolved anyway. You can delete your comment if you like. – Keith Thompson Dec 09 '11 at 09:03
7

The first example assigns an pointer to another which is valid.

The second example assigns an array to another array which in not allowed in C & C++ both.


This excellent C++ FAQ entry and this answer should be a good read for you.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
2

In addition to what the other guys said:

Contrary to popular belief, arrays are actually not pointers. They just share a lot of similarities when working with them and have a couple of implicit conversions to pointers which is why it's easy to work with them as if they are pointers.

Arrays are a standalone feature of (C and) C++. It doesn't behave exactly like a pointer would.

For example, it's possible to allocate array objects on the stack, which is not possible when you allocate objects using new (which returns a pointer) and pointers.

And the example that you showed is another one: You can't use arrays as if they are pointers. But you can use pointers to point to a continuous piece of memory (array).

TravisG
  • 2,373
  • 2
  • 30
  • 47
1

char *s means:
address and value both are not constant.

const char *s:

value is constant, not address.

const char * const s;

address and value both are constant.

char *s[] 

is an array. Array base address is always contant. You can not change the base address of it, that is not allowed in c.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
kapilddit
  • 1,729
  • 4
  • 26
  • 51
1

Array name is a const pointer. meaning, when you declare an array, the name is a pointer, which cannot be altered.

keeda
  • 2,605
  • 5
  • 28
  • 27