0

Where am I going wrong and why?

#include<stdio.h>
#include<string.h>

int main()
{
char *str;
int length, i, j, flag = 0;

printf("\n\nEnter string: ");
fgets(str, 20, stdin);
printf("You entered: %s", str);
    return 0;
}

There's a problem with the line fgets(str, 20, stdin); line. I cannot figure it out. After entering the string, the compiler just stops working and I get an error saying: This program has stopped working. Could you point where am I going wrong and also a workaround for this problem? The standard library defines fgets as:

char * fgets ( char * str, int num, FILE * stream );

I'm using Sublime Text 2 and GCC on the MinGW shell.

A different question based on the pointer concept: Are there any differences between char * str, char* str and char *str?

ash9209
  • 1,018
  • 2
  • 9
  • 16

3 Answers3

4

You need to allocate memory for str

Any size say 128.

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

or

char str[128];

P0W
  • 46,614
  • 9
  • 72
  • 119
  • One more question: Why don't we have to typecast **(char*)** before **malloc** here? – ash9209 Aug 12 '13 at 18:30
  • @ash9209 `malloc` return type is `void *`. It is implicitly converted to another pointer type, so its unnecessary. – P0W Aug 12 '13 at 18:44
1

You haven't allocated any memory for str. Replace char *str with char *str = malloc(20); for heap allocation or char str[20] for stack allocation.

It doesn't matter where you place the * - all three statements are identical.

P.S. Sublime Text 2 is just a text editor, and which text editor you use has no effect on the program. The compiler and OS do matter, though.

1''
  • 26,823
  • 32
  • 143
  • 200
  • It's showing a warning when I add the malloc function: **In function 'main': warning: incompatible implicit declaration of built-in function ' malloc' [enabled by default]** – ash9209 Aug 12 '13 at 18:23
  • Add the line `#include ` at the top of your program. – 1'' Aug 12 '13 at 18:24
  • Yeah I know it isn't necessary to mention the text editor. But being on stackoverflow for quite a while, and browsing through many questions, some idiots do ask about the editor too. I don't know what they get out of it, though. – ash9209 Aug 12 '13 at 18:26
1

So answering my own question, the code which I posted throws up an error because I am not allocating memory for the variable named str. I am just giving it an arbitrary memory location.

So for the above code to work correctly, we can either allocate memory on the stack or heap.

For allocating on the stack, use char str[20] and for allocating memory on the heap, use char *str = malloc(20) or char *str = malloc(20*sizeof(char)).

Do not worry too much about the details if you don't know about stack or heap. Any of them will do for simple console programs.

As to my other question, the position of * in char* str, char * str and char *str does not matter. All 3 of them are the same.

Another question that I asked in association with @P0W's answer was why we aren't casting the memory address that is returned by malloc in char *str = malloc(20*sizeof(char)). The answer to this question is that it's not necessary and should be avoided in C. Include the stdlib.h header file to get a workaround .

Check this StackOverflow question for more details. Or you can also view the C FAQ section.

Community
  • 1
  • 1
ash9209
  • 1,018
  • 2
  • 9
  • 16
  • One can also use _global_ memory as in `static char str[20]`. Typically this is rightfully frowned upon, but it is viable option in select cases. – chux - Reinstate Monica Aug 12 '13 at 19:37
  • @ash9209, casting the returned value of `malloc` helps the next programmer who reads your code. When`stdlib.h` is included there is no technical downside to explicitly casting `malloc`. – JackCColeman Aug 12 '13 at 20:33