1

Possible Duplicate:
Segmentation Fault - C

#include<stdio.h>
#include<string.h>
int main()
{
  char *p;

  printf("enter some thing:");
  gets(p);

  printf("you have typed:%s\n",p);
}

Why doesn't this program work? i can't use pointer as a string.

Output is:

enter some thing:raihan
Segmentation fault (core dumped)

I get this error every time when I use a char pointer. How can I solve this problem? I am using code-blocks on Linux mint13 KDE.

Community
  • 1
  • 1
MH Raihan
  • 31
  • 6
  • 1
    I don't agree with the downvote on this question. The OP is obviously very new to C and just needs some help understanding the basics. Good on them for giving it a crack. It was an easy question to answer, and they will learn a lot from the answers given here. – paddy Sep 15 '12 at 10:11
  • @paddy - I concur - get the user the benefit of the doubt. +1 to him or her just to make this forum a pleasant place. – Ed Heal Sep 15 '12 at 10:22
  • @EdHeal you have obviously not read the "Why pity upvotes are not good" post on Meta... –  Sep 15 '12 at 10:23
  • @H2CO3 He may well have read that and simply disagrees with it. – David Heffernan Sep 15 '12 at 10:28
  • @DavidHeffernan yes, actually that's possible also. But IMHO that might be an even bigger problem. –  Sep 15 '12 at 10:28
  • @H2CO3 - The person is a new to this forum. Hence just give that person the benefit of the doubt and to make that person welcome. It is not a stupid question hence the up vote. – Ed Heal Sep 15 '12 at 10:33
  • @H2CO3 I basically agree with Ed. Although it's a dupe, we only know that because we can see the error in the code. If you can't see the error, how would you be able to find that it was a dupe. So it's fine to vote to close as dupe because that actually helps user (she/he can go and read the dupe and learn from there too). But downvoting the question isn't helpful. It was a well asked question. A fully reproducible program, with the exact error message. Compiler and OS included too. +1 from me too. – David Heffernan Sep 15 '12 at 10:36
  • @DavidHeffernan - Thank you and I guess I can thank you on behalf of user1673184. When I was new to this forum I made some mistakes (i guess we all did). Just a little tolerance and understanding goes a long way. We all know the questions that are reasonable and genuine compared to the wind up ones. – Ed Heal Sep 15 '12 at 10:45
  • @DavidHeffernan "If you can't see the error, how would you be able to find that it was a dupe" - the thing is I found the dupe I mentioned on the first page of Google. You don't have to know the reason, you can just type `c gets segfault`. Sorry, but I can't really support not even making the effort to solve problems himself. This is not about tolerance. See, when I was a beginner in C, I didn't have to ask such questions here. I clearly remember making the `int *i; scanf("%d", i);` mistake, and I also remember finding the answer on the Internet in less than 5 minutes without asking anybody. –  Sep 15 '12 at 11:29
  • @H2CO3 You're entitled to that viewpoint. – David Heffernan Sep 15 '12 at 11:51
  • @H2CO3 - Why not just give the person a bit of slack - the person is new to the forum and it is not how to welcome a new guest? It was a honest and sincere question that deserves a little respect. Just my option and I guess that yours is a little more unforgiving. – Ed Heal Sep 15 '12 at 12:19
  • @DavidHeffernan Yes, I actually am, quite a bit. –  Sep 15 '12 at 12:25
  • @EdHeal You're right - I might be unforgiving here, but let that be my own decision. (I rather respect your attitude because you could argue in a civilized way.) –  Sep 15 '12 at 12:28
  • @H2CO3 - Sometimes on a cold day it is nice to let a person thru the front door and give them a drink. Besides I think that you (H2CO3) do argue in a civilize way but knock a couple of chunks off the corners to make it less harsh. – Ed Heal Sep 15 '12 at 12:38
  • 1
    @H2CO3 This discussion has been civilized hasn't it? Or are you suggesting that I have not been? Or am I reading too much into that comment. – David Heffernan Sep 15 '12 at 12:42
  • 1
    @DavidHeffernan no, I was not suggesting that you weren't civilized, I didn't have the intention to do so. Sorry if it turned out to be. My entire point is just that I generally disagree with the "let's forgive this mistake to beginners" principle. –  Sep 15 '12 at 12:45
  • 1
    @H2CO3 - Why not give a beginner a bit of slack? We was all beginners and need a little boost in both gaining knowledge and confidence. So what is the harm in that a person enters this forum, asks a perfect;y reasonably question, trying to get on the bottom rung and to learn the ropes? Why not give that person a little break? – Ed Heal Sep 15 '12 at 13:24

6 Answers6

3

You have not allocated memory. You just declared a pointer, p, but didn't make it point at anything. That explains the segmentation fault. You will need to allocate memory for your buffer.

What's more, gets does not allow you to specify how big the buffer is. So you are at risk of running over the end of the buffer. So use fgets instead.

int main(void)
{
    char buffer[1024];//allocates a buffer to receive the input
    printf("enter some thing: ");
    fgets(buffer, sizeof(buffer), stdin);
    printf("you have typed: %s\n", buffer);
    return 0;
}

I also corrected your declaration of main and made sure that it returns a value.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
2

You haven't allocated any memory for p. Also, use fgets instead of gets which may overflow the input buffer.

P.P
  • 117,907
  • 20
  • 175
  • 238
2
char *p;
printf("enter some thing:");
gets(p);

Wrong. Gets() tries to fill in the array pointed to by the supplied pointer - and it segfaults, because that pointer hasn't been initialized, so it might (and does) point to some garbage/invalid memory location. Use

char p[256];

or something like this instead - you still have to worry about a buffer overflow in if the user enters a string longer than 255 characters. You can solve that one using

fgets(p, sizeof(p), stdin);
2

Your pointer is declared but you have not initialised it and so its value will be some arbitrary memory location that you may not have access to write to. Thus anytime you read or write to this you run the risk of segfault. Allocate some heap memory for the pointer using a call to malloc then you wont get segfaults when writing to it.

mathematician1975
  • 21,161
  • 6
  • 59
  • 101
1

You have just defined a pointer - no memory for the characters have been allocated!

Use either an array or malloc.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
1

A pointer is just a memory address. It says "you have some data here". But it doesn't actually reserve that data.

In your case the problem was two-fold. The pointer didn't point to valid memory and you never even set it to anything (so it pointed to somewhere random).

You can fix this in different ways. The easiest is to just use an array (it's sort of implicitly a pointer):

char something[100];
printf("enter some thing:");
gets(something);

That gives you 100 chars on the stack. You can also point to it if you want, but in this case it's a bit redundant:

char *p = something;

The other way is dynamic allocation, where you ask the operating system at runtime to give you some number of bytes. This way you have to give it back when you're finished using it.

char *something = (char*)malloc( 100 * sizeof(char) );  // Ask for 100 chars
printf("enter some thing:");
gets(something);
free(something);  // Do this when you don't need that memory anymore.

PS: Remember when you have strings, you always need one extra byte than the number of characters you intend to store. That byte is for the string terminator, and the value of it is 0.

paddy
  • 60,864
  • 6
  • 61
  • 103