Function that returns 1D array and writes the results to a text file









up vote
-4
down vote

favorite












I've been asked to write a code that takes 2, 1D arrays from a text file, add them and then return the results to another text file. Yet, I still get errors about pointers. I'm a C newbie and I'll be very happy if someone could explain it to me.



Here's the code, the result array is global.All the variable names are in Polish, I hope that's not a problem.



int dzialanie(int a, int b)

int *wsk = &c_tab[0];
char wybor;
printf("0.Suman1.Roznica");
scanf("%c", &wybor);
for (i=0; i<9; i++)
if (wybor == '0')

*wsk = a[i] + b[i];
wsk++;

else if (wybor == '1')

*wsk = a[i] - b [i];
wsk++


return *wsk;










share|improve this question























  • A picture of code cannot be compiled. The code in your picture, even when typed in, cannot be build into a binary. Polish should not be a problem, but surprisingly it is in your interest to translate even code identifiers; if they are any good they help understanding. "get errors about pointers" is a very short, not to say useless summary of your problem. Please consider making a Minimal, Complete, and Verifiable example and explaining in more detail what happens and what should happen.
    – Yunnosch
    Nov 11 at 17:36










  • Please consider what kind of help you want. Do you expect an explanation? Then why don't you explain your problem in detail? Do you want code? Then why don't you provide code yourself? A picture of code which does not make a program is not the same thing.
    – Yunnosch
    Nov 11 at 17:37










  • It's my first time using Stack. I'm sorry for all the errors - I was in a hurry. Thank you for help :)
    – Michał.Pelc
    Nov 11 at 18:10














up vote
-4
down vote

favorite












I've been asked to write a code that takes 2, 1D arrays from a text file, add them and then return the results to another text file. Yet, I still get errors about pointers. I'm a C newbie and I'll be very happy if someone could explain it to me.



Here's the code, the result array is global.All the variable names are in Polish, I hope that's not a problem.



int dzialanie(int a, int b)

int *wsk = &c_tab[0];
char wybor;
printf("0.Suman1.Roznica");
scanf("%c", &wybor);
for (i=0; i<9; i++)
if (wybor == '0')

*wsk = a[i] + b[i];
wsk++;

else if (wybor == '1')

*wsk = a[i] - b [i];
wsk++


return *wsk;










share|improve this question























  • A picture of code cannot be compiled. The code in your picture, even when typed in, cannot be build into a binary. Polish should not be a problem, but surprisingly it is in your interest to translate even code identifiers; if they are any good they help understanding. "get errors about pointers" is a very short, not to say useless summary of your problem. Please consider making a Minimal, Complete, and Verifiable example and explaining in more detail what happens and what should happen.
    – Yunnosch
    Nov 11 at 17:36










  • Please consider what kind of help you want. Do you expect an explanation? Then why don't you explain your problem in detail? Do you want code? Then why don't you provide code yourself? A picture of code which does not make a program is not the same thing.
    – Yunnosch
    Nov 11 at 17:37










  • It's my first time using Stack. I'm sorry for all the errors - I was in a hurry. Thank you for help :)
    – Michał.Pelc
    Nov 11 at 18:10












up vote
-4
down vote

favorite









up vote
-4
down vote

favorite











I've been asked to write a code that takes 2, 1D arrays from a text file, add them and then return the results to another text file. Yet, I still get errors about pointers. I'm a C newbie and I'll be very happy if someone could explain it to me.



Here's the code, the result array is global.All the variable names are in Polish, I hope that's not a problem.



int dzialanie(int a, int b)

int *wsk = &c_tab[0];
char wybor;
printf("0.Suman1.Roznica");
scanf("%c", &wybor);
for (i=0; i<9; i++)
if (wybor == '0')

*wsk = a[i] + b[i];
wsk++;

else if (wybor == '1')

*wsk = a[i] - b [i];
wsk++


return *wsk;










share|improve this question















I've been asked to write a code that takes 2, 1D arrays from a text file, add them and then return the results to another text file. Yet, I still get errors about pointers. I'm a C newbie and I'll be very happy if someone could explain it to me.



Here's the code, the result array is global.All the variable names are in Polish, I hope that's not a problem.



int dzialanie(int a, int b)

int *wsk = &c_tab[0];
char wybor;
printf("0.Suman1.Roznica");
scanf("%c", &wybor);
for (i=0; i<9; i++)
if (wybor == '0')

