Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
07-26-2004, 05:52 AM
|
#1
|
LQ Newbie
Registered: Jan 2004
Posts: 18
Rep:
|
segmentation error
Hello!
I was using the scanf function for inputtig the character.
scanf("%s",&ch); in gcc, program shows no fault while compilation, but gives "Segmentation Error" while executing.
What is this "Segmentation Error" means?
waitting to hear you
Thanx for youe time and patience
Regards
Harbir
|
|
|
07-26-2004, 06:13 AM
|
#2
|
Member
Registered: Jul 2004
Location: Czech Republic - Roudnice nad Labem
Distribution: Debian
Posts: 253
Rep:
|
correct for one input character:
Code:
char ch;
scanf("%c",&ch);
for many characters (strings)
Code:
char ch[255]; //255 length array of chars
scanf("%s",ch);
Segmentation Error comes, when you write something into memory which is not previously allocated by process
|
|
|
07-26-2004, 09:33 AM
|
#3
|
LQ Newbie
Registered: Jan 2004
Posts: 18
Original Poster
Rep:
|
i did the same thing, thanks for letting me know.
if i use
char ch;
scanf("%s",&ch);
then do i need to include string.h
|
|
|
07-26-2004, 10:45 AM
|
#4
|
LQ Newbie
Registered: Jan 2004
Posts: 18
Original Poster
Rep:
|
the following code works fine with Turbo C, but with gcc it give the error "Segmentation Fault" at the run time and do not repeate as it should be with the do-while loop.
--------------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#define si 10
int main()
{
int search(int num,int ke,int x[]);
char ch='y';
int i,j,key;
int a[si]={0};
i=j=key=0;
printf("\nEnter the elements in the array: ");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
do
{
printf("\n________\nEnter the value to be searched: ");
scanf("%d",&key);
j=search(si,key,a);
if(j != -1)
{
printf("\n~~~~~~~\nThe valu is at a[%d] element",j);
}
else
{
printf("\n~~~\n Sorry the value does not exist");
}
printf("\nDo u wish t continue: ");
scanf("%s",&ch);
}while(ch != 'n');
return 0;
}
int search(int num,int ke,int x[])
{
int i=0;
for(i=0;i<num;i++)
{
if(ke==x[i])
{
return i;
}
}
return -1;
}
-------------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
07-26-2004, 11:22 AM
|
#5
|
Member
Registered: Jul 2004
Location: Czech Republic - Roudnice nad Labem
Distribution: Debian
Posts: 253
Rep:
|
You are joking man,
read carefully my first post.
you must have %c in scanf function.
BTW:
If you try it in DOS (TURBO C compiler), it will work. But you will rewrite foreign memory. So if you will use system longer, it will crash one-day. But Linux recognize foreign memory and do fault and thus protect others processes.
|
|
|
07-26-2004, 03:10 PM
|
#6
|
Senior Member
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038
Rep:
|
segmentation errors are errors you get when running the program, all it means is that the program tried to write to memory it does not own (didn't allocate, or something of that sort)
|
|
|
All times are GMT -5. The time now is 03:15 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|