An access violation (segmentation fault) raised in program

This is a discussion on An access violation (segmentation fault) raised in program within the C Programming forums, part of the General Programming Boards category; Hi guys, I'm on the arrays tutorial and I'm playing around with them. I've wrote the following program to store ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    26

    An access violation (segmentation fault) raised in program

    Hi guys,
    I'm on the arrays tutorial and I'm playing around with them. I've wrote the following program to store the input in an array then retrive the 3rd value and display it but when I compile and run it in Dev C++ I get
    An access violation (segmentation fault) raised in program

    And with gcc in a command line

    Enter a Word rytyrt
    9 [main] array 5728 _cygtls::handle_exceptions: Exception: STATUS_ACCESS_V
    IOLATION
    991 [main] array 5728 open_stackdumpfile: Dumping stack trace to array.exe.s
    tackdump
    45255 [main] array 5728 _cygtls::handle_exceptions: Exception: STATUS_ACCESS_V
    IOLATION
    47921 [main] array 5728 _cygtls::handle_exceptions: Error while dumping state
    (probably corrupted stack)
    Heres the code

    Code:
    #include <stdio.h>
    
    int main()
    
    {
    char array[10];
    
    printf("Enter a Word");
    scanf("%s", array[10]);
    printf("The 3rd letter is %s", array[3]);
    
    getchar();
    getchar();
    return 0;
    }

    Any ideas, thanks

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Your scanf statement tries to store the input into the 10th character in the string. Correct would be (though probably still vulnerable to buffer overflows):
    Code:
    scanf("&#37;s", array);
    Then your printf statement uses a wrong format flag to print a single character, it's %c, not %s.

    And finally, array[3] is not the third character. Indexing starts from zero, so it is the fourth character.

    It is quite possible that your compiler can warn about these errors if you turn compiler warnings on.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    And use fgets() to read strings, not scanf().

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    Cheers guys, it works now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 02:36 AM
  2. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 06:32 AM
  3. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 AM
  4. Access Violation?
    By rwmarsh in forum C++ Programming
    Replies: 6
    Last Post: 05-04-2006, 10:56 AM
  5. Segmentation fault in beginning or program
    By tameeyore in forum C Programming
    Replies: 1
    Last Post: 02-26-2005, 07:16 PM

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