Thread: Seg- faults

  1. #1
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342

    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 ..

    Code:
    # 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;
    test1=malloc(sizeof(char)*10);
    printf( " enter some string <10 chars \n");
    scanf("%s",test1);
    printf(" test1 =%s\n",test1);
    printf(" len test1 =%d\n",strlen(test1));
    
    hunt1.name=test1;
    printf("hunt1.name = %s\n",hunt1.name);
    hunt1.len=strlen(test1);
    printf("hunt1.len = %d\n",hunt1.len);
    
    strcpy(hunt2->name,test1);
    //hunt2->name=test1;
    printf("hunt2->name = %s\n",hunt2->name);
    hunt2->len=strlen(test1);
    printf("hunt2->len = %d\n",hunt2->len);
    
     return 0;
    }
    output :
    Code:
     enter some string <10 chars
    asdf
     test1 =asdf
     len test1 =4
    hunt1.name = asdf
    hunt1.len = 4
    Segmentation fault
    In the middle of difficulty, lies opportunity

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    Location
    The edge of the known universe
    Posts
    39,661
    > 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
    Posts
    342
    FYI :
    That was not even my code...
    I did write this one : http://cboard.cprogramming.com/showthread.php?t=90558
    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