Getting substrings in C [closed]












1















I currently have this string called'json' in C "{"method":"emptyFood","params":"08:00 09:00 10:00"}"



and trying to extract the times into 3 different variables.



for(int i = 0; i<5; i++)
{
ts1[i] = json[i+32];
}

for(int j = 0; j<5; j++)
{
ts2[j] = json[j+38];
}

for (int k = 0; k<5; k++)
{
ts3[k] = json[k+44];
}

printf("time1 = %sn", ts1);
printf("time2 = %sn", ts2);
printf("time3 = %sn", ts3);


If I print them all at the same time, I get strange outputs like this



time1 = 08:009:010:00
time2 = 09:010:00
time3 = 10:00


However if I print them one by one, it prints out the correct time for each print statement.



What am I doing wrong?










share|improve this question













closed as off-topic by Jonathan Leffler, Weather Vane, chux, Unheilig, EdChum Nov 22 '18 at 9:32


This question appears to be off-topic. The users who voted to close gave these specific reasons:



  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Jonathan Leffler, EdChum

  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Weather Vane, chux


If this question can be reworded to fit the rules in the help center, please edit the question.












  • 6





    You aren't zero-terminating your strings.

    – Lee Daniel Crocker
    Nov 21 '18 at 21:36











  • Can you show us "printing them all at the same time" vs "printing them 1 by 1"?

    – CoffeeTableEspresso
    Nov 21 '18 at 21:36











  • Can you show the target string definitions?

    – Weather Vane
    Nov 21 '18 at 21:36






  • 3





    Please include a main() function and provide a Compilable, Minimal, Complete, and Verifiable Example. It makes it a lot easier to reproduce what you are seeing.

    – John Murray
    Nov 21 '18 at 21:43






  • 2





    "What am I doing wrong?" --> You are not posting the definitions of ts1, ts2, ts3

    – chux
    Nov 21 '18 at 21:55


















1















I currently have this string called'json' in C "{"method":"emptyFood","params":"08:00 09:00 10:00"}"



and trying to extract the times into 3 different variables.



for(int i = 0; i<5; i++)
{
ts1[i] = json[i+32];
}

for(int j = 0; j<5; j++)
{
ts2[j] = json[j+38];
}

for (int k = 0; k<5; k++)
{
ts3[k] = json[k+44];
}

printf("time1 = %sn", ts1);
printf("time2 = %sn", ts2);
printf("time3 = %sn", ts3);


If I print them all at the same time, I get strange outputs like this



time1 = 08:009:010:00
time2 = 09:010:00
time3 = 10:00


However if I print them one by one, it prints out the correct time for each print statement.



What am I doing wrong?










share|improve this question













closed as off-topic by Jonathan Leffler, Weather Vane, chux, Unheilig, EdChum Nov 22 '18 at 9:32


This question appears to be off-topic. The users who voted to close gave these specific reasons:



  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Jonathan Leffler, EdChum

  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Weather Vane, chux


If this question can be reworded to fit the rules in the help center, please edit the question.












  • 6





    You aren't zero-terminating your strings.

    – Lee Daniel Crocker
    Nov 21 '18 at 21:36











  • Can you show us "printing them all at the same time" vs "printing them 1 by 1"?

    – CoffeeTableEspresso
    Nov 21 '18 at 21:36











  • Can you show the target string definitions?

    – Weather Vane
    Nov 21 '18 at 21:36






  • 3





    Please include a main() function and provide a Compilable, Minimal, Complete, and Verifiable Example. It makes it a lot easier to reproduce what you are seeing.

    – John Murray
    Nov 21 '18 at 21:43






  • 2





    "What am I doing wrong?" --> You are not posting the definitions of ts1, ts2, ts3

    – chux
    Nov 21 '18 at 21:55
















1












1








1








I currently have this string called'json' in C "{"method":"emptyFood","params":"08:00 09:00 10:00"}"



and trying to extract the times into 3 different variables.



for(int i = 0; i<5; i++)
{
ts1[i] = json[i+32];
}

for(int j = 0; j<5; j++)
{
ts2[j] = json[j+38];
}

for (int k = 0; k<5; k++)
{
ts3[k] = json[k+44];
}

