-1
char *str;
while(1)
{
    printf("$$$$>");
    scanf("%s",str);
}

In this code I just want to print a command prompt. When user inputs something nothing happens and the command prompt is printed again. But the scanf() runs once and then $$$$> is printed in loop. The code runs when I tried to take a character array instead of str pointer. why?

RaKo
  • 49
  • 1
  • 2
  • 11

3 Answers3

4

char *str;

You used str without initializing it. Using an uninitialized pointer in another function like scanf is a recipe for program to crash.

You can fix the issue by malloc (dynamically allocating) for str pointer, but for such simple usage, you can use array instead.

char str[100] = "";
while(1)
{
    printf("$$$$>");
    // scanf("%s",str);  // not recommended
    fgets( str, sizeof( str ), stdin );  // fgets is better
}
artm
  • 17,291
  • 6
  • 38
  • 54
  • @chux agree, changed – artm Jul 31 '16 at 11:30
  • thanks for ans. but i have 1 more doubt. what is the difference b/w char str[100]=""; and char str[100]; both gives the same output though] – RaKo Jul 31 '16 at 16:57
  • @RavindraKoranga `str[100]="";` makes sure the string is initialised to empty (ie all characters are set to 0) before any operations. It's a good practice to initialise variables before usage. – artm Jul 31 '16 at 19:15
  • char *str; str=(char *)malloc(sizeof(char)*100); fgets( str, sizeof( str ), stdin ); printf("%s",str);when i run this code only 7 characters are printed even if i take large input.Can you tell me the reason? – RaKo Aug 02 '16 at 07:06
  • that's because you use `str` as pointer; `sizeof( str )` is then only 8 in your machine, thus you can input only 7 characters. The `sizeof( str )` would work as I declared `str` as an array. If you stick with pointer then you must provide 100 yourself instead of using `sizeof` – artm Aug 02 '16 at 07:26
2

Becase you didn't allocate a memory buffer that str can point at, add this line beneath str decleration:

str=malloc(sizeof(char)*1000);
Dr.Haimovitz
  • 1,568
  • 12
  • 16
  • `sizeof(char)` is always 1, so it does not add value to the code. If one wants to match the type of `str`, just use `str=malloc(sizeof * str * 1000);` – chux - Reinstate Monica Jul 30 '16 at 16:49
  • You are so wrong... size of char is 1 indeed, thats for code readability... and str is an adress so you just allocated 1000* adress-value that can be milions of bytes... and also you dont need to "match" any type... you obviously dont know c.. – Dr.Haimovitz Jul 31 '16 at 02:46
  • 1
    Hmmm "obviously dont know c" --> See [badge](http://stackoverflow.com/help/badges/96/c?userid=2410359) and "and str is an adress so you just allocated 1000* adress-value" --> no, allocated 1000 times the _type_ of the de-referenced `str`, which in this case `sizeof * str * 1000` --> `1 * 1000`, not "can be millions of bytes" per your [comment](http://stackoverflow.com/questions/38673272/using-printf-and-then-scanf-in-loop/38673321?noredirect=1#comment64740319_38673321). Suggest testing with `printf("%zu\n", sizeof *str * 1000);` – chux - Reinstate Monica Jul 31 '16 at 03:40
  • I assert `str = malloc(sizeof *str * 1000);` is easier to code, less likely to code wrong, easier to review and maintain than `str=malloc(sizeof(char)*1000);` - IMO of 30+ years of C experience. – chux - Reinstate Monica Jul 31 '16 at 03:45
  • A difficulty with `str=malloc(sizeof(char)*1000)` is that `str` needs to be a `char *`. With `malloc(sizeof *str * 1000)`, the memory allocated is correct regardless of the type of data pointed to by `str`, thus making it 1) correct at initial coding 2) review does not need to check is correct matching type was used. 3) Code maintenance is simplified should the type of `str` change. 4) There is no _need_ for code to indicate the type with `malloc()`. – chux - Reinstate Monica Jul 31 '16 at 04:17
  • 2
    Rather than comment on the commenter with "you obviously dont know c" and snides like "im very much aware that thers a lot of bad programers out there", better to focus on the comment/post/answer themselves. That is professional and this site has [be nice](http://stackoverflow.com/help/be-nice) policy. – chux - Reinstate Monica Jul 31 '16 at 04:21
  • When you allocating an array with a fixed size you know what is the type of your pointer so there is no reason to do that.. 1) it is correct but not allowing you to know what types the array will be storing 2) ofc ther`s no need to check, but makes it very easy to mistakenly allocate wrong number of elements because of the "over generics"3) simplified but can and will cause problems – Dr.Haimovitz Jul 31 '16 at 04:31
  • Im truly sorry for what i wrote in the first comment. – Dr.Haimovitz Jul 31 '16 at 04:34
  • Suggest reviewing [Why is it safer to use sizeof(*pointer) in malloc](http://stackoverflow.com/questions/17258647/why-is-it-safer-to-use-sizeofpointer-in-malloc) – chux - Reinstate Monica Jul 31 '16 at 04:39
0

You must allocate the memory to the string after declaring the pointer. You can do it by using malloc or calloc functions.

char *str = (char*) malloc(sizeof(char) * 100);

or

char *str = (char*) calloc(100, sizeof(char));
kiner_shah
  • 3,939
  • 7
  • 23
  • 37
  • char *str; str=(char *)malloc(sizeof(char)*100); fgets( str, sizeof( str ), stdin ); printf("%s",str);when i run this code only 7 characters are printed even if i take large input.Can you tell me the reason? – RaKo Aug 01 '16 at 04:04
  • I have worked with fgets before and it didn't work well for me then! I will suggest using fscanf instead of fgets. You can write it like this fscanf(stdin, "%s", str); – kiner_shah Aug 01 '16 at 09:23