Loop through indexes in C












-2















I would like to look for values in an array that equal to 0 up to a certain index, but the function sets all my previous values to 0; instead of checking for 0s.



a is the specified index, and Mem is an array of size 100.



for(int i=0;i<a;i++){
if (Mem[i]=0){
printf("Error!n");
}
}


I'm trying to check for 0s in an array up to a certain index.

If there are any, I need to output an error.










share|improve this question

























  • What makes you doubt the code? Any unwanted behaviour observed?

    – Yunnosch
    Nov 20 '18 at 19:24











  • Yeah, the function sets all my previous values to 0; instead of checking for 0s

    – user10158754
    Nov 20 '18 at 19:27











  • Do you want Mem[a] included in the check?

    – Yunnosch
    Nov 20 '18 at 19:27






  • 1





    And do you see any part of your code which does something like Mem[i]=0 ? (Caught me at reading lazily by the way ;-)

    – Yunnosch
    Nov 20 '18 at 19:28








  • 1





    For better future questions, please study the concept of making a Minimal, Complete, and Verifiable example.

    – Yunnosch
    Nov 20 '18 at 19:41
















-2















I would like to look for values in an array that equal to 0 up to a certain index, but the function sets all my previous values to 0; instead of checking for 0s.



a is the specified index, and Mem is an array of size 100.



for(int i=0;i<a;i++){
if (Mem[i]=0){
printf("Error!n");
}
}


I'm trying to check for 0s in an array up to a certain index.

If there are any, I need to output an error.










share|improve this question

























  • What makes you doubt the code? Any unwanted behaviour observed?

    – Yunnosch
    Nov 20 '18 at 19:24











  • Yeah, the function sets all my previous values to 0; instead of checking for 0s

    – user10158754
    Nov 20 '18 at 19:27











  • Do you want Mem[a] included in the check?

    – Yunnosch
    Nov 20 '18 at 19:27






  • 1





    And do you see any part of your code which does something like Mem[i]=0 ? (Caught me at reading lazily by the way ;-)

    – Yunnosch
    Nov 20 '18 at 19:28








  • 1





    For better future questions, please study the concept of making a Minimal, Complete, and Verifiable example.

    – Yunnosch
    Nov 20 '18 at 19:41














-2












-2








-2








I would like to look for values in an array that equal to 0 up to a certain index, but the function sets all my previous values to 0; instead of checking for 0s.



a is the specified index, and Mem is an array of size 100.



for(int i=0;i<a;i++){
if (Mem[i]=0){
printf("Error!n");
}
}


I'm trying to check for 0s in an array up to a certain index.

If there are any, I need to output an error.










share|improve this question
















I would like to look for values in an array that equal to 0 up to a certain index, but the function sets all my previous values to 0; instead of checking for 0s.



a is the specified index, and Mem is an array of size 100.



for(int i=0;i<a;i++){
if (Mem[i]=0){
printf("Error!n");
}
}


I'm trying to check for 0s in an array up to a certain index.

If there are any, I need to output an error.







c arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 19:40









Yunnosch

11.3k52033




11.3k52033










asked Nov 20 '18 at 19:22







user10158754




















  • What makes you doubt the code? Any unwanted behaviour observed?

    – Yunnosch
    Nov 20 '18 at 19:24











  • Yeah, the function sets all my previous values to 0; instead of checking for 0s

    – user10158754
    Nov 20 '18 at 19:27











  • Do you want Mem[a] included in the check?

    – Yunnosch
    Nov 20 '18 at 19:27






  • 1





    And do you see any part of your code which does something like Mem[i]=0 ? (Caught me at reading lazily by the way ;-)

    – Yunnosch
    Nov 20 '18 at 19:28








  • 1





    For better future questions, please study the concept of making a Minimal, Complete, and Verifiable example.

    – Yunnosch
    Nov 20 '18 at 19:41



















  • What makes you doubt the code? Any unwanted behaviour observed?

    – Yunnosch
    Nov 20 '18 at 19:24











  • Yeah, the function sets all my previous values to 0; instead of checking for 0s

    – user10158754
    Nov 20 '18 at 19:27











  • Do you want Mem[a] included in the check?

    – Yunnosch
    Nov 20 '18 at 19:27






  • 1





    And do you see any part of your code which does something like Mem[i]=0 ? (Caught me at reading lazily by the way ;-)

    – Yunnosch
    Nov 20 '18 at 19:28








  • 1





    For better future questions, please study the concept of making a Minimal, Complete, and Verifiable example.

    – Yunnosch
    Nov 20 '18 at 19:41

















What makes you doubt the code? Any unwanted behaviour observed?

– Yunnosch
Nov 20 '18 at 19:24





What makes you doubt the code? Any unwanted behaviour observed?

– Yunnosch
Nov 20 '18 at 19:24













Yeah, the function sets all my previous values to 0; instead of checking for 0s

– user10158754
Nov 20 '18 at 19:27





Yeah, the function sets all my previous values to 0; instead of checking for 0s

– user10158754
Nov 20 '18 at 19:27













Do you want Mem[a] included in the check?

– Yunnosch
Nov 20 '18 at 19:27





Do you want Mem[a] included in the check?

– Yunnosch
Nov 20 '18 at 19:27




1




1





And do you see any part of your code which does something like Mem[i]=0 ? (Caught me at reading lazily by the way ;-)

