-1

I am trying to take this input from terminal.

ARRAY [1,2,3,4,5,6]

and pass the numbers to an array like this.

else if (strncmp(input, "CONSTRUCT", 9) == 0) {
    printf("CONSTRUCT\n");
    // CONSTRUCT [value1,value2,value3,...,valueN]
    int i = 0;
    char *token;
    char *str = strdup(input);
    char **array = str_split(str, '[');
    char **array2 = str_split(array[1], ']');
    char **array3 = str_split(array2[0], ',');
    int array4[100];
    for (i = 0; i < 100; i++){
        array4[i] = atoi(array3[i]);
    }
    for (i = 0; i < 100; i++){
        printf("%d\n", array4[i]);
    }
    for (i = 0; i < 100; i++){
        root = insert(root, array4[i]);
    }
    printf("\n");
}
Nightvision
  • 96
  • 12
  • 1
    You forgot to post your code. – Cheatah May 01 '22 at 16:50
  • It's not the code its the string coming from the user I want to take that numbers into an array – Nightvision May 01 '22 at 16:51
  • 1
    Well... you can't do anything without code :-) – pmg May 01 '22 at 16:52
  • Okay I will post my code – Nightvision May 01 '22 at 16:52
  • SO is not a code writing service. TRy something, when you get stuck post the code you have and explain what problem you are hitting – pm100 May 01 '22 at 16:55
  • I added my code – Nightvision May 01 '22 at 16:58
  • ok, so whats the problem? (Apart from the fact that this code looks for CONSTRUCT but your input has ARRAY). BTW, without seeing a whole program its unlikely SO can help very much (what is 'insert', 'str_split'....) – pm100 May 01 '22 at 17:02
  • That's the part where I need help I just want to take input like "CONSTRUCT [1,2,3]" and have an array like [1,2,3]. I can do the rest – Nightvision May 01 '22 at 17:04
  • 1
    Questions seeking debugging help should generally provide a [mre] of the problem, which includes a function `main` and all `#include` directives. This also allows other people to easily test your program, by simply using copy&paste. – Andreas Wenzel May 01 '22 at 17:14
  • If this question were asked on javascript I would explain how to split that part containing the array and how to iterate over it and put values to a real array of integers but you guys just making things harder. Why do you need to see the whole program? – Nightvision May 01 '22 at 17:14
  • @Nightvision: We are not asking to see "the whole program". We are asking for a [mre]. – Andreas Wenzel May 01 '22 at 17:15
  • I'm pretty sure that part is a "minimal reproducible example". I just tried to save your guys time by just copying that part. – Nightvision May 01 '22 at 17:18
  • Okay, @AndreasWenzel I understand what you mean. You want an easy to test example. – Nightvision May 01 '22 at 17:22
  • is there a space after 'ARRAY' (or 'CONSTRUCT') – pm100 May 01 '22 at 17:28
  • 1
    Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel May 01 '22 at 17:32
  • @pm100 yes there is one space – Nightvision May 01 '22 at 17:35
  • Guys, I solved it. When I take the input with gets(input), instead of scanf("%s",input) it worked! I think the problem is when we take input with scanf it has a new line on its end, I'm not sure but I remember something like that. – Nightvision May 01 '22 at 17:39
  • I suggest that you use [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead of `gets`. See this link for further information: [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/q/1694036/12149471) – Andreas Wenzel May 01 '22 at 17:51
  • @Nightvision: I am pleased that you found the information useful. I am curious, was there anything specific that you found useful? I posted several links on different topics, so I don't know which one you found useful. – Andreas Wenzel May 05 '22 at 23:52
  • The last one, i tried to execute my code on my schools assigment system and it said I can't use gets method because its dangerous. And I remembered your comment and fix it. Thank you! – Nightvision May 06 '22 at 00:05
  • @Nightvision: I assume that your previous comment was intended as a reply to me, but you did not write the comment in such a way that I was notified of your comment. Therefore, I only noticed your reply by coincidence. If you want people to which you are replying to receive a notification of your reply, you must use the `@` syntax with that person's name. Press the `Help` button when writing a comment for further information. If you do not notify the person you are replying to, then that person will likely not notice your comment. – Andreas Wenzel May 06 '22 at 00:44
  • @Nightvision: Only the owner of the post to which the comment is attached (which is you in this case) will automatically be notified of comments to that post. Therefore, you should generally use the `@` syntax when replying to posts, when the comment is attached to your question. – Andreas Wenzel May 06 '22 at 00:45

1 Answers1

2

here you simply run off the end of an array

       char** array3 = str_split(array2[0], ',');
        int array4[100];
        for (i = 0; i < 100; i++)
        {
            array4[i] = atoi(array3[i]);
        }

array3 is dynamically sized to number of numbers + 1, but you try to access 100 entries

you placed a null entry at the end of the list, use that

        int count = 0;
        for (i = 0; i < 100; i++)
        {
            if (array3[i] == NULL)
                break;
            count++;
            array4[i] = atoi(array3[i]);
        }
        for (i = 0; i < count; i++)
        {
            printf("%d\n", array4[i]);
        }
        for (i = 0; i < count; i++)
        {
            root = insert(root, array4[i]);
        }
      

I saw your comment about the space. this code does not work with a space after 'CONSTRUCT', thats because

  scanf("%s", input);

reads up to the first space - you want fgets.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
pm100
  • 48,078
  • 23
  • 82
  • 145