Segmentation fault 11 using arrays












-1















I am a beginner with coding and I got my code to compile but when I input integers, I get segmentation fault. Please help.



The problem is: I need to first get how many students there are then the next inputs will be the marks of those students. So if inputed 3, the next inputs should be 3 marks of the 3 students. Then the input should be either g(girl) or b(boy). From there, if boy, I need to sum up all the odd marks.



THIS IS MY CODE:



#include<stdio.h>
#include<stdlib.h>

int marks_summation(int* marks, int number_of_students, char gender){

int i=0, sum=0;
int marksforGirls=0, marksforBoys=0;
char g;

for(marks = 0; *marks <= number_of_students; marks++){
if(gender == g){
do{
if(marks[i]%2 == 0){
marksforGirls = marks[i];
i++;
sum += marksforGirls;
}
} while(*marks<=number_of_students);
}
else{
do{
if(marks[i]%2 != 0){
marksforBoys = marks[i];
i++;
sum += marksforBoys;
}
} while (*marks<=number_of_students);
}
}
return 0;
}


int main(){

int i=0, number_of_students=0;
int *marks=0;
int sum=0;
char gender;

scanf("%d",&number_of_students);

marks = (int*)malloc(number_of_students * sizeof(int));

for(i=0; i<number_of_students; i++){
scanf(" %d", &marks[i]);//for every marks put in, it will go into marks
}

scanf("%c",&gender);

marks_summation(marks, number_of_students, gender);

printf("%d", sum);

free(marks);

return 0;
}









