-1

I know that string literals are stored in read-only memory ,so you can't update them. But what's wrong with strlen() function.it works if i initialize char *s within the program. i.e

char *s="hey";
length=strlen(s);
printf("%d\n",length);// this works  

and doesn't when taking string from user

char *s;
int length;
scanf("%s",s);
length=strlen(s);
printf("%d\n",length); //this doesn't. gives segmentation fault  
Satyanarayin
  • 11
  • 1
  • 4

2 Answers2

6

You have to allocate memory where you are going to read a string. For exampe

char s[20] = { '\0' };
int length;
scanf("%19s",s);
length=strlen(s);
printf("%d\n",length); 

If you declared s like this

char *s;

then the pointer is not initialized.

If you declared s like this

char *s="hey";

then scanf will try to change the string literal that results in undefined behaviour of the program.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • thanks for reply :) what if i have few test cases .one with user input of 1000+ chars and other with 5 chars. If i use character array it wastes lot of space.any alternative(only in c)? – Satyanarayin Aug 16 '15 at 15:09
  • @Satyanarayin Dynamic memory allocation with `malloc` and `realloc` is what you're looking for. – Spikatrix Aug 16 '15 at 16:18
-2

strlen searches for the null to determine the length. If the string is not null terminated then it will give length to the size of the variable. Here in you case you didnt allocate memory,neither it is null terminated.

I hope you got what you are looking for

Grv
  • 273
  • 3
  • 14
  • thanks for reply :) what if i have few test cases .one with user input of 1000+ chars and other with 5 chars. If i use character array it wastes lot of space.any alternative(only in c)? – Satyanarayin Aug 16 '15 at 15:25
  • @Gaurav: Incorrect, if you don't `nul` terminate the behaviour of `strlen` is undefined. – Michael Petch Aug 16 '15 at 15:36
  • @Satyanarayin : There are other language specific options in C++, but in C you would likely be looking at writing a function that takes input character by character and builds the array. With your own function you can keep track of the length and resize an array allocated by `malloc` using `realloc` if need be. There are some non-portable extensions to `scanf` that allow it to allocate the memory it needs but that is only if you use gcc as a compiler (I don't recommend non standard functionality) – Michael Petch Aug 16 '15 at 15:49
  • @ Michael: can you name any such extensions.that would be a great help! – Satyanarayin Aug 16 '15 at 16:12
  • @Satyanarayin Google for the `getline()` function – Spikatrix Aug 16 '15 at 16:17
  • One non-standard C ways to do it if you use gcc (and are not compiling as C99) you can use the `a` modifier with `scanf` and allow it to return the pointer to the buffer. You can see that here http://stackoverflow.com/questions/2329909/dynamic-string-input-using-scanfas . @Cool Guy is suggesting another non standard function `getline`. That will work too but may not be available with all compilers. – Michael Petch Aug 16 '15 at 16:20
  • And of course `getline` will read an entire line. `scanf` with `%s` will only read up to the first whitespace character. So there is a difference. Not sure how you intend to use your code and what type of input you are using. – Michael Petch Aug 16 '15 at 16:25