2

version 1

char *cad1="hell";
  char *cad2="home";
  int j;
  cad2=cad1;
  for (j=0;j<4;j++){
      printf("%c",cad1[j]);
  }
  cad2[0]='a';
  for (j=0;j<4;j++){
      printf("%c",cad2[j]);
  }

version 2

 char cad1[]="hell";
  char cad2[]="home";
  int j;
  cad2=cad1;
  for (j=0;j<4;j++){
      printf("%c",cad1[j]);
  }
  cad2[0]='a';
  for (j=0;j<4;j++){
      printf("%c",cad2[j]);
  }

version 3

 char cad1[]="hell";
  char *cad2="home";
  int j;
  cad2=cad1;
  for (j=0;j<4;j++){
      printf("%c",cad1[j]);
  }
  cad2[0]='a';
  for (j=0;j<4;j++){
      printf("%c",cad2[j]);
  }

The question that I have is why version 1 hangs the dev c++?, version 2 says there is an incompatible assignment in cad2=cad1? and why version 3 works normal?

Layla
  • 5,234
  • 15
  • 51
  • 66
  • 2
    Possible duplicate of [C-FAQ: Arrays and Pointers](http://c-faq.com/aryptr/index.html) and http://c-faq.com/decl/strlitinit.html – Elazar Jun 28 '13 at 15:34
  • 1
    1) `warning: initialization discards ‘const’ qualifier from pointer target type` 2) `incompatible types when assigning to type ‘char[5]’ from type ‘char *’` – Karoly Horvath Jun 28 '13 at 15:39
  • possible duplicate of [Why do I get a segmentation fault when writing to a string?](http://stackoverflow.com/questions/164194/why-do-i-get-a-segmentation-fault-when-writing-to-a-string) – Lundin Jun 28 '13 at 15:45

4 Answers4

6

When you declare pointer like,

char *cad1="hell";

"hell" is called as constant string literal and so may be stored in read-only memory. - compiler is free to choose whatever it likes.

But when you declare it as,

char cad2[]="hell";

"hell" is stored as array member. ie, it will be stored as,

cad[0] = 'h', cad[1] = 'e', cad[2] = 'l', cad[3] = 'l', cad[4] = '\0'

C doesn't guarantee any defined behavior for changing constant literals. It may crash hang or spoil other valid data. Its called as undefined behavior.

Since you are changing cad1 which is pointing to constant literal your application hangs.


In version 2, both cad1 and cad2 are of array type. Direct array assignments in C is illegal. So you got error. Refer this link for all details as mentioned by others.


To answer why version 3 works,

cad1 is an array and cad2 is pointer here. By the statement cad2 = cad1 you made cad2 to point the memory which can be modified ( still, size is restricted). So changing cad1 and cad2 are same as they point same modifiable memory.

VoidPointer
  • 3,037
  • 21
  • 25
0

In version 1, cad2 is equal to cad1 which points to the constant string "hell". Later on, you attempt to modify that constant string, which is unpredictable. Version 3, in contrast, has cad1 declared as a char array, so you get a non-constant copy of the string, so modifying it will work.

For version 2, it's probably because both are arrays (not pointers), so I'm sure there's some issues there.

Drew McGowen
  • 11,471
  • 1
  • 31
  • 57
0

If cad is declared as char* cad="hell"; then that is a string literal (of length 4 plus 1 for a null terminator) and any attempt to modify a string literal is undefined behaviour. Anything could happen.

char cad[]="home"; will allocate 5 chars on the stack, cad[4] is '\0' - the null terminator; used by many string functions in C in modelling a set of chars as a string to mark the string end. You are free to modify these data although changing cad[4] will cause you trouble when using C string library functions as you will have removed their stopping condition.

Throughout your code you have cad2=cad1; Note that this does not copy the string, just the pointer; use strcpy in the C standard library to copy strings.

Really you should write const char* cad="hell";. Newer c++ compilers will insist on it.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0
  • why version 1 hangs the dev c++?

Read comments:

char *cad1="hell";  // pointer to constant string "hell"
char *cad2="home";
cad2=cad1;          // now cad2 points to constant string "hell" too
cad2[0]='a';        // modifying of constant string causes undefined behaviour.
  • version 2 says there is an incompatible assignment in cad2=cad1?

Read comments:

char cad2[]="home";  // cad2 is array
cad2=cad1;           // error because you can not assign to arrays in C.
  • why version 3 works normal?

Read comments:

char cad1[]="hell";  // cad1 is array
char *cad2="home";
cad2=cad1;           // now cad2 point to first element of array cad1
cad2[0]='a';         // you can modify arrays in C

Note that you can not assign to arrays but you can modify them by copying or assigning values to their elements.

kotlomoy
  • 1,420
  • 8
  • 14