Thread: Seg fault

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I also think your professor may have exaggerated the rule a little bit. Allowing "no numbers" in the source is just silly.

    Obviously, there are many different places where a constant in numeric form is more useful to the reader than a named constant. This should be the goal for all programming: Make the code readable. Typical constants that are easier to read as numbers than named constants are zero, one and minus one.

    And as discussed, naming a constant TWO, or ZERO adds no benefit over writing 2 or 0 in the code - it just makes the reader wonder why the constant TWO is defined as 2 and if it's meaningful or not to change it.

    If you can't meaningfully name your constant, then it's no point in giving it a name at all.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I'm guessing the file is getting opened, but you have a bug in the code. The reason "hello" isn't printed is because the output is buffered, so either add a newline to the printf(), or fflush(stdout).
    Code:
    printf("hello\n");

  3. #18
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Fox101 View Post
    We are not allowed to use hard coded numbers. Our prof wants every number to be defined in the constants file.
    It is silly to have a name the speaks exactly the same as the number

    Defines should make code more readable - not opposite.

    if you want to replace
    return 0;

    do not use
    return ZERO;

    make define like
    #define SUCCESS 0

    and use
    return SUCCESS;

    if your indexes have special meaning - give the names to the indexes that show this meaning like

    #define TYPE 0
    #define CODE 1
    #define VALUE 2

    and in your switch use
    switch(line[TYPE])

    or something like this
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define TWOI 2
    #define CLASS_SIZE 20
    #define NAME_SIZE 20
    #define ONEI 1
    #define SALARY_SIZE 2
    #define TEXT_BUFFER 81
    #define ZEROI 0
    
    struct employee
    {
      char *firstName;
      char *lastName;
      int id;
      char *class;
      double salary[SALARY_SIZE];
      struct employee *nextEmployee;
    };
    
    void hire(FILE **, char [], struct employee **);
    void printList(struct employee *);
    It's difficult to say why it crashes because you haven't shown any code, but the above seems suspect and may be reason due to the crashing. Are you using malloc on firstName, lastName, etc, before trying to read or copy anything into them?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a seg fault
    By ammochck21 in forum C Programming
    Replies: 11
    Last Post: 01-23-2009, 05:27 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM