Thread: very simple C programing , getting errors and please take a look at my code?

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    17

    Exclamation very simple C programing , getting errors and please take a look at my code?

    This is my program and it compile without any problem but when I run it I get segmentation fault great and if there are other problems (which I am sure there are plenty of them) please tell me,,,

    Thanks so much
    Here is the link of code and I also attached it here as well

    https://www.dropbox.com/sh/72501ycafg6u7e7/jv_zCC17aV

    driver.cgrade.cgrade.hstats.cstats.h
    Last edited by AmirHossein H-m; 02-17-2013 at 08:15 PM.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    You create ARRAY as an array of char pointers, but they are all unitialized, pointing who-knows-where. You then use them as destinations for fgets().

    You either need to malloc the space for each string you're attempting to read with fgets, or simply make ARRAY a static 2D array with enough columns to hold your largest input.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    17
    Can you please explain more,I am very new to C

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Whoops. I scanned through code a bit quick, so what I said isn't quite correct, but essentially the same problem.

    You created ARRAY as an array of pointers to strings. As such, the strings are immutable - you cannot change them.

    Given each string is 3 chars longs, create ARRAY as
    Code:
    char ARRAY[ARRAY_SIZE][4] = {"aaa","bbb","ccc","ddd","eee","fff","ggg","hhh","iii","jjj"};
    This way you can modify them.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    17
    How I can pass that array to "void print_array_names" and how to use it for fgets ?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    1. Learn how to indent code -> SourceForge.net: Indentation - cpwiki
    driver.c and grade.c look awful. stats.c looks like it was written by someone else.

    2. Pick meaningful variable names. Just calling something ARRAY doesn't say anything at all, when it's obvious from the rest of the declaration that it is an array.

    So perhaps
    Code:
    char studentNames[ARRAY_SIZE][NAME_SIZE] = {"aaa","bbb","ccc","ddd","eee","fff","ggg","hhh","iii","jjj"};
    int studentGrade[ARRAY_SIZE]= {9,20,30,40,50,60,70,80,90,100};

    Here's how you declare / call a function with a 2D array parameter.
    Code:
    void foo ( char studentNames[ARRAY_SIZE][NAME_SIZE] ) {
        printf("%s\n", studentNames[0] );
    }
    int main ( ) {
        char studentNames[ARRAY_SIZE][NAME_SIZE] = ....
        foo( studentNames );
    }
    Notice how the function parameter declaration is a copy/paste of the variable declaration you want to pass into it later.


    > fgets(array_grade[0],10,stdin);
    Grades are integers, so you need to fgets() into a temporary char buffer, then convert that buffer into an integer (see sscanf or strtol)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Network Game Programing...
    By IndioDoido in forum Networking/Device Communication
    Replies: 4
    Last Post: 11-07-2007, 06:56 AM
  2. Simple Shift errors...
    By Junior89 in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2007, 05:55 AM
  3. Incredibly simple code with an incredible list of errors
    By Twitchmokey in forum C++ Programming
    Replies: 31
    Last Post: 06-27-2006, 11:25 AM
  4. simple graphic programing
    By mattyb in forum Game Programming
    Replies: 10
    Last Post: 12-30-2002, 09:56 PM