*wsk = a[i] + b[i];
wsk++;

else if (wybor == '1')

*wsk = a[i] - b [i];
wsk++


return *wsk;







c arrays function pointers






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 18:01









harper

10k44185




10k44185










asked Nov 11 at 17:05









Michał.Pelc

33




33











  • A picture of code cannot be compiled. The code in your picture, even when typed in, cannot be build into a binary. Polish should not be a problem, but surprisingly it is in your interest to translate even code identifiers; if they are any good they help understanding. "get errors about pointers" is a very short, not to say useless summary of your problem. Please consider making a Minimal, Complete, and Verifiable example and explaining in more detail what happens and what should happen.
    – Yunnosch
    Nov 11 at 17:36










  • Please consider what kind of help you want. Do you expect an explanation? Then why don't you explain your problem in detail? Do you want code? Then why don't you provide code yourself? A picture of code which does not make a program is not the same thing.
    – Yunnosch
    Nov 11 at 17:37










  • It's my first time using Stack. I'm sorry for all the errors - I was in a hurry. Thank you for help :)
    – Michał.Pelc
    Nov 11 at 18:10
















  • A picture of code cannot be compiled. The code in your picture, even when typed in, cannot be build into a binary. Polish should not be a problem, but surprisingly it is in your interest to translate even code identifiers; if they are any good they help understanding. "get errors about pointers" is a very short, not to say useless summary of your problem. Please consider making a Minimal, Complete, and Verifiable example and explaining in more detail what happens and what should happen.
    – Yunnosch
    Nov 11 at 17:36










  • Please consider what kind of help you want. Do you expect an explanation? Then why don't you explain your problem in detail? Do you want code? Then why don't you provide code yourself? A picture of code which does not make a program is not the same thing.
    – Yunnosch
    Nov 11 at 17:37










  • It's my first time using Stack. I'm sorry for all the errors - I was in a hurry. Thank you for help :)
    – Michał.Pelc
    Nov 11 at 18:10















A picture of code cannot be compiled. The code in your picture, even when typed in, cannot be build into a binary. Polish should not be a problem, but surprisingly it is in your interest to translate even code identifiers; if they are any good they help understanding. "get errors about pointers" is a very short, not to say useless summary of your problem. Please consider making a Minimal, Complete, and Verifiable example and explaining in more detail what happens and what should happen.
– Yunnosch
Nov 11 at 17:36




A picture of code cannot be compiled. The code in your picture, even when typed in, cannot be build into a binary. Polish should not be a problem, but surprisingly it is in your interest to translate even code identifiers; if they are any good they help understanding. "get errors about pointers" is a very short, not to say useless summary of your problem. Please consider making a Minimal, Complete, and Verifiable example and explaining in more detail what happens and what should happen.
– Yunnosch
Nov 11 at 17:36












Please consider what kind of help you want. Do you expect an explanation? Then why don't you explain your problem in detail? Do you want code? Then why don't you provide code yourself? A picture of code which does not make a program is not the same thing.
– Yunnosch
Nov 11 at 17:37




Please consider what kind of help you want. Do you expect an explanation? Then why don't you explain your problem in detail? Do you want code? Then why don't you provide code yourself? A picture of code which does not make a program is not the same thing.
– Yunnosch
Nov 11 at 17:37












It's my first time using Stack. I'm sorry for all the errors - I was in a hurry. Thank you for help :)
– Michał.Pelc
Nov 11 at 18:10




It's my first time using Stack. I'm sorry for all the errors - I was in a hurry. Thank you for help :)
– Michał.Pelc
Nov 11 at 18:10












2 Answers
2






active

oldest

votes

















up vote
0
down vote













The first error I see is that you need to declare the function type with the type of the returned value, in this case the function has to be:



int *dzialanie(int a, int b)
...
return wsk;



Another error is in the for loop, the operation has to be:



...
for (int i=0; i<9; i++)
*(wsk + i) = a[i] + b[i];

...


