c language to find two numbers

White made the first two numbers and topics, using the C language classes to learn. The whole idea is relatively simple, through the array in the order sum, starting from the first number, and each number in turn summation later, and determines whether or equal to the target number. If not equal, proceed to the next number, and then summed with the next number, such that the final number can all pairwise addition, the outcome.

It was not two numbers and output the outset, that you need to look after returnSize set to 2, to read returns an array of two numbers.
Error message
Later in the figure above error by here that the cause of the error, the returned array is set to static array so that the array will not disappear after the end of the function, it can return. The final run code as follows:

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
    int i=0,j;
    *returnSize = 2;
    static int r[2]={0,0};
    for(i=0;i<numsSize-1;i++)
        for(j=i+1;j<numsSize;j++)
             {
                if(nums[i]+nums[j]==target)
                {
                     r[0]=i;
                    r[1]=j;
                    return r;
                }
             }
    return 0;
}

operation result:
Here Insert Picture Description

Released seven original articles · won praise 0 · Views 142

Guess you like

Origin blog.csdn.net/qq_44183026/article/details/104595583