Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to intialize an empty C array using pointers and fulfill it the same way

I’m trying to solve this following question: (I’m new to coding)

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

I don’t know how to proceed though cuz I don’t remember how to return the array values I’m filling this array:
I appreciate any help

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


#include <stdio.h>
#include <stdlib.h>

int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
    
    int a = 0, b = 0;

    for(int i = 0; i < numsSize; i++) {
        for(int j = 0; j < numsSize; j++) {
            if(nums[i] + nums[j] == target) {
                a = i;
                b = j;
                break;
            }
        }
    }

    returnSize* = a;
    *(returnSize + 1) = b;

    return returnSize;
}
int main() {
    int num = 0;
    int* nums[4] = {2,7,11,15};
    int numsSize = 4;
    int* returnSize[2];
    int target = 9;

    num = twoSum(nums, numsSize, target, returnSize);

    printf("Resultado: %i \n", num);

    free(myArray);

    return 0;
}

I don’t know how to proceed though cuz I don’t remember how to return the array values I’m filling this array:
I appreciate any help

>Solution :

If you want to return an array from a function, you can’t. You can return a pointer to an array. If the array is created within the function, it must be dynamically allocated so that it’s lifetime survives the function.

Since you pass in a pointer to an array allocated in main, this is not an issue.

You do have a syntax error in your code:

    returnSize* = a;
    *(returnSize + 1) = b;

Better to simplify this to:

    returnSize[0] = a;
    returnSize[1] = b;

But you also have a logical error. Your break will only escape the inner loop, and the outer loop will continue. We can replace this with the following, with the condition returning immediately from the function.

If nothing is found, return a sentinel values -1 for both indices.

int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
    for (int i = 0; i < numsSize; i++) {
        for (int j = 0; j < numsSize; j++) {
            if (nums[i] + nums[j] == target) {
                returnSize[0] = i;
                returnSize[1] = j;
                return returnSize;
            }
        }
    }

    returnSize[0] = -1;
    returnSize[1] = -1;
    return returnSize;
}

A further tweak would be that your inner loop doesn’t need to iterate over every element. Only those after the first item. And the outer loop doesn’t need to iterate to the end.

int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
    for (int i = 0; i < numsSize - 1; i++) {
        for (int j = i; j < numsSize; j++) {
            if (nums[i] + nums[j] == target) {
                returnSize[0] = i;
                returnSize[1] = j;
                return returnSize;
            }
        }
    }

    returnSize[0] = -1;
    returnSize[1] = -1;
    return returnSize;
}

This ensures that it we searched {2, 7, 11, 15} for a target value of 4, we wouldn’t get {0, 0} which would be an invalid result as it doesn’t point to two different indices.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading