How read two lines numbers from input to 2 arrays, C language? Without knowing the amount of number? in C












0














For example, input is:



12 23 32 41 45
22 11 43


lines end with 'n',



I want save nums to a and b;



a = {12, 23, 32, 41, 45}
b = {22, 11, 43}


The point is I DON'T KNOW how many number of each line.



If I know line1 n numbers and line2 m numbers, I will use "for loop", and no question.



Just like,



for(int i = 0; i < n; i++) scanf("%d", a+i);
for(int i = 0; i < m; i++) scanf("%d", b+i);


But, I DO NOT know n and m, How to do, guys?










share|improve this question
























  • Can you accept a number max for array length ?
    – Ôrel
    Nov 19 '18 at 17:03






  • 1




    If you care about line endings, then you either need to read one character at a time with fgetc, or read a line at a time with fgets.
    – user3386109
    Nov 19 '18 at 17:06










  • Read characters until a new line is reached, if one isn't reached, resize your buffer? You'll want to use dynamic memory allocation probably.
    – jacob
    Nov 19 '18 at 17:07






  • 2




    A good function for reading a line at a time is getline, which was standardized in POSIX.1-2008. In comparision to fgets, it can read a line of arbitrary length into a dynamically allocated buffer. If you don't have the getline function, there are various standalone implementations of it, such as the one at github.com/ivanrad/getline .
    – Ian Abbott
    Nov 19 '18 at 17:19










  • You could use a while loop and some arbitrary input as break condition & dynamically alloc your input array(i think).
    – ats
    Nov 19 '18 at 17:25


















0














For example, input is:



12 23 32 41 45
22 11 43


lines end with 'n',



I want save nums to a and b;



a = {12, 23, 32, 41, 45}
b = {22, 11, 43}


The point is I DON'T KNOW how many number of each line.



If I know line1 n numbers and line2 m numbers, I will use "for loop", and no question.



Just like,



for(int i = 0; i < n; i++) scanf("%d", a+i);
for(int i = 0; i < m; i++) scanf("%d", b+i);


But, I DO NOT know n and m, How to do, guys?










share|improve this question
























  • Can you accept a number max for array length ?
    – Ôrel
    Nov 19 '18 at 17:03






  • 1




    If you care about line endings, then you either need to read one character at a time with fgetc, or read a line at a time with fgets.
    – user3386109
    Nov 19 '18 at 17:06










  • Read characters until a new line is reached, if one isn't reached, resize your buffer? You'll want to use dynamic memory allocation probably.
    – jacob
    Nov 19 '18 at 17:07






  • 2




    A good function for reading a line at a time is getline, which was standardized in POSIX.1-2008. In comparision to fgets, it can read a line of arbitrary length into a dynamically allocated buffer. If you don't have the getline function, there are various standalone implementations of it, such as the one at github.com/ivanrad/getline .
    – Ian Abbott
    Nov 19 '18 at 17:19










  • You could use a while loop and some arbitrary input as break condition & dynamically alloc your input array(i think).
    – ats
    Nov 19 '18 at 17:25
















0












0








0







For example, input is:



12 23 32 41 45
22 11 43


lines end with 'n',



I want save nums to a and b;



a = {12, 23, 32, 41, 45}
b = {22, 11, 43}


The point is I DON'T KNOW how many number of each line.



If I know line1 n numbers and line2 m numbers, I will use "for loop", and no question.



Just like,



for(int i = 0; i < n; i++) scanf("%d", a+i);
for(int i = 0; i < m; i++) scanf("%d", b+i);


But, I DO NOT know n and m, How to do, guys?










share|improve this question















For example, input is:



12 23 32 41 45
22 11 43


lines end with 'n',



I want save nums to a and b;



a = {12, 23, 32, 41, 45}
b = {22, 11, 43}


The point is I DON'T KNOW how many number of each line.



If I know line1 n numbers and line2 m numbers, I will use "for loop", and no question.



Just like,



for(int i = 0; i < n; i++) scanf("%d", a+i);
for(int i = 0; i < m; i++) scanf("%d", b+i);


But, I DO NOT know n and m, How to do, guys?







c scanf






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 19:54









ReAl

6061216




6061216










asked Nov 19 '18 at 16:58









user7331489user7331489

31




31












  • Can you accept a number max for array length ?
    – Ôrel
    Nov 19 '18 at 17:03






  • 1




    If you care about line endings, then you either need to read one character at a time with fgetc, or read a line at a time with fgets.
    – user3386109
    Nov 19 '18 at 17:06










  • Read characters until a new line is reached, if one isn't reached, resize your buffer? You'll want to use dynamic memory allocation probably.
    – jacob
    Nov 19 '18 at 17:07






  • 2




    A good function for reading a line at a time is getline, which was standardized in POSIX.1-2008. In comparision to fgets, it can read a line of arbitrary length into a dynamically allocated buffer. If you don't have the getline function, there are various standalone implementations of it, such as the one at github.com/ivanrad/getline .
    – Ian Abbott
    Nov 19 '18 at 17:19










  • You could use a while loop and some arbitrary input as break condition & dynamically alloc your input array(i think).
    – ats
    Nov 19 '18 at 17:25




















  • Can you accept a number max for array length ?
    – Ôrel
    Nov 19 '18 at 17:03






  • 1




    If you care about line endings, then you either need to read one character at a time with fgetc, or read a line at a time with fgets.
    – user3386109
    Nov 19 '18 at 17:06










  • Read characters until a new line is reached, if one isn't reached, resize your buffer? You'll want to use dynamic memory allocation probably.
    – jacob
    Nov 19 '18 at 17:07






  • 2




    A good function for reading a line at a time is getline, which was standardized in POSIX.1-2008. In comparision to fgets, it can read a line of arbitrary length into a dynamically allocated buffer. If you don't have the getline function, there are various standalone implementations of it, such as the one at github.com/ivanrad/getline .
    – Ian Abbott
    Nov 19 '18 at 17:19










  • You could use a while loop and some arbitrary input as break condition & dynamically alloc your input array(i think).
    – ats
    Nov 19 '18 at 17:25


















Can you accept a number max for array length ?
– Ôrel
Nov 19 '18 at 17:03




Can you accept a number max for array length ?
– Ôrel
Nov 19 '18 at 17:03




1




1




If you care about line endings, then you either need to read one character at a time with fgetc, or read a line at a time with fgets.
– user3386109
Nov 19 '18 at 17:06




If you care about line endings, then you either need to read one character at a time with fgetc, or read a line at a time with fgets.
– user3386109
Nov 19 '18 at 17:06












Read characters until a new line is reached, if one isn't reached, resize your buffer? You'll want to use dynamic memory allocation probably.
– jacob
Nov 19 '18 at 17:07




Read characters until a new line is reached, if one isn't reached, resize your buffer? You'll want to use dynamic memory allocation probably.
– jacob
Nov 19 '18 at 17:07




2




2




A good function for reading a line at a time is getline, which was standardized in POSIX.1-2008. In comparision to fgets, it can read a line of arbitrary length into a dynamically allocated buffer. If you don't have the getline function, there are various standalone implementations of it, such as the one at github.com/ivanrad/getline .
– Ian Abbott
Nov 19 '18 at 17:19




A good function for reading a line at a time is getline, which was standardized in POSIX.1-2008. In comparision to fgets, it can read a line of arbitrary length into a dynamically allocated buffer. If you don't have the getline function, there are various standalone implementations of it, such as the one at github.com/ivanrad/getline .
– Ian Abbott
Nov 19 '18 at 17:19












You could use a while loop and some arbitrary input as break condition & dynamically alloc your input array(i think).
– ats
Nov 19 '18 at 17:25






You could use a while loop and some arbitrary input as break condition & dynamically alloc your input array(i think).
– ats
Nov 19 '18 at 17:25














