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;
c arrays function pointers
add a comment |
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;
c arrays function pointers
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
add a comment |
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;
c arrays function pointers
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
c arrays function pointers
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
add a comment |
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
add a comment |
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!
add a comment |
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).
add a comment |
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!
add a comment |
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!
add a comment |
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!
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!
edited Nov 11 at 18:10
answered Nov 11 at 17:44
Gonzalo Fernández
13
13
add a comment |
add a comment |
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).
add a comment |
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).
add a comment |
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).
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).
edited Nov 11 at 19:16
answered Nov 11 at 18:52
ats
694
694
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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