-1

I got this question asked by one of my peer, as i don't know "C" that much, but still being a beginner i tried solving it, this is the approach i used, but it is not giving the expected output.

According to the question, it should print the entered elements of an array, when we are passing the reference of an array to a function, can anyone review this code ?

#include <stdio.h>
void print(int arr[], int n);

int main(){
    
    int n,i;
    int arr[n];

    printf("enter the size of array :");
    scanf("%d",&n);
    
    
    printf("enter elements :\n");
    for(i=0; i<n; i++){
        scanf("%d",&arr[i]);
    }
    print(&arr[i], n);
    
}

void print(int arr[], int n){
    int i;
    for(i=0; i<n; i++){
        printf("\nentered elements are : %d",arr[i]);
    }
}
  • 2
    You can't say `int arr[n]` and then only later give `n` a value. – Steve Summit Sep 09 '22 at 16:13
  • @SteveSummit so, if i want to take input from the user for "array size" then how would i be able to achieve that ? – Karishma Singh Sep 09 '22 at 16:15
  • 1
    The `void print(int arr[], int n)` function you've started to write is mostly correct. But you want to call it from `main` as `print(arr, n);`. You don't need the `&` — although the reason for that is surprising — and you *definitely* don't want the `[i]`, because here you're trying to pass — trying to get the same effect as if you had passed — the whole array, not just one element of it. – Steve Summit Sep 09 '22 at 16:16
  • Why not prompt for and read the value of `n` from the user, *then* say `int arr[n];`? – Steve Summit Sep 09 '22 at 16:17

2 Answers2

2

This declaration of the variable length array

int n,i;
int arr[n];

invokes undefined behavior because the variable n was not initialized.

Instead you need to write

int n,i;

printf("enter the size of array :");

if ( scanf("%d",&n) == 1 && n > 0 )
{
    int arr[n];
    // and so on

This call

print(&arr[i], n);

again leads to undefined behavior because the value of the variable i is equal to the size of the array. So the expression &arr[i] points to outside the array.

You need to write

print( arr, n);

It will be more correctly to declare and define the function like

void print( const int arr[], int n )
{
    printf( "\nentered elements are: " );
 
    for ( int i = 0; i < n; i++ )
    {
        printf( "%d ", arr[i] );
    }
    putchar( '\n' );
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    @SadlyFullStack Yes. A parameter having an array type is adjusted by the compiler to pointer to the array element type. – Vlad from Moscow Sep 09 '22 at 16:36
  • @VladfromMoscow now, it is taking user's input for "array size", also it is taking input for "array elements", but still it is not showing the expected output, as i have done some changes in the above code. kindly re-review that – Karishma Singh Sep 09 '22 at 16:40
  • 1
    @KarishmaSingh Reread my answer one more. I showed how the function must be called. And restore the original code in your question. Otherwise the question and answers will confuse readers. – Vlad from Moscow Sep 09 '22 at 16:45
  • @VladfromMoscow okay, i have restored the original code – Karishma Singh Sep 09 '22 at 16:52
0

Your Program is not giving output because you have not declared the size of array, i.e., instead of arr[n] write arr[100] or any other positive integer. Else the code is error free.

Hope this is helpful.