2

I am new to LeetCode and am trying to improve upon my problem solving techniques. I am attempting to solve the Two Sum Problem in C and have run into trouble with the final return statement. The code initially provided to me by LeetCode was a function called "int* twoSum" and the goal is to find the two indices in an array that produce the target number. The function lists a couple parameters that I assumed were provided in main since it was not shown.

I changed the name of the function to just "int twosum" and removed the int* returnSize because I am not a big fan of unnecessary pass by address instead of by value and felt it wouldn't have a significant impact. However, after trying to run my code I run into the warning error: "returning 'int *' from a function with return type 'int' makes integer from pointer without a cast"

Could someone who understands this issue or has solved the problem before on LeetCode please provide insight as to what I need to correct? Thank you.

int twoSum(int *nums, int numsSize, int target){

    int outerCount; //loop control variable for outer loop
    int innerCount; //loop control variable for inner loop
    int array[2]; //array that stores indices of numbers that produce target

    for(outerCount = 0; outerCount < numsSize; outerCount++)
        for(innerCount = outerCount + 1; innerCount < numsSize; innerCount++)
        {
            if(nums[outerCount] + nums[innerCount] == target)
            {
                array[0] = outerCount;
                array[1] = innerCount;
            }
        }

    return array;
}
2
  • Are you allowed to change the function's signature? There are numerous solutions to this dilemma. You could make the array static, or return a struct, or use dynamic memory, or alter the input array and use it to return the values, etc... Commented Aug 9, 2019 at 0:10
  • Yes, you are allowed to change the function's signature. I've seen a couple of solutions that were similar to mine, but I didn't seem to make proper use of returning the array by address. Commented Aug 9, 2019 at 1:19

1 Answer 1

2

The problem asks you to return two integers (indices), so a return type of int is pretty clearly incorrect. int is a single integer; two return two integers you need to return an array of int, or struct containing two integer members. C doesn't allow you to return arrays by value, so if you need to return an array, you need to return an int*. That's just the way C is.

Note that since you cannot return an array by value, you also cannot return a pointer to an automatically allocated array, since that object's lifetime will end when the function returns. So you need to dynamically allocate the array (unless it is passed to your function as an argument, which is a very common style). In this case, it is pretty clear that a dynamically allocated return value is desired, based on the comment:

/* Note: The returned array must be malloced, assume caller calls free(). */

Whether or not you like this style, you will need to conform to it for this exercise, since it is pretty clear that the caller will call free()on the returned pointer, and calling free() on a pointer not originally returned by malloc is Undefined Behaviour (and very likely to crash your program). (You can free(NULL), but that's also a violation of the calling contract, which will segfault when the caller tries to examine the non-existent return values.)

C does let you return structs by value, but if you are going to return a struct, you and the caller need to agree on its declaration (the names of its members, for example).

Sign up to request clarification or add additional context in comments.

2 Comments

So with my program above, if I still wanted to use pass by the address of the array instead of using struct (which I have not yet learned yet but will practice now), how I would I go about doing that? Here is the given function definition line: int* twoSum(int* nums, int numsSize, int target, int* returnSize){ I believe that int* nums is the given test case array, and returnSize is the indices of the array. Since the function isn't void, I am unsure what to return since everything is passed by address.
@africanmamba: leetcode almost always expects that returned arrays will be dynamically allocated; also, it generally provides an "out" parameter for you to return the size of the returned array. (C doesn't provide an array type with an explicit length. In this particular case, the length of the returned array must be two, so you would (1) start by allocating a two element array using int *indices = malloc(2*sizeof * indices); (2) and then after you figure out the correct values, return them: `*returnSize = 2; return indices;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.