I am trying to print prime numbers from 1 to 100. I am passing an array to a function and then zeroing all the elements which are multiples of 2,3,5. When I compile the code,I am getting all the memory addresses as an output. Here is the code that I have written.
Further can I anyone of the moderators enlighten me on pointer arithmetic in context of arrays and functions. I have read arrays revealed article but it didn't give me much information.
Code:
#include <stdio.h>
#include <stdlib.h>
#define size 100
int main()
{
int i,a[size];
int prime(int *,int);
for(i=1;i<size+1;i++)
{
a[i]=i;
}
printf("Before procedure the array is\n");
for(i=1;i<size+1;i++)
{
printf("%d\n",a[i]);
}
prime(&a[0],size);
return 0;
}
int prime(int *ptr,int j)
{
int k;
ptr++;
for(k=1;k<j+1;k++)
{
if(*ptr==2||*ptr==3||*ptr==5)
{
break;
}
else
{
if(*ptr%2==0||*ptr%3==0||*ptr%5==0)
{
*ptr=0;
}
}
ptr++;
}
ptr=ptr-99;
printf("Array after removal\n");
for(k=1;k<j+1;k++)
{
if(*ptr==0)
{
break;
}
else
{
printf("%d\n",*ptr);
}
ptr++;
}
}
Comment