Static Struct

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lpaulson87
    New Member
    • Jun 2010
    • 4

    #1

    Static Struct

    Would any one be able to explain this chunk of code to me?


    *************** *************** *************** **************
    static struct option loptions[] = {
    {"help", no_argument, 0, 'h'},
    {"port", required_argume nt, 0, 'p'},
    {"baud", required_argume nt, 0, 'b'},
    {"send", required_argume nt, 0, 's'},
    {"receive", no_argument, 0, 'r'},
    {"num", required_argume nt, 0, 'n'},
    {"delay", required_argume nt, 0, 'd'}
    };

    *************** *************** *************** **************
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    It creates a variable of type array of option that is limited in scope to the current file (assuming this code appeared at the head of a file not inside a function) and initialised it with 7 entries.

    Comment

    • lpaulson87
      New Member
      • Jun 2010
      • 4

      #3
      Originally posted by Banfa
      It creates a variable of type array of option that is limited in scope to the current file (assuming this code appeared at the head of a file not inside a function) and initialised it with 7 entries.
      No it dose appear within a functions here is the function

      Code:
      int main(int argc, char *argv[]) 
      {
          int fd = 0;
          char serialport[256];
          int baudrate = B9600;  // default
          char buf[256];
          int rc,n;
      
          if (argc==1) {
              usage();
              exit(EXIT_SUCCESS);
          }
      
          /* parse options */
          int option_index = 0, opt;
          static struct option loptions[] = {
              {"help",       no_argument,       0, 'h'},
              {"port",       required_argument, 0, 'p'},
              {"baud",       required_argument, 0, 'b'},
              {"send",       required_argument, 0, 's'},
              {"receive",    no_argument,       0, 'r'},
              {"num",        required_argument, 0, 'n'},
              {"delay",      required_argument, 0, 'd'}
          };
          
          while(1) {
              opt = getopt_long (argc, argv, "hp:b:s:rn:d:",
                                 loptions, &option_index);
              if (opt==-1) break;
              switch (opt) {
              case '0': break;
              case 'd':
                  n = strtol(optarg,NULL,10);
                  usleep(n * 1000 ); // sleep milliseconds
                  break;
              case 'h':
                  usage();
                  break;
              case 'b':
                  baudrate = strtol(optarg,NULL,10);
                  break;
              case 'p':
                  strcpy(serialport,optarg);
                  fd = serialport_init(optarg, baudrate);
                  if(fd==-1) return -1;
                  break;
              case 'n':
                  n = strtol(optarg, NULL, 10); // convert string to number
                  rc = serialport_writebyte(fd, (uint8_t)n);
                  if(rc==-1) return -1;
                  break;
              case 's':
                  strcpy(buf,optarg);
                  rc = serialport_write(fd, buf);
                  if(rc==-1) return -1;
                  break;
              case 'r':
                  serialport_read_until(fd, buf, '\n');
                  printf("read: %s\n",buf);
                  break;
              }
          }
      
          exit(EXIT_SUCCESS);    
      } // end main

      Thanks for any and all help

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        using static inside main is nearly pointless. static used on data inside a function means that the data exists for the lifetime of the program rather than the lifetime of the function. That means that the value is saved from one execution of the function to the next.

        This is fairly pointless in main because main is running for the lifetime of the program anyway so the static has little effect. Main is not a function that is entered and exited multiple times during the lifetime of the program.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          You should have gotten compiler errors. In C, variable definitions cannot follow executable statements.

          You must move the if(argc==1) clause to a point after the definition of the loptions array.

          Another approach would be to put an open-brace before the option_index, opt, and loptions definitions and a close brace after the last executable statement that uses any of those variables.

          One reason to use static within main is to declare a static const variable. The Standard does not require it, but many compilers will respond to the phrase 'static const' by allocating the variable in read-only memory. If your compiler does this, then any attempt to write to the variable will provoke a segment violation runtime error. You can't cast away this kind of const-ness.

          Comment

          • lpaulson87
            New Member
            • Jun 2010
            • 4

            #6
            Hmm, weird Because it compiles and it works. I'm using to to communicate wiht my Arduino microcontroller . I'm trying to understand the code so I improve on it.

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              Are you using C or C++?

              If C, are you using C99? Perhaps this newest version of C relaxes this variable-definition-positioning constraint.

              Have you activated nonstandard extensions in your compiler?

              Comment

              • lpaulson87
                New Member
                • Jun 2010
                • 4

                #8
                I'm using C, I'm not sure if I'm using C99 I'm using what ever packages came standard on ubuntu and I'm using the internal compiler "gcc"

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  gcc supports a lot of nonstandard extensions. You can order it to be fully Standards-compliant by asserting the -pedantic switch. There are also switches to tell it to be compliant with a specific version of the Standard (C89, C99).

                  Comment

                  Working...