Same for the another (See that you didn't declare the i which is a variable too). Another way, is doing:



wsk[i] = a[i] - b[i];


Is the same.
If you already know the dimension of the two arrays, maybe is better to take the arguments as pointers, it will look like:



int dzialanie(int *a, int *b)
...



And you will have to allocate memory, this coul be done with malloc:



wsk = (int *)malloc(10*sizeof(int));


At the end of your program you will want to free that allocated memory, doing:



free(wsk);


Hope you understand, my english is poor, I'm from Argentina. Greetings!






share|improve this answer





























    up vote
    0
    down vote













    since you are using a global variable the return type should be void(and or some form of indication that the calculation has been done. which in turn could be a simple return value of -1 or w/e(i'll leave that part up to you).



    int res[10]; // global variable

    void add(int *a, int *b) // no return value, new array will be saved globally(not advisable)

    int sign = -1; // your +- check
    printf("0.Suman1.Roznica");
    scanf("%c", &sign);
    if (sign == 0)

    for (int i = 0; i < 9; i++)
    res[i] = a[i] + b[i];

    else if (sign == 1)

    for (int i = 0; i < 9; i++)
    res[i] = a[i] - b[i];




    an alternative way of doing it would be to use either a or b as "return" values with a simple change like a[i] += b[i] -> now a has the value of a + b. this way you won't have to deal with pointers going out of scope etc(documentation of how to return pointers from functions(in a safe way) is here).






    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',
      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%2f53251112%2ffunction-that-returns-1d-array-and-writes-the-results-to-a-text-file%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








      up vote
      0
      down vote













      The first error I see is that you need to declare the function type with the type of the returned value, in this case the function has to be:



      int *dzialanie(int a, int b)
      ...
      return wsk;



      Another error is in the for loop, the operation has to be:



      ...
      for (int i=0; i<9; i++)
      *(wsk + i) = a[i] + b[i];

      ...


      Same for the another (See that you didn't declare the i which is a variable too). Another way, is doing:



      wsk[i] = a[i] - b[i];


      Is the same.
      If you already know the dimension of the two arrays, maybe is better to take the arguments as pointers, it will look like:



      int dzialanie(int *a, int *b)
      ...



      And you will have to allocate memory, this coul be done with malloc:



      wsk = (int *)malloc(10*sizeof(int));


      At the end of your program you will want to free that allocated memory, doing:



      free(wsk);


      Hope you understand, my english is poor, I'm from Argentina. Greetings!






      share|improve this answer


























        up vote
        0
        down vote













        The first error I see is that you need to declare the function type with the type of the returned value, in this case the function has to be:



        int *dzialanie(int a, int b)
        ...
        return wsk;



        Another error is in the for loop, the operation has to be:



        ...
        for (int i=0; i<9; i++)
        *(wsk + i) = a[i] + b[i];

        ...


        Same for the another (See that you didn't declare the i which is a variable too). Another way, is doing:



        wsk[i] = a[i] - b[i];


        Is the same.
        If you already know the dimension of the two arrays, maybe is better to take the arguments as pointers, it will look like:



        int dzialanie(int *a, int *b)
        ...



        And you will have to allocate memory, this coul be done with malloc:



        wsk = (int *)malloc(10*sizeof(int));


        At the end of your program you will want to free that allocated memory, doing:



        free(wsk);


        Hope you understand, my english is poor, I'm from Argentina. Greetings!






        share|improve this answer
























          up vote
          0
          down vote










          up vote
          0
          down vote









          The first error I see is that you need to declare the function type with the type of the returned value, in this case the function has to be:



          int *dzialanie(int a, int b)
          ...
          return wsk;



          Another error is in the for loop, the operation has to be:



          ...
          for (int i=0; i<9; i++)
          *(wsk + i) = a[i] + b[i];

          ...


          Same for the another (See that you didn't declare the i which is a variable too). Another way, is doing:



          wsk[i] = a[i] - b[i];


          Is the same.
          If you already know the dimension of the two arrays, maybe is better to take the arguments as pointers, it will look like:



          int dzialanie(int *a, int *b)
          ...



          And you will have to allocate memory, this coul be done with malloc:



          wsk = (int *)malloc(10*sizeof(int));


          At the end of your program you will want to free that allocated memory, doing:



          free(wsk);


          Hope you understand, my english is poor, I'm from Argentina. Greetings!






          share|improve this answer














          The first error I see is that you need to declare the function type with the type of the returned value, in this case the function has to be:



          int *dzialanie(int a, int b)
          ...
          return wsk;



          Another error is in the for loop, the operation has to be:



          ...
          for (int i=0; i<9; i++)
          *(wsk + i) = a[i] + b[i];

          ...


          Same for the another (See that you didn't declare the i which is a variable too). Another way, is doing:



          wsk[i] = a[i] - b[i];


          Is the same.
          If you already know the dimension of the two arrays, maybe is better to take the arguments as pointers, it will look like:



          int dzialanie(int *a, int *b)
          ...



          And you will have to allocate memory, this coul be done with malloc:



          wsk = (int *)malloc(10*sizeof(int));


          At the end of your program you will want to free that allocated memory, doing:



          free(wsk);


          Hope you understand, my english is poor, I'm from Argentina. Greetings!







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 18:10

























          answered Nov 11 at 17:44









          Gonzalo Fernández

          13




          13






















              up vote
              0
              down vote













              since you are using a global variable the return type should be void(and or some form of indication that the calculation has been done. which in turn could be a simple return value of -1 or w/e(i'll leave that part up to you).



              int res[10]; // global variable

              void add(int *a, int *b) // no return value, new array will be saved globally(not advisable)

              int sign = -1; // your +- check
              printf("0.Suman1.Roznica");
              scanf("%c", &sign);
              if (sign == 0)

              for (int i = 0; i < 9; i++)
              res[i] = a[i] + b[i];

              else if (sign == 1)

              for (int i = 0; i < 9; i++)
              res[i] = a[i] - b[i];




              an alternative way of doing it would be to use either a or b as "return" values with a simple change like a[i] += b[i] -> now a has the value of a + b. this way you won't have to deal with pointers going out of scope etc(documentation of how to return pointers from functions(in a safe way) is here).






              share|improve this answer


























                up vote
                0
                down vote













                since you are using a global variable the return type should be void(and or some form of indication that the calculation has been done. which in turn could be a simple return value of -1 or w/e(i'll leave that part up to you).



                int res[10]; // global variable

                void add(int *a, int *b) // no return value, new array will be saved globally(not advisable)

                int sign = -1; // your +- check
                printf("0.Suman1.Roznica");
                scanf("%c", &sign);
                if (sign == 0)

                for (int i = 0; i < 9; i++)
                res[i] = a[i] + b[i];

                else if (sign == 1)

                for (int i = 0; i < 9; i++)
                res[i] = a[i] - b[i];




                an alternative way of doing it would be to use either a or b as "return" values with a simple change like a[i] += b[i] -> now a has the value of a + b. this way you won't have to deal with pointers going out of scope etc(documentation of how to return pointers from functions(in a safe way) is here).






                share|improve this answer
























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  since you are using a global variable the return type should be void(and or some form of indication that the calculation has been done. which in turn could be a simple return value of -1 or w/e(i'll leave that part up to you).



                  int res[10]; // global variable

                  void add(int *a, int *b) // no return value, new array will be saved globally(not advisable)

                  int sign = -1; // your +- check
                  printf("0.Suman1.Roznica");
                  scanf("%c", &sign);
                  if (sign == 0)

                  for (int i = 0; i < 9; i++)
                  res[i] = a[i] + b[i];

                  else if (sign == 1)

                  for (int i = 0; i < 9; i++)
                  res[i] = a[i] - b[i];




                  an alternative way of doing it would be to use either a or b as "return" values with a simple change like a[i] += b[i] -> now a has the value of a + b. this way you won't have to deal with pointers going out of scope etc(documentation of how to return pointers from functions(in a safe way) is here).






                  share|improve this answer














                  since you are using a global variable the return type should be void(and or some form of indication that the calculation has been done. which in turn could be a simple return value of -1 or w/e(i'll leave that part up to you).



                  int res[10]; // global variable

                  void add(int *a, int *b) // no return value, new array will be saved globally(not advisable)

                  int sign = -1; // your +- check
                  printf("0.Suman1.Roznica");
                  scanf("%c", &sign);
                  if (sign == 0)

                  for (int i = 0; i < 9; i++)
                  res[i] = a[i] + b[i];

                  else if (sign == 1)

                  for (int i = 0; i < 9; i++)
                  res[i] = a[i] - b[i];




                  an alternative way of doing it would be to use either a or b as "return" values with a simple change like a[i] += b[i] -> now a has the value of a + b. this way you won't have to deal with pointers going out of scope etc(documentation of how to return pointers from functions(in a safe way) is here).







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 11 at 19:16

























                  answered Nov 11 at 18:52









                  ats

                  694




                  694



























                      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%2f53251112%2ffunction-that-returns-1d-array-and-writes-the-results-to-a-text-file%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







                      這個網誌中的熱門文章

                      What does pagestruct do in Eviews?

                      Dutch intervention in Lombok and Karangasem

                      Channel Islands