how can i replace the array notation with pointer notation
#include<stdio.h>
#include<stdlib.h>
#include <math.h>
/* 4 */ void run_sieve(char *, int);
/* 5 */ void print_primes(char *, int);
/* 6 */ int main()
{
/* 7 */ int i, top;
/* 8 */ printf("compute primes up to: ");
/* 9 */ scanf("%d", &top);
/* 10 */ if (top < 2)
{
/* 2 is the lowest prime */
/* 11 */ printf("must be at least 2\n");
/* 12 */ return 1; /* error exit */
}
/* 13 */ top++; /* want top to appear as index in array */
/* 14 */ char *a = malloc(top*sizeof(char)); /* allocate array a[] */
/* 15 */ for (i = 2; i < top; i++)
/* 16 */ a[i] = 1; /* initialize all indices as "potential primes" */
/* 17 */ run_sieve(a, top);
/* 18 */ print_primes(a, top);
/* 19 */ return 0; /* just being compulsive about a return value for main */
}
/*
computes primes by marking positions a[i] for i
a composite number with 0
*/
/* 20 */ void run_sieve(char *a, int top)
{
/* need only remove multiples of primes of
i for i up to and including sqrt(top)
*/
int last_start = (int)sqrt(top);
/* 22 */ int i, j;
/* 23 */ for (i = 2; i <= last_start; i++) /* remove multiples of i */
/* 24 */ if (a[i]) /* need only remove multiples of primes */
/* 25 */ for (j = 2*i; j < top; j += i)
/* 26 */ a[j] = 0; /* mark j as non-prime by storing 0 */
}
/*
displays primes up to top - 1 by finding non-1
values in a[] and reporting their indices
*/
/* 27 */ void print_primes(char *a, int top)
{
/* 28 */ int i, column_count = 0;
/* 29 */ for (i = 2; i < top; i++)
/* 30 */ if (a[i]) /* a[i] is non-0 if i is prime */
{
/* 31 */ printf("%8d", i);
/* after 8 columns start a new line */
/* 32 */ if (!(++column_count%8))
/* 33 */ printf("\n");
}
/* 34 */ printf("\n");
}
ali11 -1 Light Poster
-[xxxxxxx]- 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.