1

i have written a code . this looks as follows .

#include<stdio.h>

#include<string.h>

 int do_print2(char *q[]);
 int main()
{

  char *p[]={"pointervaule","NAM", "JAM", "CALM"};
  do_print2(p);
  return 1; 
}

int do_print2(char *p[])
{ 
    printf("this is print1 char *p \n");
    strcat(p[0],"added");
    printf("%s\n", (p[0]));
    return 1;

}

after compilation, i am trying to run it, i am getting segmentation fault. help me in learning what is the reason for that error. thank in advance.

Srinivas Thanneeru
  • 161
  • 1
  • 2
  • 12

3 Answers3

4

In your code: strcat(p[0],"added"); try to write on read only memory that is illegal in C. Because p[0] points to a constant string.

Not p is pointer to char array, but not 2-dimensional char array.

Read: Difference between char* str[] and char str[][] and how both stores in memory? an answer with diagrams and code examples, to understand it better.

Community
  • 1
  • 1
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • hii Grijesh chauhan, firstly very thank full for helping me in fixing this error, and can u let me know a book or some material kind of thing that makes me comfortable in dealing with pointer and strings. ( any tutorial ? ) – Srinivas Thanneeru Jul 18 '13 at 20:55
  • @SrinivasThanneeru A huge list: [The Definitive C Book Guide and List](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and for [pointers](http://stackoverflow.com/questions/tagged/pointers) – Grijesh Chauhan Jul 18 '13 at 20:59
  • @SrinivasThanneeru If one need to write code in C he/she should read [FAQ](http://c-faq.com/) regularly – Grijesh Chauhan Jul 18 '13 at 21:03
3

The OS says that the C strings are in the read section of the object (i.e. protected from writing).

Due to historical reasons "bla bla" is really a const char * const data type, but is allowed to get away in the C compilers eyes some teenage interdependencies. But the headmaster (OS) is less forgiving and expels the running of such code in the corridors. (how many metaphors in that statement).

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • In `C` the type of string literal is `char[N]`. It is an array, not a pointer, but there's no `const` in the type. With `gcc` you can force the compiler to treat string litterals as `'const char *'`. The option `-Wwrite-strings` will do that. While it is **not** standard C, it is really useful to make the code more robust. – Grijesh Chauhan Jul 18 '13 at 21:10
  • I really meant it is in the read section of code i.e. not on the stack. – Ed Heal Jul 18 '13 at 21:24
  • Ok just wanted to inform about this, some people are not aware of this and also confuse between c and c++, (because in C++ string literals are `const char *`). Btw you have nice lines in second para. – Grijesh Chauhan Jul 18 '13 at 21:37
1

You can't write to read only memory, better way to do this:

#include<stdio.h>

#include<string.h>

int do_print2(char q[][20]);
int main()
{
  char p[4][20] = {{0}, {0}, {0}, {0}};
  strcat(p[0],"pointervaule");
  strcat(p[1],"NAM");
  strcat(p[2],"JAM");
  strcat(p[3],"CALM");
  do_print2(p);
  return 1; 
}

int do_print2(char p[][20])
{ 
   printf("this is print1 char *p \n");
   strcat(p[0],"added");
   printf("%s\n", (p[0]));
   return 1;

}
Kostia
  • 271
  • 1
  • 9