3 Answers
3






active

oldest

votes


















1














If you would like to continue using a scanf approach, I would recommend the negated scanset %[^] format specifier.



scanf("%[^n]", pointerToCharArray)



This should read in any number of characters up to but not including the character specified (which, in our case, is a newline). If you'd like to discard the newline, read it in as follows:



scanf("%[^n]n", pointerToCharArray)



A link to the reference page can be found below. The negated scanset specifier is included in the list:



http://www.cplusplus.com/reference/cstdio/scanf/



From this point, it is a simple matter to use strtok() to tokenize the output of the scanf into number arrays. If you are not familiar with strtok, another reference link is provided below:



http://www.cplusplus.com/reference/cstring/strtok/



I hope this helps!






share|improve this answer





















  • scanf("%[^n]n", pointerToCharArray) not only consumes a following newline, it waits for additional input consuming all other white-spaces. scanf() then does not return until following non-whitespace detected or EOF.
    – chux
    Nov 19 '18 at 18:57












  • As OP is concerned about "I DON'T KNOW how many number ", using "%[^n]" without a width limit is fragile code.
    – chux
    Nov 19 '18 at 18:58










  • Thank you very much!
    – user7331489
    Nov 22 '18 at 8:43



















0














Let us use the non standard getline, a common library extension. @Abbott to read the entire line into allocated memory.



If an array is needed and its size determined at run-time, use a variable length array available in C99 and optionally in C11.



Make a function to count and maybe save the numbers. Use strto...() functions for robust parsing.



size_t count_longs(char *line, long *dest) {
size_t count = 0;
char *s = line;
for (;;) {
char *endptr;
long num = strtol(s, &endptr, 10);
if (s == endptr) {
break; /// no conversion
}
s = endptr;
if (dest) {
dest[count] = num;
}
count++;
}
return count;
}


Sample code snippet



  char *line = NULL;
size_t len = 0;
ssize_t nread = getline(&line, &len, stdin); // read the whole line
if (nread != -1) {
// successful read

long a[count_longs(line, NULL)]; // determine array size and use a
count_longs(line, a); // 2nd pass: assign array elements.

nread = getline(&line, &len, stdin);
if (nread != -1) {
// successful read

long b[count_longs(line, NULL)];
count_longs(line, b);

// Do something with a and b
}
}
free(line);
len = 0;





