1

I'm very new to programming so please bear with me!

This is my code (in C):

int * arrayExpander(int arr[], int size);

int main()
{
    int arr[] = { 1,2,3,4,5 };
    int size = 5;
    int* arrPtr = arrayExpander(arr, size);

    for (int i = 0; i < size * 2; i++) {
        printf("New array: %d \n", *arrPtr);
    }
}

int * arrayExpander(int arr[], int size)
{
    int newInt[10];
    int* expandedArr = &newInt;

    //move elements from original to expanded and initialise remaining elements with 0

    for (int i = 0; i < size * 2; i++) {
        if (i < size) {
            expandedArr[i] = arr[i];
        } else {
            expandedArr[i] = 0;
        }
    }
    return expandedArr;
}

My print statement isn't printing the values stored in the expandedArr correctly. I've tried a few variations and not sure how to fix it.

Thanks in advance

Ôrel
  • 7,044
  • 3
  • 27
  • 46
Aisha
  • 13
  • 2
  • You can't use the pointer because `int newInt[10];` no longer exists after the function has returned. Please see [Function returning address of local variable error in C](https://stackoverflow.com/questions/22288871/function-returning-address-of-local-variable-error-in-c). You can allocate dynamic memory; use a `static` array; pass another array as the target; or modify the passed array in-place. – Weather Vane Dec 12 '20 at 17:44
  • ... if it is large enough. – Weather Vane Dec 12 '20 at 17:51

1 Answers1

1

In your code, expandedArr is a local pointer variable, which gets destroyed, after we return from function. We need to allocate memory using malloc to extend their lifetime. See the below code :

#include<stdio.h>
#include<stdlib.h>
int * arrayExpander(int arr[], int size);

int main()
{
    int arr[] = { 1,2,3,4,5 };
    int size = 5;
    int* arrPtr = arrayExpander(arr, size);

    for (int i = 0; i < size * 2; i++) {
        printf("New array: %d \n", *(arrPtr+i)); // *arrPtr prints only first element
    }
}

int * arrayExpander(int arr[], int size)
{
    
    int* expandedArr = malloc(10*sizeof(int)); // You need allocate memory in heap

    //move elements from original to expanded and initialise remaining elements with 0

    for (int i = 0; i < size * 2; i++) {
        if (i < size) {
            expandedArr[i] = arr[i];
        } else {
            expandedArr[i] = 0;
        }
    }
    return expandedArr;
}

The output :

New array: 1 
New array: 2 
New array: 3 
New array: 4 
New array: 5 
New array: 0 
New array: 0 
New array: 0 
New array: 0 
New array: 0 
Krishna Kanth Yenumula
  • 2,533
  • 2
  • 14
  • 26
  • @Aisha Check this to know about accepting answer : https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Krishna Kanth Yenumula Dec 15 '20 at 14:13
  • I have done but i am getting a warning: Dereferencing NULL pointer 'expandedArr', in the else statement of the function. Would you be able to explain why? – Aisha Dec 15 '20 at 19:03
  • @Aisha I enabled all the warnings, but I did not get that warning in my system. I am using Ubuntu 18.04. Ask an another question, with more details – Krishna Kanth Yenumula Dec 16 '20 at 00:15
  • @Aisha If we try to dereference a pointer with NULL value, then we will get that warning. Example: `int *x = NULL; int y = *x`. But , in the code, we are allocating 10 valid addresses using `malloc`, so, you should not get that warning. – Krishna Kanth Yenumula Dec 16 '20 at 00:22