• C

segmetnation fault

#include<stdio.h>

int main() {


 char *buffer;
printf("etner a string");
gets(buffer);
 printf("%s", buffer);
 return 0;
}

getting segmentation fault

how to correct this.
nagaharikolaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rysicCommented:
char buffer[50]
0
nagaharikolaAuthor Commented:
I don't want to declare arrays.
i want to declare  a char pointer and run the program
0
evilrixSenior Software Engineer (Avast)Commented:
Well, the pointer has to point to an area of memory to which it can store your input. A pointer by itself is nothing other then a reference to a memory location. You need to assign some memory to which the pointer can point too. Either you can do that on the stack as a static array or on the heap using malloc.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MilleniumaireCommented:
char *buffer only declares a pointer to a character buffer, it does not declare or reserve and space in memory in which a character string can be stored.

The description of gets shows:

      gets()         Reads characters from the standard input stream, stdin,
                     into the array pointed to by s, until a new-line
                     character is read or an end-of-file condition is
                     encountered.  The new-line character is discarded and
                     the string is terminated with a null character.

Notice how it says "into the array pointed to by s".  You aren't declaring an array and so there is nowhere for gets to store the string that it processes.

Why don't you want to declare an array?
0
evilrixSenior Software Engineer (Avast)Commented:
>> char *buffer only declares a pointer to a character buffer
Sorry to be pedantic but actually, it declares a pointer than can point to char. Whether this has 'buffer' (or string) semantics or not is down to how this pointer is interpreted.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.