printf("time1 = %sn", ts1);
printf("time2 = %sn", ts2);
printf("time3 = %sn", ts3);


If I print them all at the same time, I get strange outputs like this



time1 = 08:009:010:00
time2 = 09:010:00
time3 = 10:00


However if I print them one by one, it prints out the correct time for each print statement.



What am I doing wrong?










share|improve this question














I currently have this string called'json' in C "{"method":"emptyFood","params":"08:00 09:00 10:00"}"



and trying to extract the times into 3 different variables.



for(int i = 0; i<5; i++)
{
ts1[i] = json[i+32];
}

for(int j = 0; j<5; j++)
{
ts2[j] = json[j+38];
}

for (int k = 0; k<5; k++)
{
ts3[k] = json[k+44];
}

printf("time1 = %sn", ts1);
printf("time2 = %sn", ts2);
printf("time3 = %sn", ts3);


If I print them all at the same time, I get strange outputs like this



time1 = 08:009:010:00
time2 = 09:010:00
time3 = 10:00


However if I print them one by one, it prints out the correct time for each print statement.



What am I doing wrong?







c string






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 21:32









HerofireHerofire

416




416




closed as off-topic by Jonathan Leffler, Weather Vane, chux, Unheilig, EdChum Nov 22 '18 at 9:32


This question appears to be off-topic. The users who voted to close gave these specific reasons:



  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Jonathan Leffler, EdChum

  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Weather Vane, chux


If this question can be reworded to fit the rules in the help center, please edit the question.







closed as off-topic by Jonathan Leffler, Weather Vane, chux, Unheilig, EdChum Nov 22 '18 at 9:32


This question appears to be off-topic. The users who voted to close gave these specific reasons:



  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Jonathan Leffler, EdChum

  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Weather Vane, chux


If this question can be reworded to fit the rules in the help center, please edit the question.








  • 6





    You aren't zero-terminating your strings.

    – Lee Daniel Crocker
    Nov 21 '18 at 21:36











  • Can you show us "printing them all at the same time" vs "printing them 1 by 1"?

    – CoffeeTableEspresso
    Nov 21 '18 at 21:36











  • Can you show the target string definitions?

    – Weather Vane
    Nov 21 '18 at 21:36






  • 3





    Please include a main() function and provide a Compilable, Minimal, Complete, and Verifiable Example. It makes it a lot easier to reproduce what you are seeing.

    – John Murray
    Nov 21 '18 at 21:43






  • 2





    "What am I doing wrong?" --> You are not posting the definitions of ts1, ts2, ts3

    – chux
    Nov 21 '18 at 21:55
















  • 6





    You aren't zero-terminating your strings.

    – Lee Daniel Crocker
    Nov 21 '18 at 21:36











  • Can you show us "printing them all at the same time" vs "printing them 1 by 1"?

    – CoffeeTableEspresso
    Nov 21 '18 at 21:36











  • Can you show the target string definitions?

    – Weather Vane
    Nov 21 '18 at 21:36






  • 3





    Please include a main() function and provide a Compilable, Minimal, Complete, and Verifiable Example. It makes it a lot easier to reproduce what you are seeing.

    – John Murray
    Nov 21 '18 at 21:43






  • 2





    "What am I doing wrong?" --> You are not posting the definitions of ts1, ts2, ts3

    – chux
    Nov 21 '18 at 21:55










6




6





You aren't zero-terminating your strings.

– Lee Daniel Crocker
Nov 21 '18 at 21:36





You aren't zero-terminating your strings.

– Lee Daniel Crocker
Nov 21 '18 at 21:36













Can you show us "printing them all at the same time" vs "printing them 1 by 1"?

– CoffeeTableEspresso
Nov 21 '18 at 21:36





Can you show us "printing them all at the same time" vs "printing them 1 by 1"?

– CoffeeTableEspresso
Nov 21 '18 at 21:36













Can you show the target string definitions?

– Weather Vane
Nov 21 '18 at 21:36





Can you show the target string definitions?

– Weather Vane
Nov 21 '18 at 21:36




3




3





Please include a main() function and provide a Compilable, Minimal, Complete, and Verifiable Example. It makes it a lot easier to reproduce what you are seeing.

