Thread: Accessing elements in a struct

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    Accessing elements in a struct

    Hi all I have a struct defined as the following:
    Code:
    typedef struct command_struct {
          char name[BUFFSIZE];
          char *argv[ARRAYSIZE];
          int argc;
    } command;
    How do I go about accessing the *argv[ARRAYSIZE] data elements if I had a struct called command *cmds[ARRAYSIZE]?

    Code:
    void parse (char cmdline[], command *cmds)
    {
      /* create a pointer to the command line */
      char * cmdline_ptr;
      cmdline_ptr = cmdline;
    
      char * cmd_name;
      cmd_name = cmds->name;
    
      char * cmd_argv;
      cmd_argv = cmds->name->argv;
    /* ????? */
    }
    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    #deinfe <string.h>
    strlen(cmds->argv);

    i think that answers your question

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    92
    Actually misplaced, it doesn't. All I need to know is how to access that *argv[ARRAYSIZE] element in the command struct. I don't know what your code is doing. I want to create a pointer to that data element and that is what I was trying to do in my last set of instructions with:
    Code:
    command *cmds[ARRAYSIZE];
    ...
      char * cmd_argv;
      cmd_argv = cmds->name->argv;
    ...
    But of course this does not access it at all. I just need a alternative to this. Thank you.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    lol....after re-reading the original post i don't know where i got my answer from....

    this is your first problem that i'm sure about:
    cmd_argv = cmds->name->argv;

    should be
    cmd_argv = cmds->argv;

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    i could be wrong.....
    but from there use

    cmd_argv[x][y]
    or
    *cmd_argv[y]

    don't get that stuck in your head until you know it works though

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    92
    Misplaced, could you please help me out with this... obviously I am having some trouble with this but it's been such a long time since I've done C. Anyways, I want to read from the command line of a shell I am making and parse each individual word accordingly into each "word" into the argv array.
    I have two problems: Number one, I am not sure if I am iterating correctly to place each value in each element of the argv. Number two, I am getting a seg fault when I am running the program below. Although of course, if we fix problem 1. number 2 should be no sweat.
    Code:
    ...
    #define BUFFSIZE 128
    #define ARRAYSIZE 4
    ...
    typedef struct command_struct {
          char name[BUFFSIZE];
          char *argv[ARRAYSIZE];
          int argc;
    } command;
    ...
       command *cmds[ARRAYSIZE];
    ...
    Code:
    void parse (char cmdline[], command *cmds)
    {
      /* create a pointer to the command line */
      char * cmdline_ptr;
      cmdline_ptr = cmdline;
    
      char * cmd_name;
      cmd_name = cmds->name;
    
      char * cmd_argv;
      cmd_argv = cmds->argv[ARRAYSIZE];
    
      while (*cmdline_ptr != '\0')
        /* while the cmdline_ptr is not equal to NULL */
        {
          /* write values to cmd->name and cmd->argv */
          while ((*cmdline_ptr != ' ') || (*cmdline_ptr != '\t')){
            *cmd_name++ = *cmdline_ptr;
            *cmd_argv++ = *cmdline_ptr++;
          }
        }
    }

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I don't use command line processing but you can try this:

    #include <iostream>
    using namespace std;

    int main(int argc, char *argv[])
    {
    int i;
    for(i = 0; i < argc; ++i)
    cout << argv[i] << endl;
    return 0;
    }

    compile and execute from the command line. I think each output will be a single word (that is, a single string) as I believe items in the command line are space separated, meaning that each "word" in the command line is interpretted as a separate string and read into argv individually.

    //command line input
    c:\ myProg.exe hello world

    //expected output
    myProg.exe
    hello
    world

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    i think he's right...
    to the best of my recollection....
    *argv[] is an array of string arrays
    argc is the count of arguements

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    92
    I know I know... but I wanted to do it a bit differently... oh well, I gues I am just going to have to go back to argc and argv the regular way. Thanks guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing a struct within a struct
    By Bladactania in forum C Programming
    Replies: 9
    Last Post: 04-21-2009, 08:06 AM
  2. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  3. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM