For a C++ program, the memory of a computer is like a succession of memory cells, each one byte in size, and each with a unique address. These single-byte memory cells are ordered in a way that allows data representations larger than one byte to occupy memory cells that have consecutive addresses.
This way, each cell can be easily located in the memory by means of its unique address. For example, the memory cell with the address 1776 always follows immediately after the cell with address 1775 and precedes the one with 1777, and is exactly one thousand cells after 776 and exactly one thousand cells before 2776.
When a variable is declared, the memory needed to store its value is assigned a specific location in memory (its memory address). Generally, C++ programs do not actively decide the exact memory addresses where its variables are stored. Fortunately, that task is left to the environment where the program is run - generally, an operating system that decides the particular memory locations on runtime. However, it may be useful for a program to be able to obtain the address of a variable during runtime in order to access data cells that are at a certain position relative to it.
*n is a pointer in this code which locates value stored at “A”.
Initially the value of all elements in “result” is zero. So if you remove that line your code always change the value of “result[k++]” and the value of “A” is always constant.
I am assuming you know what pointers are and how they work. If not, please take a look here. In summary, a pointer stores the memory address of another variable. Which can be used to change the value of a variable from a different function, for instance.
The sieve function you have here is defined as int* sieve(int A, int *n). It takes 2 arguments. int A is the limit upto which it finds all primes. int *n is the address of a variable sent to this functions, whose purpose is to store the count of all primes found (not mentioned in the code). The function returns an int* value, which is a pointer to the result array containing the primes found. The method is used as
int main(void) {
int cnt, i; // cnt will store the number of primes found
int *res = sieve(100, &cnt);
// ^ here the address of cnt variable is passed to sieve function
for(i=0; i< cnt; i++)
printf("%d ", res[i]);
return 0;
}
After the sieve function receives the value of the cnt variable as int *n, whenever *n is used it refers to the value of the cnt variable. In the first line of the code, *n is set to A. So the variable cnt in main gets set to A. Later while gathering the primes into result, k refers to the number of primes found. Each time a new prime is collected the count needs to be updated, so *n is updated to k.
If you delete this line, the *n = A line remains. So the value of cnt will be set to A and when sieve finishes execution it will remain A. As a result you will see A values while printing, where only k are primes and rest are 0 as provided by malloc.
I have tried to document the code it properly so that it’s easier to understand, here. Hope this helps, and do ask if something is not clear.
First of all, thanks for your efforts mate. It’s my mistake that I didn’t tell the use of *n, it is used to define the length of array. The line I have problem is *n = k. This is the part I didn’t understand.
But there is one thing I don’t get, in my for loop, *n is getting changed alright but that *n is not getting used anywhere inside my loop still if I remove the value changes
It’s not “n=k”, it’s “*n=k”. “*n = k” changes the value at the address stored in n to k. The address stored in n comes from the caller function, so that the sieve function can save the number of primes found in *n. I would suggest you read up on pointers further.