share|improve this question




















  • 1





    for(marks=0; *marks <= … directly crashes. You probably meant for(*marks=0; ...!?

    – Werner Henze
    Nov 21 '18 at 22:49








  • 2





    Fyi, turn up your warnings. Look at marks_summation, specifically at if(gender == g) and ask yourself, "What is the value of g when this is checked?" if the answer is anything other than "I don't know", it's wrong. Evaluation of indeterminate variables invokes undefined behavior. From what I see, g is never set, and never even used in that function other than the improper usage I already mentioned, so consider it fair game for removal. That function also accumulates sum, and does absolutely nothing with it.

    – WhozCraig
    Nov 21 '18 at 22:49













  • @WhozCraig maybe it was meant be 'g', ignoring the declaration...

    – sstefan
    Nov 21 '18 at 22:52











  • You have an unnecessary space in front of %d but lack the necessary space in front of %c. You must also check the function return value of scanf.

    – Weather Vane
    Nov 21 '18 at 22:53








  • 1





    @sstefan i seriously doubt it, but i give us a one-in-five chance the OP actually clarifies the matter further. If anything, it would be checked for f and/or m . (at least in the world I live in). If ever there was a need for a conversation with ones rubber duck that function is it.

    – WhozCraig
    Nov 21 '18 at 22:54


















-1















I am a beginner with coding and I got my code to compile but when I input integers, I get segmentation fault. Please help.



The problem is: I need to first get how many students there are then the next inputs will be the marks of those students. So if inputed 3, the next inputs should be 3 marks of the 3 students. Then the input should be either g(girl) or b(boy). From there, if boy, I need to sum up all the odd marks.



THIS IS MY CODE:



#include<stdio.h>
#include<stdlib.h>

int marks_summation(int* marks, int number_of_students, char gender){

int i=0, sum=0;
int marksforGirls=0, marksforBoys=0;
char g;

for(marks = 0; *marks <= number_of_students; marks++){
if(gender == g){
do{
if(marks[i]%2 == 0){
marksforGirls = marks[i];
i++;
sum += marksforGirls;
}
} while(*marks<=number_of_students);
}
else{
do{
if(marks[i]%2 != 0){
marksforBoys = marks[i];
i++;
sum += marksforBoys;
}
} while (*marks<=number_of_students);
}
}
return 0;
}


int main(){

int i=0, number_of_students=0;
int *marks=0;
int sum=0;
char gender;

scanf("%d",&number_of_students);

marks = (int*)malloc(number_of_students * sizeof(int));

for(i=0; i<number_of_students; i++){
scanf(" %d", &marks[i]);//for every marks put in, it will go into marks
}

scanf("%c",&gender);

marks_summation(marks, number_of_students, gender);

printf("%d", sum);

free(marks);

return 0;
}









share|improve this question




















  • 1





    for(marks=0; *marks <= … directly crashes. You probably meant for(*marks=0; ...!?

    – Werner Henze
    Nov 21 '18 at 22:49








  • 2





    Fyi, turn up your warnings. Look at marks_summation, specifically at if(gender == g) and ask yourself, "What is the value of g when this is checked?" if the answer is anything other than "I don't know", it's wrong. Evaluation of indeterminate variables invokes undefined behavior. From what I see, g is never set, and never even used in that function other than the improper usage I already mentioned, so consider it fair game for removal. That function also accumulates sum, and does absolutely nothing with it.

    – WhozCraig
    Nov 21 '18 at 22:49













  • @WhozCraig maybe it was meant be 'g', ignoring the declaration...

    – sstefan
    Nov 21 '18 at 22:52











  • You have an unnecessary space in front of %d but lack the necessary space in front of %c. You must also check the function return value of scanf.

    – Weather Vane
    Nov 21 '18 at 22:53








  • 1





    @sstefan i seriously doubt it, but i give us a one-in-five chance the OP actually clarifies the matter further. If anything, it would be checked for f and/or m . (at least in the world I live in). If ever there was a need for a conversation with ones rubber duck that function is it.

    – WhozCraig
    Nov 21 '18 at 22:54
















-1












-1








-1








I am a beginner with coding and I got my code to compile but when I input integers, I get segmentation fault. Please help.



The problem is: I need to first get how many students there are then the next inputs will be the marks of those students. So if inputed 3, the next inputs should be 3 marks of the 3 students. Then the input should be either g(girl) or b(boy). From there, if boy, I need to sum up all the odd marks.



THIS IS MY CODE:



#include<stdio.h>
#include<stdlib.h>

int marks_summation(int* marks, int number_of_students, char gender){

int i=0, sum=0;
int marksforGirls=0, marksforBoys=0;
char g;

for(marks = 0; *marks <= number_of_students; marks++){
if(gender == g){
do{
if(marks[i]%2 == 0){
marksforGirls = marks[i];
i++;
sum += marksforGirls;
}
} while(*marks<=number_of_students);
}
else{
do{
if(marks[i]%2 != 0){
marksforBoys = marks[i];
i++;
sum += marksforBoys;
}
} while (*marks<=number_of_students);
}
}
return 0;
}


int main(){

int i=0, number_of_students=0;
int *marks=0;
int sum=0;
char gender;

scanf("%d",&number_of_students);

marks = (int*)malloc(number_of_students * sizeof(int));

for(i=0; i<number_of_students; i++){
scanf(" %d", &marks[i]);//for every marks put in, it will go into marks
}

scanf("%c",&gender);

marks_summation(marks, number_of_students, gender);

printf("%d", sum);

free(marks);

return 0;
}









share|improve this question
















I am a beginner with coding and I got my code to compile but when I input integers, I get segmentation fault. Please help.



The problem is: I need to first get how many students there are then the next inputs will be the marks of those students. So if inputed 3, the next inputs should be 3 marks of the 3 students. Then the input should be either g(girl) or b(boy). From there, if boy, I need to sum up all the odd marks.



THIS IS MY CODE:



#include<stdio.h>
#include<stdlib.h>

int marks_summation(int* marks, int number_of_students, char gender){

int i=0, sum=0;
int marksforGirls=0, marksforBoys=0;
char g;

for(marks = 0; *marks <= number_of_students; marks++){
if(gender == g){
do{
if(marks[i]%2 == 0){
marksforGirls = marks[i];
i++;
sum += marksforGirls;
}
} while(*marks<=number_of_students);
}
else{
do{
if(marks[i]%2 != 0){
marksforBoys = marks[i];
i++;
sum += marksforBoys;
}
} while (*marks<=number_of_students);
}
}
return 0;
}


int main(){

int i=0, number_of_students=0;
int *marks=0;
int sum=0;
char gender;

scanf("%d",&number_of_students);

marks = (int*)malloc(number_of_students * sizeof(int));

for(i=0; i<number_of_students; i++){
scanf(" %d", &marks[i]);//for every marks put in, it will go into marks
}

scanf("%c",&gender);

marks_summation(marks, number_of_students, gender);

printf("%d", sum);

free(marks);

return 0;
}






c arrays malloc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 22:47









WhozCraig

51.7k958108




51.7k958108










asked Nov 21 '18 at 22:45









J.LeeJ.Lee

1




1








  • 1





    for(marks=0; *marks <= … directly crashes. You probably meant for(*marks=0; ...!?

    – Werner Henze
    Nov 21 '18 at 22:49








  • 2





    Fyi, turn up your warnings. Look at marks_summation, specifically at if(gender == g) and ask yourself, "What is the value of g when this is checked?" if the answer is anything other than "I don't know", it's wrong. Evaluation of indeterminate variables invokes undefined behavior. From what I see, g is never set, and never even used in that function other than the improper usage I already mentioned, so consider it fair game for removal. That function also accumulates sum, and does absolutely nothing with it.

    – WhozCraig
    Nov 21 '18 at 22:49













  • @WhozCraig maybe it was meant be 'g', ignoring the declaration...

    – sstefan
    Nov 21 '18 at 22:52











  • You have an unnecessary space in front of %d but lack the necessary space in front of %c. You must also check the function return value of scanf.

    – Weather Vane
    Nov 21 '18 at 22:53








  • 1





    @sstefan i seriously doubt it, but i give us a one-in-five chance the OP actually clarifies the matter further. If anything, it would be checked for f and/or m . (at least in the world I live in). If ever there was a need for a conversation with ones rubber duck that function is it.

    – WhozCraig
    Nov 21 '18 at 22:54
















  • 1





    for(marks=0; *marks <= … directly crashes. You probably meant for(*marks=0; ...!?

    – Werner Henze
    Nov 21 '18 at 22:49








  • 2





    Fyi, turn up your warnings. Look at marks_summation, specifically at if(gender == g) and ask yourself, "What is the value of g when this is checked?" if the answer is anything other than "I don't know", it's wrong. Evaluation of indeterminate variables invokes undefined behavior. From what I see, g is never set, and never even used in that function other than the improper usage I already mentioned, so consider it fair game for removal. That function also accumulates sum, and does absolutely nothing with it.

    – WhozCraig
    Nov 21 '18 at 22:49













  • @WhozCraig maybe it was meant be 'g', ignoring the declaration...

    – sstefan
    Nov 21 '18 at 22:52











  • You have an unnecessary space in front of %d but lack the necessary space in front of %c. You must also check the function return value of scanf.

    – Weather Vane
    Nov 21 '18 at 22:53








  • 1





    @sstefan i seriously doubt it, but i give us a one-in-five chance the OP actually clarifies the matter further. If anything, it would be checked for f and/or m . (at least in the world I live in). If ever there was a need for a conversation with ones rubber duck that function is it.

    – WhozCraig
    Nov 21 '18 at 22:54










1




1





for(marks=0; *marks <= … directly crashes. You probably meant for(*marks=0; ...!?

– Werner Henze
Nov 21 '18 at 22:49







for(marks=0; *marks <= … directly crashes. You probably meant for(*marks=0; ...!?

– Werner Henze
Nov 21 '18 at 22:49






2




2





Fyi, turn up your warnings. Look at marks_summation, specifically at if(gender == g) and ask yourself, "What is the value of g when this is checked?" if the answer is anything other than "I don't know", it's wrong. Evaluation of indeterminate variables invokes undefined behavior. From what I see, g is never set, and never even used in that function other than the improper usage I already mentioned, so consider it fair game for removal. That function also accumulates sum, and does absolutely nothing with it.

– WhozCraig
Nov 21 '18 at 22:49







Fyi, turn up your warnings. Look at marks_summation, specifically at if(gender == g) and ask yourself, "What is the value of g when this is checked?" if the answer is anything other than "I don't know", it's wrong. Evaluation of indeterminate variables invokes undefined behavior. From what I see, g is never set, and never even used in that function other than the improper usage I already mentioned, so consider it fair game for removal. That function also accumulates sum, and does absolutely nothing with it.

– WhozCraig
Nov 21 '18 at 22:49















@WhozCraig maybe it was meant be 'g', ignoring the declaration...

– sstefan
Nov 21 '18 at 22:52





@WhozCraig maybe it was meant be 'g', ignoring the declaration...

– sstefan
Nov 21 '18 at 22:52













You have an unnecessary space in front of %d but lack the necessary space in front of %c. You must also check the function return value of scanf.

– Weather Vane
Nov 21 '18 at 22:53







You have an unnecessary space in front of %d but lack the necessary space in front of %c. You must also check the function return value of scanf.

– Weather Vane
Nov 21 '18 at 22:53






1




1





@sstefan i seriously doubt it, but i give us a one-in-five chance the OP actually clarifies the matter further. If anything, it would be checked for f and/or m . (at least in the world I live in). If ever there was a need for a conversation with ones rubber duck that function is it.

– WhozCraig
Nov 21 '18 at 22:54







@sstefan i seriously doubt it, but i give us a one-in-five chance the OP actually clarifies the matter further. If anything, it would be checked for f and/or m . (at least in the world I live in). If ever there was a need for a conversation with ones rubber duck that function is it.

– WhozCraig
Nov 21 '18 at 22:54














2 Answers
2






active

oldest

votes


















0














The following line is not doing what you might think it does:



for(marks = 0; *marks <= number_of_students; marks++)


I believe you'd want to use an index to access each of marks elements.






share|improve this answer































    0














    As others have pointed out, the problem is in the for loop. Besides pointers being wrong, your loop condition is also wrong. Furthermore your function doesn't return anything. Also, both of your do-while loops are infinite loops.



    Try this:



    int mark_sum(int *marks, int number_of_students, char gender){
    int sum = 0;
    for(int i = 0; i < number_of_students; ++i){
    if(gender = 'g'){
    if(marks[i] % 2 == 0)
    sum += marks[i];
    } else {
    if(marks[i] % 2 != 0)
    sum += marks[i];
    }
    }
    return sum;
    }


    Also, in main you need to actually store the value returned by the function:



    int sum = mark_sum(marks, number_of_students, gender);





    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%2f53421482%2fsegmentation-fault-11-using-arrays%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









      0














      The following line is not doing what you might think it does:



      for(marks = 0; *marks <= number_of_students; marks++)


      I believe you'd want to use an index to access each of marks elements.






      share|improve this answer




























        0














        The following line is not doing what you might think it does:



        for(marks = 0; *marks <= number_of_students; marks++)


        I believe you'd want to use an index to access each of marks elements.






        share|improve this answer


























          0












          0








          0







          The following line is not doing what you might think it does:



          for(marks = 0; *marks <= number_of_students; marks++)


          I believe you'd want to use an index to access each of marks elements.






          share|improve this answer













          The following line is not doing what you might think it does:



          for(marks = 0; *marks <= number_of_students; marks++)


          I believe you'd want to use an index to access each of marks elements.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 22:54









          Bruno KimBruno Kim

          1,21311125




          1,21311125

























              0














              As others have pointed out, the problem is in the for loop. Besides pointers being wrong, your loop condition is also wrong. Furthermore your function doesn't return anything. Also, both of your do-while loops are infinite loops.



              Try this:



              int mark_sum(int *marks, int number_of_students, char gender){
              int sum = 0;
              for(int i = 0; i < number_of_students; ++i){
              if(gender = 'g'){
              if(marks[i] % 2 == 0)
              sum += marks[i];
              } else {
              if(marks[i] % 2 != 0)
              sum += marks[i];
              }
              }
              return sum;
              }


              Also, in main you need to actually store the value returned by the function:



              int sum = mark_sum(marks, number_of_students, gender);





              share|improve this answer




























                0














                As others have pointed out, the problem is in the for loop. Besides pointers being wrong, your loop condition is also wrong. Furthermore your function doesn't return anything. Also, both of your do-while loops are infinite loops.



                Try this:



                int mark_sum(int *marks, int number_of_students, char gender){
                int sum = 0;
                for(int i = 0; i < number_of_students; ++i){
                if(gender = 'g'){
                if(marks[i] % 2 == 0)
                sum += marks[i];
                } else {
                if(marks[i] % 2 != 0)
                sum += marks[i];
                }
                }
                return sum;
                }


                Also, in main you need to actually store the value returned by the function:



                int sum = mark_sum(marks, number_of_students, gender);





                share|improve this answer


























                  0












                  0








                  0







                  As others have pointed out, the problem is in the for loop. Besides pointers being wrong, your loop condition is also wrong. Furthermore your function doesn't return anything. Also, both of your do-while loops are infinite loops.



                  Try this:



                  int mark_sum(int *marks, int number_of_students, char gender){
                  int sum = 0;
                  for(int i = 0; i < number_of_students; ++i){
                  if(gender = 'g'){
                  if(marks[i] % 2 == 0)
                  sum += marks[i];
                  } else {
                  if(marks[i] % 2 != 0)
                  sum += marks[i];
                  }
                  }
                  return sum;
                  }


                  Also, in main you need to actually store the value returned by the function:



                  int sum = mark_sum(marks, number_of_students, gender);





                  share|improve this answer













                  As others have pointed out, the problem is in the for loop. Besides pointers being wrong, your loop condition is also wrong. Furthermore your function doesn't return anything. Also, both of your do-while loops are infinite loops.



                  Try this:



                  int mark_sum(int *marks, int number_of_students, char gender){
                  int sum = 0;
                  for(int i = 0; i < number_of_students; ++i){
                  if(gender = 'g'){
                  if(marks[i] % 2 == 0)
                  sum += marks[i];
                  } else {
                  if(marks[i] % 2 != 0)
                  sum += marks[i];
                  }
                  }
                  return sum;
                  }


                  Also, in main you need to actually store the value returned by the function:



                  int sum = mark_sum(marks, number_of_students, gender);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 23:23









                  sstefansstefan

                  336311




                  336311






























                      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%2f53421482%2fsegmentation-fault-11-using-arrays%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 change which sound is reproduced for terminal bell?

                      Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                      Can I use Tabulator js library in my java Spring + Thymeleaf project?