473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

segmentation fault error!

Hi every one,

I write a simple code in c,but I get segmentation fault error while running! I know it this error occurs while we want to get wrong access to a special part of memory but actually I can't find it in my code:( I attached my code in attachments ...

would you please help me?
special tanks for your help...
Expand|Select|Wrap|Line Numbers
  1. /*
  2.  ============================================================================
  3.  Name        : tamrin2.c
  4.  Author      :
  5.  Version     :
  6.  Copyright   : Your copyright notice
  7.  Description : The information of students in a university
  8.  ============================================================================
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14.  
  15.  
  16. int main() {
  17.  
  18.     int number;
  19.     printf("Enter number of students :");
  20.     scanf("%d",&number);
  21.  
  22.     typedef struct Student{
  23.         char *Fname;
  24.         char *Lname;
  25.         char *name;
  26.         float ave;
  27.         int yearOfBirth;
  28.         char *STD;
  29.     }St;
  30.  
  31.     St st[number];
  32.  
  33.     int i;
  34.     for(i=0;i<number;i++){
  35.  
  36.  
  37.         printf("Enter %d student's first name :\n",i+1);
  38.         scanf("%s",st[i].Fname);
  39.         st[i].Fname=(char*)malloc(sizeof(char)*(strlen(st[i].Fname)+1));
  40.  
  41.  
  42.         printf("Enter %d student's last name :\n",i+1);
  43.         scanf("%s",st[i].Lname);
  44.  
  45.         st[i].Lname=(char*)malloc(sizeof(char)*(strlen(st[i].Lname)+1));
  46.  
  47.  
  48.         strcpy(st[i].name,strcat(st[i].Fname,st[i].Lname));
  49.         st[i].name=(char*)malloc(strlen(st[i].Fname)+strlen(st[i].Lname)+3);
  50.  
  51.         printf("Enter %d student's average : \n",i+1);
  52.         scanf("%f",&st[i].ave);
  53.  
  54.         printf("Enter %d student's year of birth : \n",i+1);
  55.         scanf("%d",&st[i].yearOfBirth);
  56.  
  57.         printf("Enter %d student's STD number : \n",i+1);
  58.         scanf("%s",st[i].STD);
  59.         st[i].STD=(char*)malloc(sizeof(char)*(strlen(st[i].STD)+1));
  60.  
  61.     }
  62.  
  63.     for(i=0;i<number;i++){
  64.         free(st[i].STD);
  65.         free(st[i].name);
  66.         free(st[i].Lname);
  67.         free(st[i].Fname);
  68.     }
  69.     return 0;
  70. }
Dec 31 '11 #1
1 1704
weaknessforcats
9,208 Expert Mod 8TB
In yur struct Fname is a char* and you are using it in scanf. You need memory allocated before you call scanf.

The usual practice is to use a buffer of a known size in the scanf. Then you can strlen the buffer and use that to allocate for your Fname.
Dec 31 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Polar | last post by:
Hi! i'm a newbie in C language and i'm writing my first simple codes. In one of these, my purpose is to append the ascii value of an interger (example 101 --> e) at the end of a string to...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
1
by: utab | last post by:
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <iterator> using std::cout; using std::vector; using std::string; using std::endl;
3
by: Dhieraj | last post by:
While compiling a C++ code I am getting the following error : CC -c -I/opt/iona/artix/2.0/include -I/opt/iona/asp/6.0/include -I/opt/ar/api63/include -I//var/tmp/vidya/aotscommon/include ...
14
by: cnixuser | last post by:
I am new to C programming and am still at an early level in java and C#. I am posting regarding a segmentation fault error code I get when I try to run a program that I am developing. I am coding on...
1
by: Alvin | last post by:
Note: Source code and compile commands are at the bottom. I've spent some time with a friend making a nice and simple to use OOPified C++ networking layer for TCP that works on multiple...
4
by: william | last post by:
I received a segfault error in a very short program for test purpose. I found no luck even if I read some tricks about tracking down the segfault. Can any one give me a hint which line of the...
1
by: Sharad Maloo | last post by:
Hi, I am running the image file of my project, it is giving following segmentation fault: ERROR----- > Program received signal SIGSEGV, Segmentation fault. 0x004240cb in strlen () from...
20
by: enjoyfate | last post by:
I am newbie ,in linux/gnu envirement,wrot test file below to see the first argument. #include "stdio.h" void main (int argc, char *argv) { printf ("%s", argv); } After compiled it, $ ./test...
1
by: jcancino | last post by:
The program compiles and works all the way until the multiplecheck function, any help would be great. The program is not finished yet and I am new to C programming so I know the program may not be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.