Thread: Seg- faults

  1. #1
    Eager young mind
    Join Date
    Jun 2006

    Seg- faults

    Hmm.. Something tells me I have to get back to K&R this weekend. Found a similar question in some C aptitude test , Not able to explain why it segfaults ..

    # include<stdio.h>
    # include<string.h>
    # include<stdlib.h>
    struct ping{
     char *name;
     int len;
    struct ping hunt1;
    struct ping *hunt2;
    int main()
     char *test1;
    printf( " enter some string <10 chars \n");
    printf(" test1 =%s\n",test1);
    printf(" len test1 =%d\n",strlen(test1));;
    printf(" = %s\n",;
    printf("hunt1.len = %d\n",hunt1.len);
    printf("hunt2->name = %s\n",hunt2->name);
    printf("hunt2->len = %d\n",hunt2->len);
     return 0;
    output :
     enter some string <10 chars
     test1 =asdf
     len test1 =4 = asdf
    hunt1.len = 4
    Segmentation fault
    In the middle of difficulty, lies opportunity

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    hunt2 is not initialized pointer (due to global var - it is null pointer) you cannot dereferense it without allocating space first
    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

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > Join Date: Jun 2006
    > Posts: 279
    Still working on the idea of indentation then I see
    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.

  4. #4
    Eager young mind
    Join Date
    Jun 2006
    FYI :
    That was not even my code...
    I did write this one :
    In the middle of difficulty, lies opportunity

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-25-2009, 09:29 PM
  2. Seg faults. structs. pointers.
    By ominub in forum C Programming
    Replies: 12
    Last Post: 05-03-2009, 07:04 PM
  3. Help with arrays and seg faults
    By IdioticCreation in forum C++ Programming
    Replies: 7
    Last Post: 04-03-2008, 03:16 PM
  4. sorted linked list...why seg faults!
    By S15_88 in forum C Programming
    Replies: 4
    Last Post: 03-24-2008, 11:30 AM
  5. prog runs on 64bit - seg faults on 32bit
    By hollie in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 01:59 AM