0
#include <stdio.h>

void main()
{
    char *p;
    gets(p);
    puts(p);
}

When I run the code this happens enter image description here

Jongware
  • 22,200
  • 8
  • 54
  • 100
  • 1
    You need to pass in a pointer that points to a memory buffer. As it is, `p` is unintialised. – kaylum Nov 15 '15 at 19:25
  • C isn't well suited to trial and error learning... learn from a book. [Here are some recommendations](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – M.M Nov 16 '15 at 00:03

3 Answers3

2

You have to declare how much memory is needed for that pointer. for example when you are typing this : *p="Hello world" it is automatically declaring that how much memory is used for this. So if u still want to use pointer for this program then you have to use malloc function:

#include<stdio.h>

int main()
{
    char* p;
    p=malloc(100);

    printf("Enter some text:");
    gets(p);
    printf("\nYou have typed: %s\n",p);

    return 0;
}

Here malloc(100); means u need 100byte memory to store that string.

princebillyGK
  • 2,917
  • 1
  • 26
  • 20
1

It appears that you are learning from the wrong source, in the sense that the code you posted was taught a long time ago, and is no longer. Because it has a lot of problems and it uses a non standard form of the main() function which was used before .

Also you are trying to write to a pointer that does not point anywhere.

A good version of your program would be

int main(void)
{

    char p[1024];
    if (fgets(p, sizeof(p), stdin) == NULL)
        return -1;
    puts(p);
    return 0;
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • Not even sure if it's worth pointing out, but the past tense of teach is taught, not teached ;) – user4520 Nov 15 '15 at 20:17
  • It is, you know! Spanish is my mother tongue. – Iharob Al Asimi Nov 15 '15 at 20:18
  • Yeah, your profile states that. What I meant is: it doesn't make your answer any less useful, but at the same time, maybe I should let you know so you (and others who read this in the future) don't make that mistake again. This is a discussion for Meta SO though... – user4520 Nov 15 '15 at 20:19
-3

A pointer *p provides no storage. You would need to provide something that actually has memory. For example:

#include <stdio.h>

void main()
{
    char p[1024];
    gets(p);
    puts(p);
}

Note however that if you input more than 1024 characters, this will again have issues.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
wojtow
  • 864
  • 5
  • 11
  • 3
    While your suggestion is almost correct (1024 characters will cause UB, 1023 will not), please provide a more elaborate answer involving the `fgets` function, not the bogus function `gets`. It was finally removed from the last version of the C Standard for a good reason! – chqrlie Nov 15 '15 at 19:31
  • Also, `void main()` is wrong, it's found in old books pre standard c AFAIK. – Iharob Al Asimi Nov 15 '15 at 19:40
  • 1
    Sorry, but downvoting you for suggesting to use `gets` is pretty much obligatory. – user4520 Nov 15 '15 at 20:03
  • @iharob well it causes implementation-defined behaviour ... as do a lot of things that are not considered "wrong" – M.M Nov 16 '15 at 00:04
  • Admittedly, the use of gets() is undesirable and I have been duly chastised. However, I was just answering in the limited scope of the question in that the title specifically asked about gets() and the problem wasn't related to the use of gets(). Correcting the use of main() for example was outside the scope of the question entirely. But I will know better next time to provide a more correct example than making the minimal change to the OPs code. – wojtow Nov 23 '15 at 18:58