Return a string from if statement in C
I was trying to let a function to return the correct char value from an array by an if statement but kept getting the following errorreturning ‘char *’ from a function with return type ‘char’ makes integer from pointer without a cast
and here is the function
...
char GradeFromPercentage(float x)
{
char y[5][1];
y[1] == "A";
y[2] == "B";
y[3] == "C";
y[4] == "D";
y[4] == "F";
if (x >= 90)
{
return y[0];
}
else if (x >= 70)
{
return y[1];
}
else if (x >= 50)
{
return y[3];
}
else if (x >= 30)
{
return y[4];
}
else if (x < 30)
{
return y[5];
}
return 0;
}
...
c function if-statement char
add a comment |
I was trying to let a function to return the correct char value from an array by an if statement but kept getting the following errorreturning ‘char *’ from a function with return type ‘char’ makes integer from pointer without a cast
and here is the function
...
char GradeFromPercentage(float x)
{
char y[5][1];
y[1] == "A";
y[2] == "B";
y[3] == "C";
y[4] == "D";
y[4] == "F";
if (x >= 90)
{
return y[0];
}
else if (x >= 70)
{
return y[1];
}
else if (x >= 50)
{
return y[3];
}
else if (x >= 30)
{
return y[4];
}
else if (x < 30)
{
return y[5];
}
return 0;
}
...
c function if-statement char
add a comment |
I was trying to let a function to return the correct char value from an array by an if statement but kept getting the following errorreturning ‘char *’ from a function with return type ‘char’ makes integer from pointer without a cast
and here is the function
...
char GradeFromPercentage(float x)
{
char y[5][1];
y[1] == "A";
y[2] == "B";
y[3] == "C";
y[4] == "D";
y[4] == "F";
if (x >= 90)
{
return y[0];
}
else if (x >= 70)
{
return y[1];
}
else if (x >= 50)
{
return y[3];
}
else if (x >= 30)
{
return y[4];
}
else if (x < 30)
{
return y[5];
}
return 0;
}
...
c function if-statement char
I was trying to let a function to return the correct char value from an array by an if statement but kept getting the following errorreturning ‘char *’ from a function with return type ‘char’ makes integer from pointer without a cast
and here is the function
...
char GradeFromPercentage(float x)
{
char y[5][1];
y[1] == "A";
y[2] == "B";
y[3] == "C";
y[4] == "D";
y[4] == "F";
if (x >= 90)
{
return y[0];
}
else if (x >= 70)
{
return y[1];
}
else if (x >= 50)
{
return y[3];
}
else if (x >= 30)
{
return y[4];
}
else if (x < 30)
{
return y[5];
}
return 0;
}
...
c function if-statement char
c function if-statement char
asked Nov 23 '18 at 1:26
Leo Wang
61
61
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
Whoah, there are errors everywhere here!!
If you want to store an array of single chars, just define it as:
char y[5];
Now, the next bit has a few issues:
y[1] == "A";
y[2] == "B";
y[3] == "C";
y[4] == "D";
y[4] == "F";
Apart from the obvious where you're using the comparison operator (==
) instead of assignment (=
), you were also trying to assign a string literal to a char
array. You cannot do this outside of variable's definition. But I assert that you don't want to use strings. You also missed y[0]
and doubled-up on y[4]
.
Since I've already changed your array definition, we need character literals (single-quotes). Let's fix all of this:
y[0] == 'A';
y[1] == 'B';
y[2] == 'C';
y[3] == 'D';
y[4] == 'F';
But that's so much typing... Why not define it in one line, both saving your fingers and making your code more compact (and actually more readable):
char y[5] = { 'A', 'B', 'C', 'D', 'F' };
The next issue is you're returning y[5]
for the 'F' score, which is not okay -- that is accessing outside your array: remember indexing is zero-based so the valid indices range from 0 to 4 inclusive. You need to check all your indices, because a few of them are wrong.
My final point is going to be a style-based thing, and you can take it or leave it. Instead of having this big if statement, why not put all your cutoff scores into an array... Rolling this into everything else I've mentioned so far, you'd end up with something like this:
char GradeFromPercentage(float score)
{
const char grades[5] = { 'A', 'B', 'C', 'D', 'F' };
const float scores[5] = { 90.f, 70.f, 50.f, 30.f, -FLT_MAX };
for (int i = 0; i < 5; i++)
{
if (score >= scores[i]) return grades[i];
}
// It should not ordinarily be possible to reach here, but you should
// return something anyway. It's possible to get here if you supply
// a score of `NaN`. You may choose to return an invalid grade, or
// 'F' or whatever.
return '';
}
add a comment |
You get the error message because you try to return y[N]
from the function. When y
is defined as
char y[5][1]; // array of 5 arrays of 1 chars
then y[N]
is an array of 1 char which will decay to a pointer to char.
Also, you used a comparison operator ==
where you clearly intended assignment =
.
add a comment |
You're only getting the array from your 2D array! You have to do something like this:
if (...) {
return y[0][1];
} else if (...) {
return y[1][1];
}
...
Your y
array is a 2D array. 2D arrays are arrays that hold arrays. y[5][1]
will define a character array of length 5
with each cell in the array holding another character array of length 1
.
Since you are referencing elements in y
at the first level only (e.g. y[0]
), you will be getting the character array instead of just a character. To get the characters inside y
, you will need to reference twice: one to get the array holding the character and another to get the character inside the array.
Alternatively, you can just use a 1D array which you can define with
char y[5];
y[0] = 'A';
...
or, as achicn3 suggested, with
char y[5] = {'A', ..., 'F'};
add a comment |
edit your "==" to "="
like this
y[1] = "A";
y[2] = "B";
y[3] = "C";
y[4] = "D";
y[5] = "F";
and you just want return A,B,C,D,F
you can use 1-d array
like this
char y[5] ={'A','B','C','D','F'};
and the easiest way is
retun 'A'; return 'B'; ...
add a comment |
If you still getting error then problem is with function return type.
Firs you declare function with char return type and returning char*.
So you have to replace char * instead of char as a function return type.
If you don't know array is same like pointer Go to below link and read.
http://www.cplusplus.com/doc/tutorial/pointers/
add a comment |
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
});
}
});
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%2f53439666%2freturn-a-string-from-if-statement-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
Whoah, there are errors everywhere here!!
If you want to store an array of single chars, just define it as:
char y[5];
Now, the next bit has a few issues:
y[1] == "A";
y[2] == "B";
y[3] == "C";
y[4] == "D";
y[4] == "F";
Apart from the obvious where you're using the comparison operator (==
) instead of assignment (=
), you were also trying to assign a string literal to a char
array. You cannot do this outside of variable's definition. But I assert that you don't want to use strings. You also missed y[0]
and doubled-up on y[4]
.
Since I've already changed your array definition, we need character literals (single-quotes). Let's fix all of this:
y[0] == 'A';
y[1] == 'B';
y[2] == 'C';
y[3] == 'D';
y[4] == 'F';
But that's so much typing... Why not define it in one line, both saving your fingers and making your code more compact (and actually more readable):
char y[5] = { 'A', 'B', 'C', 'D', 'F' };
The next issue is you're returning y[5]
for the 'F' score, which is not okay -- that is accessing outside your array: remember indexing is zero-based so the valid indices range from 0 to 4 inclusive. You need to check all your indices, because a few of them are wrong.
My final point is going to be a style-based thing, and you can take it or leave it. Instead of having this big if statement, why not put all your cutoff scores into an array... Rolling this into everything else I've mentioned so far, you'd end up with something like this:
char GradeFromPercentage(float score)
{
const char grades[5] = { 'A', 'B', 'C', 'D', 'F' };
const float scores[5] = { 90.f, 70.f, 50.f, 30.f, -FLT_MAX };
for (int i = 0; i < 5; i++)
{
if (score >= scores[i]) return grades[i];
}
// It should not ordinarily be possible to reach here, but you should
// return something anyway. It's possible to get here if you supply
// a score of `NaN`. You may choose to return an invalid grade, or
// 'F' or whatever.
return '';
}
add a comment |
Whoah, there are errors everywhere here!!
If you want to store an array of single chars, just define it as:
char y[5];
Now, the next bit has a few issues:
y[1] == "A";
y[2] == "B";
y[3] == "C";
y[4] == "D";
y[4] == "F";
Apart from the obvious where you're using the comparison operator (==
) instead of assignment (=
), you were also trying to assign a string literal to a char
array. You cannot do this outside of variable's definition. But I assert that you don't want to use strings. You also missed y[0]
and doubled-up on y[4]
.
Since I've already changed your array definition, we need character literals (single-quotes). Let's fix all of this:
y[0] == 'A';
y[1] == 'B';
y[2] == 'C';
y[3] == 'D';
y[4] == 'F';
But that's so much typing... Why not define it in one line, both saving your fingers and making your code more compact (and actually more readable):
char y[5] = { 'A', 'B', 'C', 'D', 'F' };
The next issue is you're returning y[5]
for the 'F' score, which is not okay -- that is accessing outside your array: remember indexing is zero-based so the valid indices range from 0 to 4 inclusive. You need to check all your indices, because a few of them are wrong.
My final point is going to be a style-based thing, and you can take it or leave it. Instead of having this big if statement, why not put all your cutoff scores into an array... Rolling this into everything else I've mentioned so far, you'd end up with something like this:
char GradeFromPercentage(float score)
{
const char grades[5] = { 'A', 'B', 'C', 'D', 'F' };
const float scores[5] = { 90.f, 70.f, 50.f, 30.f, -FLT_MAX };
for (int i = 0; i < 5; i++)
{
if (score >= scores[i]) return grades[i];
}
// It should not ordinarily be possible to reach here, but you should
// return something anyway. It's possible to get here if you supply
// a score of `NaN`. You may choose to return an invalid grade, or
// 'F' or whatever.
return '';
}
add a comment |
Whoah, there are errors everywhere here!!
If you want to store an array of single chars, just define it as:
char y[5];
Now, the next bit has a few issues:
y[1] == "A";
y[2] == "B";
y[3] == "C";
y[4] == "D";
y[4] == "F";
Apart from the obvious where you're using the comparison operator (==
) instead of assignment (=
), you were also trying to assign a string literal to a char
array. You cannot do this outside of variable's definition. But I assert that you don't want to use strings. You also missed y[0]
and doubled-up on y[4]
.
Since I've already changed your array definition, we need character literals (single-quotes). Let's fix all of this:
y[0] == 'A';
y[1] == 'B';
y[2] == 'C';
y[3] == 'D';
y[4] == 'F';
But that's so much typing... Why not define it in one line, both saving your fingers and making your code more compact (and actually more readable):
char y[5] = { 'A', 'B', 'C', 'D', 'F' };
The next issue is you're returning y[5]
for the 'F' score, which is not okay -- that is accessing outside your array: remember indexing is zero-based so the valid indices range from 0 to 4 inclusive. You need to check all your indices, because a few of them are wrong.
My final point is going to be a style-based thing, and you can take it or leave it. Instead of having this big if statement, why not put all your cutoff scores into an array... Rolling this into everything else I've mentioned so far, you'd end up with something like this:
char GradeFromPercentage(float score)
{
const char grades[5] = { 'A', 'B', 'C', 'D', 'F' };
const float scores[5] = { 90.f, 70.f, 50.f, 30.f, -FLT_MAX };
for (int i = 0; i < 5; i++)
{
if (score >= scores[i]) return grades[i];
}
// It should not ordinarily be possible to reach here, but you should
// return something anyway. It's possible to get here if you supply
// a score of `NaN`. You may choose to return an invalid grade, or
// 'F' or whatever.
return '';
}
Whoah, there are errors everywhere here!!
If you want to store an array of single chars, just define it as:
char y[5];
Now, the next bit has a few issues:
y[1] == "A";
y[2] == "B";
y[3] == "C";
y[4] == "D";
y[4] == "F";
Apart from the obvious where you're using the comparison operator (==
) instead of assignment (=
), you were also trying to assign a string literal to a char
array. You cannot do this outside of variable's definition. But I assert that you don't want to use strings. You also missed y[0]
and doubled-up on y[4]
.
Since I've already changed your array definition, we need character literals (single-quotes). Let's fix all of this:
y[0] == 'A';
y[1] == 'B';
y[2] == 'C';
y[3] == 'D';
y[4] == 'F';
But that's so much typing... Why not define it in one line, both saving your fingers and making your code more compact (and actually more readable):
char y[5] = { 'A', 'B', 'C', 'D', 'F' };
The next issue is you're returning y[5]
for the 'F' score, which is not okay -- that is accessing outside your array: remember indexing is zero-based so the valid indices range from 0 to 4 inclusive. You need to check all your indices, because a few of them are wrong.
My final point is going to be a style-based thing, and you can take it or leave it. Instead of having this big if statement, why not put all your cutoff scores into an array... Rolling this into everything else I've mentioned so far, you'd end up with something like this:
char GradeFromPercentage(float score)
{
const char grades[5] = { 'A', 'B', 'C', 'D', 'F' };
const float scores[5] = { 90.f, 70.f, 50.f, 30.f, -FLT_MAX };
for (int i = 0; i < 5; i++)
{
if (score >= scores[i]) return grades[i];
}
// It should not ordinarily be possible to reach here, but you should
// return something anyway. It's possible to get here if you supply
// a score of `NaN`. You may choose to return an invalid grade, or
// 'F' or whatever.
return '';
}
edited Nov 23 '18 at 3:07
answered Nov 23 '18 at 1:39
paddy
42.5k53076
42.5k53076
add a comment |
add a comment |
You get the error message because you try to return y[N]
from the function. When y
is defined as
char y[5][1]; // array of 5 arrays of 1 chars
then y[N]
is an array of 1 char which will decay to a pointer to char.
Also, you used a comparison operator ==
where you clearly intended assignment =
.
add a comment |
You get the error message because you try to return y[N]
from the function. When y
is defined as
char y[5][1]; // array of 5 arrays of 1 chars
then y[N]
is an array of 1 char which will decay to a pointer to char.
Also, you used a comparison operator ==
where you clearly intended assignment =
.
add a comment |
You get the error message because you try to return y[N]
from the function. When y
is defined as
char y[5][1]; // array of 5 arrays of 1 chars
then y[N]
is an array of 1 char which will decay to a pointer to char.
Also, you used a comparison operator ==
where you clearly intended assignment =
.
You get the error message because you try to return y[N]
from the function. When y
is defined as
char y[5][1]; // array of 5 arrays of 1 chars
then y[N]
is an array of 1 char which will decay to a pointer to char.
Also, you used a comparison operator ==
where you clearly intended assignment =
.
answered Nov 23 '18 at 1:40
Swordfish
111335
111335
add a comment |
add a comment |
You're only getting the array from your 2D array! You have to do something like this:
if (...) {
return y[0][1];
} else if (...) {
return y[1][1];
}
...
Your y
array is a 2D array. 2D arrays are arrays that hold arrays. y[5][1]
will define a character array of length 5
with each cell in the array holding another character array of length 1
.
Since you are referencing elements in y
at the first level only (e.g. y[0]
), you will be getting the character array instead of just a character. To get the characters inside y
, you will need to reference twice: one to get the array holding the character and another to get the character inside the array.
Alternatively, you can just use a 1D array which you can define with
char y[5];
y[0] = 'A';
...
or, as achicn3 suggested, with
char y[5] = {'A', ..., 'F'};
add a comment |
You're only getting the array from your 2D array! You have to do something like this:
if (...) {
return y[0][1];
} else if (...) {
return y[1][1];
}
...
Your y
array is a 2D array. 2D arrays are arrays that hold arrays. y[5][1]
will define a character array of length 5
with each cell in the array holding another character array of length 1
.
Since you are referencing elements in y
at the first level only (e.g. y[0]
), you will be getting the character array instead of just a character. To get the characters inside y
, you will need to reference twice: one to get the array holding the character and another to get the character inside the array.
Alternatively, you can just use a 1D array which you can define with
char y[5];
y[0] = 'A';
...
or, as achicn3 suggested, with
char y[5] = {'A', ..., 'F'};
add a comment |
You're only getting the array from your 2D array! You have to do something like this:
if (...) {
return y[0][1];
} else if (...) {
return y[1][1];
}
...
Your y
array is a 2D array. 2D arrays are arrays that hold arrays. y[5][1]
will define a character array of length 5
with each cell in the array holding another character array of length 1
.
Since you are referencing elements in y
at the first level only (e.g. y[0]
), you will be getting the character array instead of just a character. To get the characters inside y
, you will need to reference twice: one to get the array holding the character and another to get the character inside the array.
Alternatively, you can just use a 1D array which you can define with
char y[5];
y[0] = 'A';
...
or, as achicn3 suggested, with
char y[5] = {'A', ..., 'F'};
You're only getting the array from your 2D array! You have to do something like this:
if (...) {
return y[0][1];
} else if (...) {
return y[1][1];
}
...
Your y
array is a 2D array. 2D arrays are arrays that hold arrays. y[5][1]
will define a character array of length 5
with each cell in the array holding another character array of length 1
.
Since you are referencing elements in y
at the first level only (e.g. y[0]
), you will be getting the character array instead of just a character. To get the characters inside y
, you will need to reference twice: one to get the array holding the character and another to get the character inside the array.
Alternatively, you can just use a 1D array which you can define with
char y[5];
y[0] = 'A';
...
or, as achicn3 suggested, with
char y[5] = {'A', ..., 'F'};
answered Nov 23 '18 at 1:40
Sean Francis N. Ballais
1,26421932
1,26421932
add a comment |
add a comment |
edit your "==" to "="
like this
y[1] = "A";
y[2] = "B";
y[3] = "C";
y[4] = "D";
y[5] = "F";
and you just want return A,B,C,D,F
you can use 1-d array
like this
char y[5] ={'A','B','C','D','F'};
and the easiest way is
retun 'A'; return 'B'; ...
add a comment |
edit your "==" to "="
like this
y[1] = "A";
y[2] = "B";
y[3] = "C";
y[4] = "D";
y[5] = "F";
and you just want return A,B,C,D,F
you can use 1-d array
like this
char y[5] ={'A','B','C','D','F'};
and the easiest way is
retun 'A'; return 'B'; ...
add a comment |
edit your "==" to "="
like this
y[1] = "A";
y[2] = "B";
y[3] = "C";
y[4] = "D";
y[5] = "F";
and you just want return A,B,C,D,F
you can use 1-d array
like this
char y[5] ={'A','B','C','D','F'};
and the easiest way is
retun 'A'; return 'B'; ...
edit your "==" to "="
like this
y[1] = "A";
y[2] = "B";
y[3] = "C";
y[4] = "D";
y[5] = "F";
and you just want return A,B,C,D,F
you can use 1-d array
like this
char y[5] ={'A','B','C','D','F'};
and the easiest way is
retun 'A'; return 'B'; ...
edited Nov 23 '18 at 1:46
answered Nov 23 '18 at 1:28
achicn3
246
246
add a comment |
add a comment |
If you still getting error then problem is with function return type.
Firs you declare function with char return type and returning char*.
So you have to replace char * instead of char as a function return type.
If you don't know array is same like pointer Go to below link and read.
http://www.cplusplus.com/doc/tutorial/pointers/
add a comment |
If you still getting error then problem is with function return type.
Firs you declare function with char return type and returning char*.
So you have to replace char * instead of char as a function return type.
If you don't know array is same like pointer Go to below link and read.
http://www.cplusplus.com/doc/tutorial/pointers/
add a comment |
If you still getting error then problem is with function return type.
Firs you declare function with char return type and returning char*.
So you have to replace char * instead of char as a function return type.
If you don't know array is same like pointer Go to below link and read.
http://www.cplusplus.com/doc/tutorial/pointers/
If you still getting error then problem is with function return type.
Firs you declare function with char return type and returning char*.
So you have to replace char * instead of char as a function return type.
If you don't know array is same like pointer Go to below link and read.
http://www.cplusplus.com/doc/tutorial/pointers/
answered Nov 23 '18 at 2:03
the summer
93
93
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%2f53439666%2freturn-a-string-from-if-statement-in-c%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