share|improve this answer































    0














    A solution that could potentially fit what OP asked for would be the code below.
    The solution reads an unspecified number of integer values & stores them in arr. to break the loop/end input simply input a non-integer value. This could be further specified to search for a specific input etc.



    int c = 0;
    int *arr = NULL;
    int *extArr = NULL;
    int i = 0;
    for (i = 0; scanf("%d", &c) == 1; i++)
    {
    extArr = (int*)realloc(arr, (i+1) * sizeof(int));
    arr = extArr;
    arr[i] = c;

    }
    free(arr);


    I would, however personally go with a mix between the solution i provided & armitus answer(if i cant use getline or similar) since reallocating memory for every single int value seems redundant to me.



    Edit Since there has been some question about the code i provided and how to best use it in order to fulfill the OP's question. The codesnippet was supposed to showcase how one might create his own scanf-style function for unspecified number of integer inputs reading one full array at a time(in that case the free() has to be omitted till one is done with the array(obviously).






    share|improve this answer























    • I do see a functional problem. scanf("%d", &c) will read through an end-of-line like any other white-space. I'd expect OP's two lines of integers to end up in one arr here.
      – chux
      Nov 19 '18 at 19:02










    • The idea was to use my codesnippet as a standalone function & then just call it n times for the number of array's required. i admittedly omitted those details from my answer.
      – ats
      Nov 19 '18 at 19:06










    • Trouble is we do not know n prior to reading the line.
      – chux
      Nov 19 '18 at 19:08










    • Maybe i should've phrased that better. I did not mean n as in array length, but n as in number of array's read. Since the codesnippet can easily be packed inside a function and used as a sort of scanf-overload for array's
      – ats
      Nov 19 '18 at 19:10











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379386%2fhow-read-two-lines-numbers-from-input-to-2-arrays-c-language-without-knowing-t%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    If you would like to continue using a scanf approach, I would recommend the negated scanset %[^] format specifier.



    scanf("%[^n]", pointerToCharArray)



    This should read in any number of characters up to but not including the character specified (which, in our case, is a newline). If you'd like to discard the newline, read it in as follows:



    scanf("%[^n]n", pointerToCharArray)



    A link to the reference page can be found below. The negated scanset specifier is included in the list:



    http://www.cplusplus.com/reference/cstdio/scanf/



    From this point, it is a simple matter to use strtok() to tokenize the output of the scanf into number arrays. If you are not familiar with strtok, another reference link is provided below:



    http://www.cplusplus.com/reference/cstring/strtok/



    I hope this helps!






    share|improve this answer





















    • scanf("%[^n]n", pointerToCharArray) not only consumes a following newline, it waits for additional input consuming all other white-spaces. scanf() then does not return until following non-whitespace detected or EOF.
      – chux
      Nov 19 '18 at 18:57












    • As OP is concerned about "I DON'T KNOW how many number ", using "%[^n]" without a width limit is fragile code.
      – chux
      Nov 19 '18 at 18:58










    • Thank you very much!
      – user7331489
      Nov 22 '18 at 8:43
















    1














    If you would like to continue using a scanf approach, I would recommend the negated scanset %[^] format specifier.



    scanf("%[^n]", pointerToCharArray)



    This should read in any number of characters up to but not including the character specified (which, in our case, is a newline). If you'd like to discard the newline, read it in as follows:



    scanf("%[^n]n", pointerToCharArray)



    A link to the reference page can be found below. The negated scanset specifier is included in the list:



    http://www.cplusplus.com/reference/cstdio/scanf/



    From this point, it is a simple matter to use strtok() to tokenize the output of the scanf into number arrays. If you are not familiar with strtok, another reference link is provided below:



    http://www.cplusplus.com/reference/cstring/strtok/



    I hope this helps!






    share|improve this answer





















    • scanf("%[^n]n", pointerToCharArray) not only consumes a following newline, it waits for additional input consuming all other white-spaces. scanf() then does not return until following non-whitespace detected or EOF.
      – chux
      Nov 19 '18 at 18:57












    • As OP is concerned about "I DON'T KNOW how many number ", using "%[^n]" without a width limit is fragile code.
      – chux
      Nov 19 '18 at 18:58










    • Thank you very much!
      – user7331489
      Nov 22 '18 at 8:43














    1












    1








    1






    If you would like to continue using a scanf approach, I would recommend the negated scanset %[^] format specifier.



    scanf("%[^n]", pointerToCharArray)



    This should read in any number of characters up to but not including the character specified (which, in our case, is a newline). If you'd like to discard the newline, read it in as follows:



    scanf("%[^n]n", pointerToCharArray)



    A link to the reference page can be found below. The negated scanset specifier is included in the list:



    http://www.cplusplus.com/reference/cstdio/scanf/



    From this point, it is a simple matter to use strtok() to tokenize the output of the scanf into number arrays. If you are not familiar with strtok, another reference link is provided below:



    http://www.cplusplus.com/reference/cstring/strtok/



    I hope this helps!






    share|improve this answer












    If you would like to continue using a scanf approach, I would recommend the negated scanset %[^] format specifier.



    scanf("%[^n]", pointerToCharArray)



    This should read in any number of characters up to but not including the character specified (which, in our case, is a newline). If you'd like to discard the newline, read it in as follows:



    scanf("%[^n]n", pointerToCharArray)



    A link to the reference page can be found below. The negated scanset specifier is included in the list:



    http://www.cplusplus.com/reference/cstdio/scanf/



    From this point, it is a simple matter to use strtok() to tokenize the output of the scanf into number arrays. If you are not familiar with strtok, another reference link is provided below:



    http://www.cplusplus.com/reference/cstring/strtok/



    I hope this helps!







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 19 '18 at 17:51









    armitusarmitus

    845




    845












    • scanf("%[^n]n", pointerToCharArray) not only consumes a following newline, it waits for additional input consuming all other white-spaces. scanf() then does not return until following non-whitespace detected or EOF.
      – chux
      Nov 19 '18 at 18:57












    • As OP is concerned about "I DON'T KNOW how many number ", using "%[^n]" without a width limit is fragile code.
      – chux
      Nov 19 '18 at 18:58










    • Thank you very much!
      – user7331489
      Nov 22 '18 at 8:43


















    • scanf("%[^n]n", pointerToCharArray) not only consumes a following newline, it waits for additional input consuming all other white-spaces. scanf() then does not return until following non-whitespace detected or EOF.
      – chux
      Nov 19 '18 at 18:57












    • As OP is concerned about "I DON'T KNOW how many number ", using "%[^n]" without a width limit is fragile code.
      – chux
      Nov 19 '18 at 18:58










    • Thank you very much!
      – user7331489
      Nov 22 '18 at 8:43
















    scanf("%[^n]n", pointerToCharArray) not only consumes a following newline, it waits for additional input consuming all other white-spaces. scanf() then does not return until following non-whitespace detected or EOF.
    – chux
    Nov 19 '18 at 18:57






    scanf("%[^n]n", pointerToCharArray) not only consumes a following newline, it waits for additional input consuming all other white-spaces. scanf() then does not return until following non-whitespace detected or EOF.
    – chux
    Nov 19 '18 at 18:57














    As OP is concerned about "I DON'T KNOW how many number ", using "%[^n]" without a width limit is fragile code.
    – chux
    Nov 19 '18 at 18:58




    As OP is concerned about "I DON'T KNOW how many number ", using "%[^n]" without a width limit is fragile code.
    – chux
    Nov 19 '18 at 18:58












    Thank you very much!
    – user7331489
    Nov 22 '18 at 8:43




    Thank you very much!
    – user7331489
    Nov 22 '18 at 8:43













    0














    Let us use the non standard getline, a common library extension. @Abbott to read the entire line into allocated memory.



    If an array is needed and its size determined at run-time, use a variable length array available in C99 and optionally in C11.



    Make a function to count and maybe save the numbers. Use strto...() functions for robust parsing.



    size_t count_longs(char *line, long *dest) {
    size_t count = 0;
    char *s = line;
    for (;;) {
    char *endptr;
    long num = strtol(s, &endptr, 10);
    if (s == endptr) {
    break; /// no conversion
    }
    s = endptr;
    if (dest) {
    dest[count] = num;
    }
    count++;
    }
    return count;
    }


    Sample code snippet



      char *line = NULL;
    size_t len = 0;
    ssize_t nread = getline(&line, &len, stdin); // read the whole line
    if (nread != -1) {
    // successful read

    long a[count_longs(line, NULL)]; // determine array size and use a
    count_longs(line, a); // 2nd pass: assign array elements.

    nread = getline(&line, &len, stdin);
    if (nread != -1) {
    // successful read

    long b[count_longs(line, NULL)];
    count_longs(line, b);

    // Do something with a and b
    }
    }
    free(line);
    len = 0;





    share|improve this answer




























      0














      Let us use the non standard getline, a common library extension. @Abbott to read the entire line into allocated memory.



      If an array is needed and its size determined at run-time, use a variable length array available in C99 and optionally in C11.



      Make a function to count and maybe save the numbers. Use strto...() functions for robust parsing.



      size_t count_longs(char *line, long *dest) {
      size_t count = 0;
      char *s = line;
      for (;;) {
      char *endptr;
      long num = strtol(s, &endptr, 10);
      if (s == endptr) {
      break; /// no conversion
      }
      s = endptr;
      if (dest) {
      dest[count] = num;
      }
      count++;
      }
      return count;
      }


      Sample code snippet



        char *line = NULL;
      size_t len = 0;
      ssize_t nread = getline(&line, &len, stdin); // read the whole line
      if (nread != -1) {
      // successful read

      long a[count_longs(line, NULL)]; // determine array size and use a
      count_longs(line, a); // 2nd pass: assign array elements.

      nread = getline(&line, &len, stdin);
      if (nread != -1) {
      // successful read

      long b[count_longs(line, NULL)];
      count_longs(line, b);

      // Do something with a and b
      }
      }
      free(line);
      len = 0;





      share|improve this answer


























        0












        0








        0






        Let us use the non standard getline, a common library extension. @Abbott to read the entire line into allocated memory.



        If an array is needed and its size determined at run-time, use a variable length array available in C99 and optionally in C11.



        Make a function to count and maybe save the numbers. Use strto...() functions for robust parsing.



        size_t count_longs(char *line, long *dest) {
        size_t count = 0;
        char *s = line;
        for (;;) {
        char *endptr;
        long num = strtol(s, &endptr, 10);
        if (s == endptr) {
        break; /// no conversion
        }
        s = endptr;
        if (dest) {
        dest[count] = num;
        }
        count++;
        }
        return count;
        }


        Sample code snippet



          char *line = NULL;
        size_t len = 0;
        ssize_t nread = getline(&line, &len, stdin); // read the whole line
        if (nread != -1) {
        // successful read

        long a[count_longs(line, NULL)]; // determine array size and use a
        count_longs(line, a); // 2nd pass: assign array elements.

        nread = getline(&line, &len, stdin);
        if (nread != -1) {
        // successful read

        long b[count_longs(line, NULL)];
        count_longs(line, b);

        // Do something with a and b
        }
        }
        free(line);
        len = 0;





        share|improve this answer














        Let us use the non standard getline, a common library extension. @Abbott to read the entire line into allocated memory.



        If an array is needed and its size determined at run-time, use a variable length array available in C99 and optionally in C11.



        Make a function to count and maybe save the numbers. Use strto...() functions for robust parsing.



        size_t count_longs(char *line, long *dest) {
        size_t count = 0;
        char *s = line;
        for (;;) {
        char *endptr;
        long num = strtol(s, &endptr, 10);
        if (s == endptr) {
        break; /// no conversion
        }
        s = endptr;
        if (dest) {
        dest[count] = num;
        }
        count++;
        }
        return count;
        }


        Sample code snippet



          char *line = NULL;
        size_t len = 0;
        ssize_t nread = getline(&line, &len, stdin); // read the whole line
        if (nread != -1) {
        // successful read

        long a[count_longs(line, NULL)]; // determine array size and use a
        count_longs(line, a); // 2nd pass: assign array elements.

        nread = getline(&line, &len, stdin);
        if (nread != -1) {
        // successful read

        long b[count_longs(line, NULL)];
        count_longs(line, b);

        // Do something with a and b
        }
        }
        free(line);
        len = 0;






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 19 '18 at 18:19

























        answered Nov 19 '18 at 18:05









        chuxchux

        80.8k870148




        80.8k870148























            0














            A solution that could potentially fit what OP asked for would be the code below.
            The solution reads an unspecified number of integer values & stores them in arr. to break the loop/end input simply input a non-integer value. This could be further specified to search for a specific input etc.



            int c = 0;
            int *arr = NULL;
            int *extArr = NULL;
            int i = 0;
            for (i = 0; scanf("%d", &c) == 1; i++)
            {
            extArr = (int*)realloc(arr, (i+1) * sizeof(int));
            arr = extArr;
            arr[i] = c;

            }
            free(arr);


            I would, however personally go with a mix between the solution i provided & armitus answer(if i cant use getline or similar) since reallocating memory for every single int value seems redundant to me.



            Edit Since there has been some question about the code i provided and how to best use it in order to fulfill the OP's question. The codesnippet was supposed to showcase how one might create his own scanf-style function for unspecified number of integer inputs reading one full array at a time(in that case the free() has to be omitted till one is done with the array(obviously).






            share|improve this answer























            • I do see a functional problem. scanf("%d", &c) will read through an end-of-line like any other white-space. I'd expect OP's two lines of integers to end up in one arr here.
              – chux
              Nov 19 '18 at 19:02










            • The idea was to use my codesnippet as a standalone function & then just call it n times for the number of array's required. i admittedly omitted those details from my answer.
              – ats
              Nov 19 '18 at 19:06










            • Trouble is we do not know n prior to reading the line.
              – chux
              Nov 19 '18 at 19:08










            • Maybe i should've phrased that better. I did not mean n as in array length, but n as in number of array's read. Since the codesnippet can easily be packed inside a function and used as a sort of scanf-overload for array's
              – ats
              Nov 19 '18 at 19:10
















            0














            A solution that could potentially fit what OP asked for would be the code below.
            The solution reads an unspecified number of integer values & stores them in arr. to break the loop/end input simply input a non-integer value. This could be further specified to search for a specific input etc.



            int c = 0;
            int *arr = NULL;
            int *extArr = NULL;
            int i = 0;
            for (i = 0; scanf("%d", &c) == 1; i++)
            {
            extArr = (int*)realloc(arr, (i+1) * sizeof(int));
            arr = extArr;
            arr[i] = c;

            }
            free(arr);


            I would, however personally go with a mix between the solution i provided & armitus answer(if i cant use getline or similar) since reallocating memory for every single int value seems redundant to me.



            Edit Since there has been some question about the code i provided and how to best use it in order to fulfill the OP's question. The codesnippet was supposed to showcase how one might create his own scanf-style function for unspecified number of integer inputs reading one full array at a time(in that case the free() has to be omitted till one is done with the array(obviously).






            share|improve this answer























            • I do see a functional problem. scanf("%d", &c) will read through an end-of-line like any other white-space. I'd expect OP's two lines of integers to end up in one arr here.
              – chux
              Nov 19 '18 at 19:02










            • The idea was to use my codesnippet as a standalone function & then just call it n times for the number of array's required. i admittedly omitted those details from my answer.
              – ats
              Nov 19 '18 at 19:06










            • Trouble is we do not know n prior to reading the line.
              – chux
              Nov 19 '18 at 19:08










            • Maybe i should've phrased that better. I did not mean n as in array length, but n as in number of array's read. Since the codesnippet can easily be packed inside a function and used as a sort of scanf-overload for array's
              – ats
              Nov 19 '18 at 19:10














            0












            0








            0






            A solution that could potentially fit what OP asked for would be the code below.
            The solution reads an unspecified number of integer values & stores them in arr. to break the loop/end input simply input a non-integer value. This could be further specified to search for a specific input etc.



            int c = 0;
            int *arr = NULL;
            int *extArr = NULL;
            int i = 0;
            for (i = 0; scanf("%d", &c) == 1; i++)
            {
            extArr = (int*)realloc(arr, (i+1) * sizeof(int));
            arr = extArr;
            arr[i] = c;

            }
            free(arr);


            I would, however personally go with a mix between the solution i provided & armitus answer(if i cant use getline or similar) since reallocating memory for every single int value seems redundant to me.



            Edit Since there has been some question about the code i provided and how to best use it in order to fulfill the OP's question. The codesnippet was supposed to showcase how one might create his own scanf-style function for unspecified number of integer inputs reading one full array at a time(in that case the free() has to be omitted till one is done with the array(obviously).






            share|improve this answer














            A solution that could potentially fit what OP asked for would be the code below.
            The solution reads an unspecified number of integer values & stores them in arr. to break the loop/end input simply input a non-integer value. This could be further specified to search for a specific input etc.



            int c = 0;
            int *arr = NULL;
            int *extArr = NULL;
            int i = 0;
            for (i = 0; scanf("%d", &c) == 1; i++)
            {
            extArr = (int*)realloc(arr, (i+1) * sizeof(int));
            arr = extArr;
            arr[i] = c;

            }
            free(arr);


            I would, however personally go with a mix between the solution i provided & armitus answer(if i cant use getline or similar) since reallocating memory for every single int value seems redundant to me.



            Edit Since there has been some question about the code i provided and how to best use it in order to fulfill the OP's question. The codesnippet was supposed to showcase how one might create his own scanf-style function for unspecified number of integer inputs reading one full array at a time(in that case the free() has to be omitted till one is done with the array(obviously).







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 19 '18 at 19:08

























            answered Nov 19 '18 at 18:37









            atsats

            694




            694












            • I do see a functional problem. scanf("%d", &c) will read through an end-of-line like any other white-space. I'd expect OP's two lines of integers to end up in one arr here.
              – chux
              Nov 19 '18 at 19:02










            • The idea was to use my codesnippet as a standalone function & then just call it n times for the number of array's required. i admittedly omitted those details from my answer.
              – ats
              Nov 19 '18 at 19:06










            • Trouble is we do not know n prior to reading the line.
              – chux
              Nov 19 '18 at 19:08










            • Maybe i should've phrased that better. I did not mean n as in array length, but n as in number of array's read. Since the codesnippet can easily be packed inside a function and used as a sort of scanf-overload for array's
              – ats
              Nov 19 '18 at 19:10


















            • I do see a functional problem. scanf("%d", &c) will read through an end-of-line like any other white-space. I'd expect OP's two lines of integers to end up in one arr here.
              – chux
              Nov 19 '18 at 19:02










            • The idea was to use my codesnippet as a standalone function & then just call it n times for the number of array's required. i admittedly omitted those details from my answer.
              – ats
              Nov 19 '18 at 19:06










            • Trouble is we do not know n prior to reading the line.
              – chux
              Nov 19 '18 at 19:08










            • Maybe i should've phrased that better. I did not mean n as in array length, but n as in number of array's read. Since the codesnippet can easily be packed inside a function and used as a sort of scanf-overload for array's
              – ats
              Nov 19 '18 at 19:10
















            I do see a functional problem. scanf("%d", &c) will read through an end-of-line like any other white-space. I'd expect OP's two lines of integers to end up in one arr here.
            – chux
            Nov 19 '18 at 19:02




            I do see a functional problem. scanf("%d", &c) will read through an end-of-line like any other white-space. I'd expect OP's two lines of integers to end up in one arr here.
            – chux
            Nov 19 '18 at 19:02












            The idea was to use my codesnippet as a standalone function & then just call it n times for the number of array's required. i admittedly omitted those details from my answer.
            – ats
            Nov 19 '18 at 19:06




            The idea was to use my codesnippet as a standalone function & then just call it n times for the number of array's required. i admittedly omitted those details from my answer.
            – ats
            Nov 19 '18 at 19:06












            Trouble is we do not know n prior to reading the line.
            – chux
            Nov 19 '18 at 19:08




            Trouble is we do not know n prior to reading the line.
            – chux
            Nov 19 '18 at 19:08












            Maybe i should've phrased that better. I did not mean n as in array length, but n as in number of array's read. Since the codesnippet can easily be packed inside a function and used as a sort of scanf-overload for array's
            – ats
            Nov 19 '18 at 19:10




            Maybe i should've phrased that better. I did not mean n as in array length, but n as in number of array's read. Since the codesnippet can easily be packed inside a function and used as a sort of scanf-overload for array's
            – ats
            Nov 19 '18 at 19:10


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379386%2fhow-read-two-lines-numbers-from-input-to-2-arrays-c-language-without-knowing-t%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            Npm cannot find a required file even through it is in the searched directory