LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 07-26-2004, 05:52 AM   #1
harbir
LQ Newbie
 
Registered: Jan 2004
Posts: 18

Rep: Reputation: 0
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
 
Old 07-26-2004, 06:13 AM   #2
Vookimedlo
Member
 
Registered: Jul 2004
Location: Czech Republic - Roudnice nad Labem
Distribution: Debian
Posts: 253

Rep: Reputation: 34
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
 
Old 07-26-2004, 09:33 AM   #3
harbir
LQ Newbie
 
Registered: Jan 2004
Posts: 18

Original Poster
Rep: Reputation: 0
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
 
Old 07-26-2004, 10:45 AM   #4
harbir
LQ Newbie
 
Registered: Jan 2004
Posts: 18

Original Poster
Rep: Reputation: 0
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;
}
-------------------------------------------------------------------------------------------------------------------------------------------------
 
Old 07-26-2004, 11:22 AM   #5
Vookimedlo
Member
 
Registered: Jul 2004
Location: Czech Republic - Roudnice nad Labem
Distribution: Debian
Posts: 253

Rep: Reputation: 34
You are joking man,

read carefully my first post.

you must have %c in scanf function.
Code:
scanf("%c",&ch);
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.
 
Old 07-26-2004, 03:10 PM   #6
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
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)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation Fault ERROR bluechicken Programming 8 06-30-2005 07:02 AM
Segmentation error - WHAT??????????? the_rydster Linux - Laptop and Netbook 1 11-16-2003 05:40 PM
Segmentation Error WNxSquee Linux - Software 1 08-15-2003 12:48 PM
Segmentation Error MBtronics Linux - Software 4 07-10-2003 08:09 AM
Segmentation Error Roy-Svork Linux - Software 1 01-08-2003 12:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration