-1

Its a simple insert and display elements in an array code that I have done.

'    

#include<stdio.h>
int insert(int *arr);
int display(int *arr);
int main()
{

int MAX=5;
int arr[MAX];
insert(arr);
display(arr);
}
int insert(int *arr)
{
int n,i;
printf("Enter how many elements");
scanf("%d",&n);
 for(i=0;i<n;i++)
        {
            printf("Enter value at arr[%d]\n",i);
            scanf("%d",&arr[i]);
        }
return *arr;
}


int display(int *arr)
{
        int i;
        printf("The values are::");
        for(i=0;arr[i]!=NULL;i++)//This loop is giving some trouble
        {
                printf("\n a[%d]->%d \n",i,arr[i]);
        }
        return *arr;
}

'

Output:: In function 'display': prog.c:33:24: error: comparison between pointer and integer for(i=0;arr[i]!=NULL;i++) ^ This is the error am not able to solve.

ameyCU
  • 16,489
  • 2
  • 26
  • 41
Oswarld
  • 11
  • 2

2 Answers2

1

You have multiple things to be corrected in your code, but the error that you are getting is due to comparison between pointer (NULL) and an int arr[i]

for(i=0;arr[i]!=NULL;i++)

Your integer array arr does not have any NULL element in the end which can be compared. Perhaps you are confused between string and integer arrays.

Another thing that can be changed in your code, is the return type of your functions insert() and delete(). You are not recieveing the return values and thus its better to keep them void.

A Suggestion

Return n from the insert() function, receive it in main(). Then send it to the display() function along with the array, then use that in the for loop.

Best way to know the number of elements in an array that you are passing to a function is sending it to that function along with the array.

Haris
  • 12,120
  • 6
  • 43
  • 70
0
'
#include<stdio.h>
#include<stdlib.h>
int MAX=5,i,pos,val;
int display(int *arr);
int main(void)
{
    int arr[MAX];
    display(arr);
}

int display(int *arr)
{
    printf("The values are::");
    for(i=0;i<MAX;i++)
    {
           printf("\n a[%d]-->%d ",i,arr[i]);
    }
        return *arr;
}'

To avoid use of too many variables I continued the use of 'MAX' in the code.Making it global and it worked!!

Oswarld
  • 11
  • 2