How to input data into char array?

This is a discussion on How to input data into char array? within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to create a simple unix shell. Right now I'm having problems inputting the command and arguments that ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    9

    How to input data into char array?

    Hi,

    I'm trying to create a simple unix shell. Right now I'm having problems inputting the command and arguments that a user types in. I need to pass them off to execvp to execute the command. This is what the call to execvp looks like:
    int execvp(const char *file, char *const argv[]);

    So, I need to get the command as char* and the arguments as char*[]. This is what I tried to do, but got a segmentation fault:
    char *c;
    cout << "command: ";
    cin >> c;
    cout << "args: ";
    char* args[128];
    cin >> args[0] >> args[1];

    Any idea how to do this properly? Thanks!

    --jumpy

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,691
    Go back and review your notes on pointers. You are trying to get input into pointers that don't point to anything.

    gg

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    9
    Codeplug,

    I looked at the pointer faq on this site, but that didn't help. Can you give me some other resource?

    Are you saying that I need to reserve space first using the new operator?

    Thanks,

    Jumpy

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,691
    You can reserve space on the stack as well:
    Code:
    char input[255];
    ...
    cin >> input;
    gg

  5. #5
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    >Are you saying that I need to reserve space first using the new operator?
    Yes, a pointer has to point to something that you owm before you can safely use it. That means using dynamic memory with new, or by assigning the address of a static of local variable:
    Code:
    char *p = new char[SIZE];
    Code:
    char a[SIZE];
    char *p = a;
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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