三行代码  ›  专栏  ›  技术社区  ›  weshare

如何调用指针函数

  •  -1
  • weshare  · 技术社区  · 1 周前

    请告诉我如何调用函数twoSum bellow,它有int*,我不知道怎么做。非常感谢。

    #include <stdio.h>
    
    //int twoSum(nums, target);
    int main() {
        printf("Hello, World!\n");
        int nums[10]= {2,7,11,15};
        int target = 9;
        int numsSize=3;
        int retrunSize=3;
        int a=twoSum(nums,numsSize,target,retrunSize);
        return 0;
    }
    
    int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
        for (int i = 0; i < numsSize; ++i) {
            for (int j = i + 1; j < numsSize; ++j) {
                if (nums[i] + nums[j] == target) {
                    int* ret = malloc(sizeof(int) * 2);
                    ret[0] = i, ret[1] = j;
                    *returnSize = 2;
                    return ret;
                }
            }
        }
    
        *returnSize = 0;
        return NULL;
    }
    

    有了评论中的有用建议,我得到了修改后的版本,但“tprintf(a)”什么也没给我;

    #include <stdio.h>
    //int twoSum(nums, target);
    #include <stdlib.h>
    int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
        for (int i = 0; i < numsSize; ++i) {
            for (int j = i + 1; j < numsSize; ++j) {
                if (nums[i] + nums[j] == target) {
                    int* ret = malloc(sizeof(int) * 2);
                    ret[0] = i, ret[1] = j;
                    *returnSize = 2;
                    return ret;
                }
            }
        }
        *returnSize = 0;
        return NULL;
    }
    int main() {
        printf("Hello, World!\n");
        int nums[4]= {2,7,11,15};
        int target = 13;
        int numsSize=6;
        int retrunSize=0;
        int* a=twoSum(nums,numsSize,target,retrunSize);
        printf(&a);
        printf(*a);
        return 0;
    }
    

    enter image description here

    调试图片在这里。

    1 回复  |  直到 1 周前
        1
  •  -2
  •   Magesh    1 周前

    你可以参考下面的,

     #include <stdio.h>
     #include <stdlib.h>
     
    int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
        for (int i = 0; i < numsSize; ++i) {
            for (int j = i + 1; j < numsSize; ++j) {
                if (nums[i] + nums[j] == target) {
                    int* ret = malloc(sizeof(int) * 2);
                    ret[0] = i, ret[1] = j;
                    *returnSize = 2;
                    return ret;
                }
            }
        }
        *returnSize = 0;
        return NULL;
    }
     
        //int twoSum(nums, target);
        int main() {
            printf("Hello, World!\n");
            int i;
            int* (*funptr)(int*, int, int, int*) = &twoSum; // function pointer declaration
            int nums[10] = {2,7,11,15};
            int target = 9;
            int *returnSize = malloc(sizeof(int)); // need returnsize pointer vairable to collect return size
            int *a=(*funptr)(nums, 10, target, returnSize); // function pointer calling
            // Below are output statements
            printf("returnSize = %d\n", *returnSize);
            printf("Nums porsition are: ");
            for (i = 0; i < *returnSize; i++) {
                printf ("a[%d] = %d ", i, a[i]);
            }
            return 0;
        
        }