– Yunnosch
Nov 20 '18 at 19:28







And do you see any part of your code which does something like Mem[i]=0 ? (Caught me at reading lazily by the way ;-)

– Yunnosch
Nov 20 '18 at 19:28






1




1





For better future questions, please study the concept of making a Minimal, Complete, and Verifiable example.

– Yunnosch
Nov 20 '18 at 19:41





For better future questions, please study the concept of making a Minimal, Complete, and Verifiable example.

– Yunnosch
Nov 20 '18 at 19:41












2 Answers
2






active

oldest

votes


















2














if (Mem[i]=0)
is assigning 0 to each Mem[i].

You are effectively writing 0 to every element of the array.



You need to use the == comparison operator
i.e if (Mem[i] == 0)



Other than that, you have the right idea.






share|improve this answer


























  • thank you, that sorted it!

    – user10158754
    Nov 20 '18 at 19:31













  • Quite nice work for what seems to be your first answer. Check out formatting options for future answers and have fun.

    – Yunnosch
    Nov 20 '18 at 19:37



















0














Try below code:



for(int i=0;i<a;i++){
if (Mem[i]==0){
printf("Error!n");
}
}


The = operator is for assigning a value to a variable.

But == is a comparison operator for logical expressions.






share|improve this answer

























    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%2f53400116%2floop-through-indexes-in-c%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown
























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    if (Mem[i]=0)
    is assigning 0 to each Mem[i].

    You are effectively writing 0 to every element of the array.



    You need to use the == comparison operator
    i.e if (Mem[i] == 0)



    Other than that, you have the right idea.






    share|improve this answer


























    • thank you, that sorted it!

      – user10158754
      Nov 20 '18 at 19:31













    • Quite nice work for what seems to be your first answer. Check out formatting options for future answers and have fun.

      – Yunnosch
      Nov 20 '18 at 19:37
















    2














    if (Mem[i]=0)
    is assigning 0 to each Mem[i].

    You are effectively writing 0 to every element of the array.



    You need to use the == comparison operator
    i.e if (Mem[i] == 0)



    Other than that, you have the right idea.






    share|improve this answer


























    • thank you, that sorted it!

      – user10158754
      Nov 20 '18 at 19:31













    • Quite nice work for what seems to be your first answer. Check out formatting options for future answers and have fun.

      – Yunnosch
      Nov 20 '18 at 19:37














    2












    2








    2







    if (Mem[i]=0)
    is assigning 0 to each Mem[i].

    You are effectively writing 0 to every element of the array.



    You need to use the == comparison operator
    i.e if (Mem[i] == 0)



    Other than that, you have the right idea.






    share|improve this answer















    if (Mem[i]=0)
    is assigning 0 to each Mem[i].

    You are effectively writing 0 to every element of the array.



    You need to use the == comparison operator
    i.e if (Mem[i] == 0)



    Other than that, you have the right idea.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 20 '18 at 19:34









    Yunnosch

    11.3k52033




    11.3k52033










    answered Nov 20 '18 at 19:29









    64bitFrog64bitFrog

    362




    362













    • thank you, that sorted it!

      – user10158754
      Nov 20 '18 at 19:31













    • Quite nice work for what seems to be your first answer. Check out formatting options for future answers and have fun.

      – Yunnosch
      Nov 20 '18 at 19:37



















    • thank you, that sorted it!

      – user10158754
      Nov 20 '18 at 19:31













    • Quite nice work for what seems to be your first answer. Check out formatting options for future answers and have fun.

      – Yunnosch
      Nov 20 '18 at 19:37

















    thank you, that sorted it!

    – user10158754
    Nov 20 '18 at 19:31







    thank you, that sorted it!

    – user10158754
    Nov 20 '18 at 19:31















    Quite nice work for what seems to be your first answer. Check out formatting options for future answers and have fun.

    – Yunnosch
    Nov 20 '18 at 19:37





    Quite nice work for what seems to be your first answer. Check out formatting options for future answers and have fun.

    – Yunnosch
    Nov 20 '18 at 19:37













    0














    Try below code:



    for(int i=0;i<a;i++){
    if (Mem[i]==0){
    printf("Error!n");
    }
    }


    The = operator is for assigning a value to a variable.

    But == is a comparison operator for logical expressions.






    share|improve this answer






























      0














      Try below code:



      for(int i=0;i<a;i++){
      if (Mem[i]==0){
      printf("Error!n");
      }
      }


      The = operator is for assigning a value to a variable.

      But == is a comparison operator for logical expressions.






      share|improve this answer




























        0












        0








        0







        Try below code:



        for(int i=0;i<a;i++){
        if (Mem[i]==0){
        printf("Error!n");
        }
        }


        The = operator is for assigning a value to a variable.

        But == is a comparison operator for logical expressions.






        share|improve this answer















        Try below code:



        for(int i=0;i<a;i++){
        if (Mem[i]==0){
        printf("Error!n");
        }
        }


        The = operator is for assigning a value to a variable.

        But == is a comparison operator for logical expressions.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 '18 at 19:33









        Yunnosch

        11.3k52033




        11.3k52033










        answered Nov 20 '18 at 19:31









        Mohammadreza PanahiMohammadreza Panahi

        2,66521433




        2,66521433






























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400116%2floop-through-indexes-in-c%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

            How to send String Array data to Server using php in android

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Is anime1.com a legal site for watching anime?