1

I am taking a number in the main function, make it an array in make_array function. In the palindrome function, I need to check the array which i made in the make_array function but it is not visible in the palindrome function.

How can I solve this problem?

#include<stdio.h>
#define N 5

void make_array(int n);
int palindrome(int ar[],int size);

int main()
{
    int num;
    printf("Enter a number to check: ");scanf("%d",&num);

    make_array(num);

    if(palindrome(/*Don't know what should I write here*/))
         printf("It is palindrome");

    else
         printf("It is not palindrome");
}

void make_array(int n)
{
    int arr[N];
    int digit,i=0;

    while(n>0){
        digit=n%10;
        arr[i]=digit;
        n/=10;
        i++;
    }

    printf("Array: ");
    for(i=0; i<N; i++)
        printf("%d ",arr[i]);
}

int palindrome(int ar[],int size)
{
    int i,j;
    int temp[N];
    j=N;

    for(i=0; i<N; i++)
        temp[i]=ar[i];


    for(i=0; i<N; i++){
        if(temp[j-1]!=ar[i])
            return 0;
        j--;

    }


    return 1;

}
Duck Dodgers
  • 3,409
  • 8
  • 29
  • 43
  • Possible duplicate of [C: Palindrome: Different strlen-values](https://stackoverflow.com/questions/34093093/c-palindrome-different-strlen-values) – Santosh A Jan 14 '19 at 10:37
  • Local variables vanish when the function returns. Either pass the array into the `make_array()` function, or have it dynamically allocate the array and return a pointer to it. Changes either way. – Jonathan Leffler Jan 14 '19 at 10:37
  • There are several issues, make_array only creates an array on the stack, when you leave the function, it will be popped from the stack and not exist anymore. Move your array from make_array and make it global to the application, then you can access it after you leave make_array. – SPlatten Jan 14 '19 at 10:59

3 Answers3

0

The best way is to leave allocation to the caller. Then you can simply do

int main()
{
    int num;
    printf("Enter a number to check: ");scanf("%d",&num);

    int array[num];

    fill_array(num, array);

where fill_array does what "make_array" does in your code, minus the allocation part.

void fill_array(int num, int array[num]);

The palindrome function could be rewritten similarly:

int palindrome(int size, int array[size])

All of this uses the concept of variable-length arrays (VLA).

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • It looks like the array holds digits, so "enter number to check: " should return say 123, you'll allocate an array 123 elements large, but you really only need 3. – matt Jan 14 '19 at 11:01
0

I have done some modification in your code so please refer it.

#include<stdio.h>
#include "stdafx.h"

#define N 5

int make_array(int n, int *arr);
int palindrome(int ar[],int size);

int main()
{
    int num;
    int arr[N];
    int iRet;

    printf("Enter a number to check: ");scanf_s("%d",&num);

    iRet = make_array(num, arr);

    if(palindrome(arr, iRet))
         printf("It is palindrome");

    else
         printf("It is not palindrome");
}

int make_array(int n, int *arr)
{
    //int arr[N];
    int digit,i=0;

    while(n>0){
        digit=n%10;
        arr[i]=digit;
        n/=10;
        i++;
    }

    printf("Array: ");
    for(int j=0; j<i; j++)
        printf("%d ",arr[j]);

    return i;
}

int palindrome(int ar[],int size)
{
    int i,j;
    int temp[N];
    j=size;

    for(i=0; i<size; i++)
        temp[i]=ar[i];


    for(i=0; i<size; i++){
        if(temp[j-1]!=ar[i])
            return 0;
        j--;

    }


    return 1;

}
  • To enhance your answer add some explanation on how your code solves OP's problem and about what was causing his issue. Simply providing a code might not be enough. – delirium Jan 14 '19 at 11:08
0

The problem is with your make array function. When a function is called, the stack grows down and registers and pointers are allocated to save the point from which that function was called, now, here you send n by value and your function creates a place on the stack for an array that you fill- BUT- when your function returns- the stack pointer returns back up to the caller( if your function has a return value it will be saved in a pre-allocated place, but other than that all of the other function data on stack is unavailable.).

So in general, if you want a function to create an array that could be used later on it must be allocated on heap you can either return int* or send foo(int**) to the function that will hold the add. of the new allocated array.

another option is to allocate that array[N] in your main, and send foo(int arr[], int n, size_t size) to the function. Since the array was allocated by the caller in main- this memory will be valid for all of the main function life.

so option 1)

int main()
{
 int num;
 int* array;
 printf("Enter a number to check: ");scanf("%d",&num);
 array = make_array(num, N);

 if(palindrome(array, N))
     printf("It is palindrome");

 else
     printf("It is not palindrome");

 free(array); /*free heap allocation */
}


int* make_array(int n, size_t size)
{
int* arr;
int digit ,i=0; 

arr = malloc(sizeof(int)*size);
if(NULL == arr)
{
   return NULL; /* malloc failed*/
}
while(n>0 && i<size){
    digit=n%10;
    arr[i]=digit;
    n/=10;
    i++;
}

printf("Array: ");
for(i=0; i<N; i++)
    printf("%d ",arr[i]);

 return arr;
 }

or 2)

int main()
{
int num;
int array[N];/*array saved on stack in main function */
printf("Enter a number to check: ");scanf("%d",&num);

make_array(array,num, N);

if(palindrome(/*Don't know what should I write here*/))
     printf("It is palindrome");

else
     printf("It is not palindrome");
}



void make_array(int* arr, int n, size_t size)
{

int digit,i=0;

if(NULL == arr)/*if arr is not a valid pointer*/
{
   return;
}
while(n>0 && i<size){
    digit=n%10;
    arr[i]=digit;
    n/=10;
    i++;
}

printf("Array: ");
for(i=0; i<N; i++)
    printf("%d ",arr[i]);
}
H.cohen
  • 517
  • 3
  • 9