Quick question

This is a discussion on Quick question within the C Programming forums, part of the General Programming Boards category; Sorry, I'm having a huge brainfart and need some help. I have the following code and I'm trying to read ...

  1. #1

    Quick question

    Sorry, I'm having a huge brainfart and need some help. I have the following code and I'm trying to read a string from the user (maximum of three strings) then execute those commands. Everytime I compile, I get a segmentation fault. Can someone out there help me find my mistake?

    #include <stdio.h>
    #include <string.h>

    int main() {

    int i, j, times = 1;
    char choice;
    char *command[3];
    char *temp;

    for(i=0;times < 3;i++) {

    printf("Please input a valid UNIX command: ");
    /* I get seg. Fault on the following line */
    scanf("%s", command[i]);
    /* gets(*command[i]); */

    printf("You have successfully entered %d command(s) out of a possible /
    %d commands.\n");
    printf("Would you like to enter another command? (y/n): ");

    scanf("%c" ,&choice);

    if(choice == 'Y' || choice =='y') {
    if(choice == 'N' || choice == 'n')
    return 0;

    } /* End loop */

    for(i=0;times <= 3;i--) {

    strcpy(command[i], temp);

    temp[j] = *command[j];

    printf("%% %s\n", temp);

    } /*End for loop*/

  2. #2
    Join Date
    Aug 2001
    This is the easy way to do it!
    #include <stdio.h>
    #include  <string.h>
    #include <stdlib.h>
    int main(int argc, char* argv[])
         int i;
         for (i = 0; i < argc, ++i)
         return 0;
    Your code has a number of bugs. It uses gets and using scanf with %s and no
    field specifier is just as bad. You should use fgets.

    command[3] should be command[3][MAX_CMD_SIZE]

    If I'm correct the best way to do this is to avoid the 2D array with something
    like this

    char command[MAX_CMD_SIZE];
    while(fgets(comand, sizeof command, stdin) != NULL) {
          printf("Enter command EOF to quit: ");

  3. #3
    Registered User Gwargh's Avatar
    Join Date
    Aug 2001
    Brainfart... my seventh grade history teacher used that word...

  4. #4
    Thanks for all the help guys, I ended up doing it with 2D char arrays, but it's done and it works and that's all that matters!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

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