Q:

C- Save string to array and print a char

Im trying to save a 100 character max string into an array and then print an specified character of the array via an index, yet I get Segmentation error 11, here is the code:`
#include<stdio.h>
#include<stdlib.h>
int main()
{
   char str1[100];
   int index;
   printf("Enter text of max 100 characters: \n");
   scanf("%s", str1);

   printf("Enter the index to search\n");
   scanf("%d", &index);

   printf("your char is: %c\n", str1[index]);

   return(0);
}

`
Any suggestions?

A:

While user input is generally better handled by reading input with fgets and then parsing what you need from the resulting buffer with sscanf of simply with a pair of pointers and "inch-worming down" (a/k/a "walking") the string testing each char and handling as needed -- it is always worth a look at scanf to detail what you need to do to successfully use it for user input.
While fgets is not without needed validations, the number and types of validations needed with scanf and handling of characters that remain in the input buffer in the different cases of input failure or matching failures create a number of extra pitfalls for new (and not so new) C programmers.
The two primary problems with scanf are (1) there is no default limitation on the number of characters that it will read into any buffer (potentially overflowing your array); and (2) the fact that it does not remove the trailing '\n' (or any of the characters following an input or matching failure) from the input buffer (e.g. stdin). It is up to you to account for all characters in the input buffer and empty the buffer as needed.
Further complicating the picture are the ways the different scanf format specifiers handle leading-whitespace (numeric conversions typically skip leading whitespace, while a character conversion won't) Another issue is handling included whitespace. The "%s" format specifier will only read up to the first whitespace encountered, making it impossible to read "My dog has fleas" with a single %s specifier. (you can use a character class to read included whitespace -- as shown in the example below)
There are many other subtleties with scanf as well, so it is well worth the time it takes to read and understand man scanf.
From the comments, you now know if you ask for a string of 100 chars, you need, at minimum, 101 characters of storage -- we will assume that is learned.
When taking any input with scanf, you must always validate the return to insure that the number of conversion expected, in fact took place. For example, if you are reading "5 dogs" with the conversion specifier "%d %s", a return of 2 indicates a successful conversion to integer and string. However, you also know, at minimum a '\n' remains in the input buffer (and potentially many more characters, if say, "5 dogs and cats" were entered. It is up to you to remove the '\n' and any other characters that remain, before attempting to read more input with scanf.
The following example captures most of the pitfalls with your example and provides a couple of tools you can use when dealing with user input. The bottom line is learn to use fgets, but know how to use scanf as well. Your goal is to provide as robust and bullet-proof input routine as you can. Think about all the dumb things a user might do when prompted for input (or heaven forbid, a cat walks across the keyboard) There are always more validations you can add. Look at each of the included validations, and let me know if you have questions:
#include <stdio.h>
#include <string.h>

#define MAXC 100    /* if you need a constant, declare one  */

/* helper function to remove any chars left in input buffer */
void empty_stdin()
{
    int c = getchar();
    while (c != '\n' && c != EOF)
        c = getchar();
}

int main (void) {

    char str1[MAXC+1] = ""; /* initialize to all zero */
    int index, rtn, len;    /* index, return, length  */

    for (;;) {  /* loop until valid input obtained */
        printf ("Enter text of max 100 characters: ");
        rtn = scanf ("%100[^\n]", str1);    /* read at most MAXC char */

        if (rtn != 1) {         /* validate scanf return */
            if (rtn == EOF) {   /* check if EOF, ctrl+d, ctrl+z (windoze) */
                printf ("input canceled.\n");
                return 0;
            }
            if (!str1[0])       /* was a character entered? */
                fprintf (stderr, "error: string is empty.\n");
            /* remove '\n' and any chars that remain in stdin */
            empty_stdin();
        }
        else {  /* all conditions met, good entry, empty stdin and break */
            empty_stdin();
            break;
        }
    }

    len = (int)strlen (str1);   /* get string length */

    for (;;) {  /* now do the same thing for integer */
        printf ("Enter the index to search (0-%d): ", len - 1);
        if ((rtn = scanf ("%d", &index)) != 1) {
            if (rtn == EOF) {
                printf ("input canceled.\n");
                return 0;
            }
            fprintf (stderr, "error: invalid input - not integer.\n");
            /* only need to strip if non-integer entered, because %d
             * will skip leading whitespace, including '\n'.
             */
            empty_stdin();
        }
        else if (index < 0 || len < index + 1)  /* validate index value */
            fprintf (stderr, "error: invalid index - out of range.\n");
        else
            break;
    }

    printf ("your char is: %c\n", str1[index]);

    return 0;
}

Example Use/Output
$ ./bin/scanfstr1
Enter text of max 100 characters: 12345678901234567890
Enter the index to search (0-19): -1
error: invalid index - out of range.
Enter the index to search (0-19): 0
your char is: 1

$ ./bin/scanfstr1
Enter text of max 100 characters: 12345678901234567890
Enter the index to search (0-19): foo
error: invalid input - not integer.
Enter the index to search (0-19): 6
your char is: 7

$ ./bin/scanfstr1
Enter text of max 100 characters: My dog has fleas.
Enter the index to search (0-16): d
error: invalid input - not integer.
Enter the index to search (0-16): 3
your char is: d

$ ./bin/scanfstr1
Enter text of max 100 characters:
error: string is empty.
Enter text of max 100 characters: My cats are fine.
Enter the index to search (0-16): meow
error: invalid input - not integer.
Enter the index to search (0-16): input canceled.

