Seg- faults

This is a discussion on Seg- faults within the C Programming forums, part of the General Programming Boards category; Hmm.. Something tells me I have to get back to K&R this weekend. Found a similar question in some C ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    hunt2 is not initialized pointer (due to global var - it is null pointer) you cannot dereferense it without allocating space first
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    FYI :
    That was not even my code...
    I did write this one : error: syntax error before 'struct'
    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, 10:29 PM
  2. Seg faults. structs. pointers.
    By ominub in forum C Programming
    Replies: 12
    Last Post: 05-03-2009, 08:04 PM
  3. Help with arrays and seg faults
    By IdioticCreation in forum C++ Programming
    Replies: 7
    Last Post: 04-03-2008, 04:16 PM
  4. sorted linked list...why seg faults!
    By S15_88 in forum C Programming
    Replies: 4
    Last Post: 03-24-2008, 12:30 PM
  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21