Thread: Filling arrays

  1. #1
    Registered User
    Join Date
    Jan 2022
    Posts
    7

    Question Filling arrays

    I'm taking in an input string, parsing it, and separating the items into arrays. From what I can tell, everything is getting parsed correctly. But, If I iterate of the arrays, I don't get the correct output. Any ideas?

    The code is in HolyC. But, it's just c, with a tad bit of cpp. So, anything you say about incorrect pointer use or anything should be 100% valid, towards this code:

    Code:
    // The green colored lines are printf, without the word printf.
    
    I64 token_pull(U8 *cmd, U8 *out, U8 delim = ' ') {
      I64 i;
    
      "cmd : %s\n", cmd;
      "cmd before %d\n", cmd;
      for (i=0;*cmd != '\0' && *cmd != delim; cmd++) {
        out[i] = cmd[0];
        "%c", cmd[0];
        i++;
      };
      "\n";
      "cmd after  %d\n\n", cmd;
    
      return cmd;
    }
    
    "cc->token_count :%d\n", cc->token_count;
    "cc->flag_count :%d\n", cc->flag_count;
    "cc->size :%d\n", cc->size;
    //Saying U8 is the same as:
    //char tokens[5][128];
    U8 tokens[cc->token_count][cc->size];
    U8 flags[cc->flag_count][cc->size];
    
    "All tokens :%d\n\n", at;
    for (i=0; i < at; i++) {
      token_trim(cmd,,TRUE);   
      if (cmd[0] == '-') {
        "fi : %d\n", fi;
        cmd = token_pull(cmd, &flags[fi]); fi++;
      }
      else {
        "ti : %d\n", ti;
        cmd = token_pull(cmd, &tokens[ti]); ti++;
      }
    }
    
    // List all the tokens.
    for (i=0; i < tc; i++) {
      "tokens[%d] : %s\n", i, tokens[i];
    }
    "\n";
    // List all the flags.
    for (i=0; i < fc; i++) {
      "flags[%d] : %s\n", i, flags[i];
    }
    Ouput I get, when ran:

    cmd: ls -a b c

    cc->token_count : 3
    cc->flag_count : 1
    cc->size : 2
    All tokens : 4

    ti : 0
    cmd : ls -a b c
    cmd before 819438096
    ls
    cmd after 819438098

    fi : 0
    cmd : -a b c
    cmd before 819438098
    -a
    cmd after 819438100

    ti : 1
    cmd : b c
    cmd before 819438100
    b
    cmd after 819438101

    ti : 2
    cmd : c
    cmd before 819438101
    c
    cmd after 819438102

    Arrays:
    tokens[0] : -bc
    tokens[1] : bc
    tokens[2] : c

    flags[0] : -bc

    Should Be:
    tokens[0] : ls
    tokens[1] : b
    tokens[2] : c

    flags[0] : -a
    Last edited by tibegato; 01-10-2022 at 05:12 PM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,331
    If you're using a freak language then you're on your own.
    Choose C or C++.
    Here's a simple C program that does something like you want.
    (If you're trying to make a mini-shell, this is not how to do it.)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    #define MAX_LINE 1024
    #define MAX_TOKS 128
    #define DELIMS   " \t\n"
     
    int main() {
        char line[MAX_LINE];
     
        // Enter EOF signal from keyboard to stop (ctrl-D on linux)
        while (printf("> "), fgets(line, MAX_LINE, stdin) != NULL) {
     
            char *tokens[MAX_TOKS];
            char *flags[MAX_TOKS];
            int t = 0, f = 0;
            for (char *tok = strtok(line, DELIMS); tok; tok = strtok(NULL, DELIMS)) {
                if (tok[0] == '-') {
                    if (f >= MAX_TOKS) {
                        printf("Error: flags overflow\n");
                        exit(EXIT_FAILURE);
                    }
                    flags[f++] = tok;
                }
                else {
                    if (t >= MAX_TOKS) {
                        printf("Error: tokens overflow\n");
                        exit(EXIT_FAILURE);
                    }
                    tokens[t++] = tok;
                }
            }
     
            printf("Total tokens: %d\n", t + f);
            printf("Flags: %d\n", f);
            printf("Non-flag tokens: %d\n", t);
            printf("Tokens:\n");
            for (int i = 0; i < t; ++i)
                printf("  %d: %s\n", i, tokens[i]);
            printf("Flags:\n");
            for (int i = 0; i < f; ++i)
                printf("  %d: %s\n", i, flags[i]);
        }
     
        putchar('\n');
        return 0;
    }
    Sign at the Sichuan Tobacco Project Hope Primary School:
    "Work hard for society. Tobacco can help you become an achiever!"

  3. #3
    Registered User
    Join Date
    Jan 2022
    Posts
    7

    Question

    I am using c syntax.

    Anyhow, I still want to do it without using any libraries. These functions need to be "pure", generic'ish, and easily be modified.

    I know, my first approach has BIG flaws currently. I can't have a flag which also has arguments ... -color "#ff00ff" & right now, you could have a flag first. That one, I'll be fixing soon.

    When I say this out loud, I say it positively, how should I be doing it. I'll gladly take input, from you.

    Note: I can't edit my original post. I can't delete the & from the front of the arrays.

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    817
    Quote Originally Posted by tibegato View Post
    I am using c syntax.
    A statement like this:

    Code:
      "cmd : %s\n", cmd;
    happens to be valid C syntax, but it certainly doesn't do the same thing in C that it does in HolyC. You'll get more help if you stick to the standard syntax and semantics of C.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,331
    You're on your own, moron.
    Sign at the Sichuan Tobacco Project Hope Primary School:
    "Work hard for society. Tobacco can help you become an achiever!"

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    Quote Originally Posted by tibegato
    Anyhow, I still want to do it without using any libraries. These functions need to be "pure", generic'ish, and easily be modified.
    john.c's code stuck strictly to the C standard library. This makes it more generic than the lesser known and specialised HolyC, and easily modified, e.g., I can look up stuff if I don't know and thereby understand and modify the program, whereas for your code you didn't tell us what token_trim does, so I have to guess, and might guess wrong.

    Besides, most of the standard library usage was for I/O, which you can easily translate to HolyC. Presumably there is a core language equivalent of exit in HolyC, so you could use that. The only function that you have to replicate in HolyC using its core language only would be strtok. Oh, and your implementation of token_trim (or if you choose to do so, strtok) could be wrong, whereas an implementation of strtok made available in a C standard library implementation release is more likely to be well tested and peer reviewed.

    Quote Originally Posted by tibegato
    So, anything you say about incorrect pointer use or anything should be 100% valid
    Yeah, but then you wrote:
    Quote Originally Posted by tibegato
    I can't edit my original post. I can't delete the & from the front of the arrays.
    lol

    Anyway, given what you showed of your output:
    Code:
    cc->size : 2
    This means that your arrays to store strings are too small. HolyC seems to use null terminated strings, so your arrays can only store either empty strings or strings of length 1. But in your expected output, two of your strings are of length 2. Hence, your error is right there: you need to increase cc->size sufficiently for the maximum length of the strings that you intend to store.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. filling an array..
    By kromagnon in forum C Programming
    Replies: 3
    Last Post: 11-06-2011, 06:53 PM
  2. help in filling the gaps..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 03-24-2009, 04:49 PM
  3. filling arrays
    By muzihc in forum C Programming
    Replies: 2
    Last Post: 11-06-2008, 12:57 PM
  4. Filling out _iobuf?
    By jesper_o in forum C Programming
    Replies: 3
    Last Post: 05-28-2006, 06:30 AM
  5. Filling up a circle
    By LowLife in forum C Programming
    Replies: 4
    Last Post: 01-21-2006, 04:52 PM

Tags for this Thread