– John Murray
Nov 21 '18 at 21:43





Please include a main() function and provide a Compilable, Minimal, Complete, and Verifiable Example. It makes it a lot easier to reproduce what you are seeing.

– John Murray
Nov 21 '18 at 21:43




2




2





"What am I doing wrong?" --> You are not posting the definitions of ts1, ts2, ts3

– chux
Nov 21 '18 at 21:55







"What am I doing wrong?" --> You are not posting the definitions of ts1, ts2, ts3

– chux
Nov 21 '18 at 21:55














1 Answer
1






active

oldest

votes


















0














Strings in C are null-terminated. This means that a string ends with a null byte. (A byte in a string has the type char.) When you copy the bytes of a substring to a new array of bytes, you're copying the “useful” content of the string, but to get a complete string, you also need to write the null terminator. For example:



for(int i = 0; i<5; i++)
{
ts1[i] = json[i+32];
}
ts1[5] = 0;


(Don't confuse the null byte 0, which can also be written '', with the digit '0'.)



A more idiomatic way of writing this would be



memcpy(ts1, json + 32, 5);
ts1[5] = 0;


A better-designed function to copy a substring is strlcpy but unfortunately it is not part of standard C so many platforms don't have it. Note that strncpy doesn't do the job well because it wouldn't write a null byte in your case.



Note that you need 6 bytes to store a 5-byte string, i.e. you must declare char ts1[6] or char *ts1 = malloc(6) (or with a larger number).



The reason it seemed to work when you printed the strings one by one is that there must have been a null byte immediately after the string in memory. This is just a coincidence and might not happen if the function was called from different place, if the code before it had worked on different data, if compiled with a different compiler, etc. Given the output you got with the three variables, what must have happened is that the compiler happened to put ts1, ts2 and ts3 and something else which happened to start with a null byte consecutively in memory. So when you tried to print ts1, the string at that address ran over ts1, ts2 and ts3 to finally stop at that null byte afterward. Having a null byte was just a stroke of luck, or maybe a stroke of unluck since it hid the bug about the missing null terminator.






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Strings in C are null-terminated. This means that a string ends with a null byte. (A byte in a string has the type char.) When you copy the bytes of a substring to a new array of bytes, you're copying the “useful” content of the string, but to get a complete string, you also need to write the null terminator. For example:



    for(int i = 0; i<5; i++)
    {
    ts1[i] = json[i+32];
    }
    ts1[5] = 0;


    (Don't confuse the null byte 0, which can also be written '', with the digit '0'.)



    A more idiomatic way of writing this would be



    memcpy(ts1, json + 32, 5);
    ts1[5] = 0;


    A better-designed function to copy a substring is strlcpy but unfortunately it is not part of standard C so many platforms don't have it. Note that strncpy doesn't do the job well because it wouldn't write a null byte in your case.



    Note that you need 6 bytes to store a 5-byte string, i.e. you must declare char ts1[6] or char *ts1 = malloc(6) (or with a larger number).



    The reason it seemed to work when you printed the strings one by one is that there must have been a null byte immediately after the string in memory. This is just a coincidence and might not happen if the function was called from different place, if the code before it had worked on different data, if compiled with a different compiler, etc. Given the output you got with the three variables, what must have happened is that the compiler happened to put ts1, ts2 and ts3 and something else which happened to start with a null byte consecutively in memory. So when you tried to print ts1, the string at that address ran over ts1, ts2 and ts3 to finally stop at that null byte afterward. Having a null byte was just a stroke of luck, or maybe a stroke of unluck since it hid the bug about the missing null terminator.






    share|improve this answer




























      0














      Strings in C are null-terminated. This means that a string ends with a null byte. (A byte in a string has the type char.) When you copy the bytes of a substring to a new array of bytes, you're copying the “useful” content of the string, but to get a complete string, you also need to write the null terminator. For example:



      for(int i = 0; i<5; i++)
      {
      ts1[i] = json[i+32];
      }
      ts1[5] = 0;


      (Don't confuse the null byte 0, which can also be written '', with the digit '0'.)



      A more idiomatic way of writing this would be



      memcpy(ts1, json + 32, 5);
      ts1[5] = 0;


      A better-designed function to copy a substring is strlcpy but unfortunately it is not part of standard C so many platforms don't have it. Note that strncpy doesn't do the job well because it wouldn't write a null byte in your case.



      Note that you need 6 bytes to store a 5-byte string, i.e. you must declare char ts1[6] or char *ts1 = malloc(6) (or with a larger number).



      The reason it seemed to work when you printed the strings one by one is that there must have been a null byte immediately after the string in memory. This is just a coincidence and might not happen if the function was called from different place, if the code before it had worked on different data, if compiled with a different compiler, etc. Given the output you got with the three variables, what must have happened is that the compiler happened to put ts1, ts2 and ts3 and something else which happened to start with a null byte consecutively in memory. So when you tried to print ts1, the string at that address ran over ts1, ts2 and ts3 to finally stop at that null byte afterward. Having a null byte was just a stroke of luck, or maybe a stroke of unluck since it hid the bug about the missing null terminator.






      share|improve this answer


























        0












        0








        0







        Strings in C are null-terminated. This means that a string ends with a null byte. (A byte in a string has the type char.) When you copy the bytes of a substring to a new array of bytes, you're copying the “useful” content of the string, but to get a complete string, you also need to write the null terminator. For example:



        for(int i = 0; i<5; i++)
        {
        ts1[i] = json[i+32];
        }
        ts1[5] = 0;


        (Don't confuse the null byte 0, which can also be written '', with the digit '0'.)



        A more idiomatic way of writing this would be



        memcpy(ts1, json + 32, 5);
        ts1[5] = 0;


        A better-designed function to copy a substring is strlcpy but unfortunately it is not part of standard C so many platforms don't have it. Note that strncpy doesn't do the job well because it wouldn't write a null byte in your case.



        Note that you need 6 bytes to store a 5-byte string, i.e. you must declare char ts1[6] or char *ts1 = malloc(6) (or with a larger number).



        The reason it seemed to work when you printed the strings one by one is that there must have been a null byte immediately after the string in memory. This is just a coincidence and might not happen if the function was called from different place, if the code before it had worked on different data, if compiled with a different compiler, etc. Given the output you got with the three variables, what must have happened is that the compiler happened to put ts1, ts2 and ts3 and something else which happened to start with a null byte consecutively in memory. So when you tried to print ts1, the string at that address ran over ts1, ts2 and ts3 to finally stop at that null byte afterward. Having a null byte was just a stroke of luck, or maybe a stroke of unluck since it hid the bug about the missing null terminator.






        share|improve this answer













        Strings in C are null-terminated. This means that a string ends with a null byte. (A byte in a string has the type char.) When you copy the bytes of a substring to a new array of bytes, you're copying the “useful” content of the string, but to get a complete string, you also need to write the null terminator. For example:



        for(int i = 0; i<5; i++)
        {
        ts1[i] = json[i+32];
        }
        ts1[5] = 0;


        (Don't confuse the null byte 0, which can also be written '', with the digit '0'.)



        A more idiomatic way of writing this would be



        memcpy(ts1, json + 32, 5);
        ts1[5] = 0;


        A better-designed function to copy a substring is strlcpy but unfortunately it is not part of standard C so many platforms don't have it. Note that strncpy doesn't do the job well because it wouldn't write a null byte in your case.



        Note that you need 6 bytes to store a 5-byte string, i.e. you must declare char ts1[6] or char *ts1 = malloc(6) (or with a larger number).



        The reason it seemed to work when you printed the strings one by one is that there must have been a null byte immediately after the string in memory. This is just a coincidence and might not happen if the function was called from different place, if the code before it had worked on different data, if compiled with a different compiler, etc. Given the output you got with the three variables, what must have happened is that the compiler happened to put ts1, ts2 and ts3 and something else which happened to start with a null byte consecutively in memory. So when you tried to print ts1, the string at that address ran over ts1, ts2 and ts3 to finally stop at that null byte afterward. Having a null byte was just a stroke of luck, or maybe a stroke of unluck since it hid the bug about the missing null terminator.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 6:57









        GillesGilles

        75.4k19161205




        75.4k19161205

















            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

            How to fix TextFormField cause rebuild widget in Flutter