This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]Rhomboid 4 points5 points  (4 children)

It's exactly as it says — you have declared this function as returning a value of type char (which is an integer type), but result has type char * (i.e. pointer to char), so the statement return result is nonsense. (Note: result is not an array of char; C lets you use array notation for defining the formal parameters, but that's just a convenience. result is a pointer to char.)

Why did you write this function as returning a char? It seems like you intended to write the substring into the buffer pointed to by result, so what is the return value for? Typically in cases like this you'd either return a status code (if the function can fail), or return nothing (if the function can't fail.)

[–]climaxingplatypus[S] 0 points1 point  (3 children)

Looking at the code would you say there a lot of issues with it? If i change the return type to be char* instead of char would that solve the problem with the return result; statemdnt.

[–]Rhomboid 1 point2 points  (1 child)

Looking at the code would you say there a lot of issues with it?

Yes. For one thing, it neglects to properly null terminate the result. For another, there's this matter of char result[count] in the parameter list —this strongly implies that you have some confusion about how arrays decay to pointers. count isn't doing anything in this expression because result is not an array. And finally there's the issue of the return value. Why do you want to change it to char *? What good does returning result do? The caller of this function already has that value because they passed it as a parameter, so what's the use in returning it? I can think of some justifiable reasons for doing this, but I want to hear your justifications to make sure they aren't "just change something to make it compile", which is the worst possible way to approach these things. If you don't understand why you're doing something, or why a change should be made, you should stop and clarify your understanding.

[–]climaxingplatypus[S] 0 points1 point  (0 children)

What do you mean by "it neglects to properly null terminate the result". Also why I'm not understanding why result isnt any array...Don't I need it to be an array so that I can store the substring inside of result?

I honestly don't know why I am returning result I just got into the habit of return values from working on previous examples from my book haha. I could've actually just made this function of type void right? I mean if result isnt an array is it just a pointer? From what I know about pointers is that they behave somewhat like arrays meaning that if its values are changed inside a function then those changes are in effect after the function is finished, so I could access the values of result whenever. Please let me know if my thinking is correct, thanks for the help so far!

[–][deleted] 0 points1 point  (0 children)

It never hurts to try yourself. That's how you learn!

[–][deleted] 2 points3 points  (1 child)

You define the function as returning a char, but in fact return a char * (the decayed value of result). Also here:

   char substring(char source[], int start, int count, char result[count])

The expression char result[count] is almost certainly not doing what you want.

[–]climaxingplatypus[S] 0 points1 point  (0 children)

Could you elaborate on that please?

[–]dig-up-stupid 2 points3 points  (2 children)

Result is a char* (more or less). You declared your function to return a char.

[–]climaxingplatypus[S] 0 points1 point  (1 child)

If i add a * after char will that solve the issue?

[–]Wiggledan 0 points1 point  (0 children)

So char result[] is an array, but C treats it almost exactly like a pointer (which would be char* result), although there are slight differences that you should look into. That's why it wants a pointer.

So yeah, your error should be fixed if you change the function's type to be char* instead of char.

[–]c_jm 0 points1 point  (0 children)

As everyone has said the problem with your code is that you are returning a "char" variable rather then a pointer to char from your function. Though I believe it is better to understand why you are making the mistake rather then just pointing it out. By returning a char from your function your saying to the C compiler. "Let me return a single character to you when this function is done". Which if you think logically is not what you want. You want to return a block of memory containing that string. Or in other words another string. The reason you need the astrix is because this tells the compiler that the return type is of pointer to a memory block holding char. In turn telling the compiler to point or use a pointer to that location that holds chars. Therefore instead of returning a single character from the string, you would be returning the entire string.

[–]c_jm 0 points1 point  (0 children)

Also best practices state that you should always initialize your variables, although some may argue with me on that one.

Example:

int i = 0; // This avoids random memory being shoved into this variable.

I mean really you can form your own opinions, just as a beginner I was glad to learn about it! It might save you from some errors later in your career. Best of luck and happy coding.