×

# Help needed for pointer variable

 0 I was solving the question that prints primes up to a limit. I solved after looking at solution. I couldn't understand the use of pointers here.  int prime[100000000]; int* sieve(int A, int *n) { *n = A; // length of result array int *result = (int *) malloc(*n * sizeof(int)); int i,j; prime[0] = 1; prime[1] = 1; int k=0; for(i = 2;i <=A; i++) { if(prime[i] == 0) { for(j=i*2;j<=A;j+=i) prime[j] = 1; } } for(i = 2;i <= A; i++) { if(prime[i]==0) result[k++] = i; *n = k; // PLEASE HELP ME UNDERSTAND THE USE OF THIS LINE } return result; }  I have commented the line I don't understand. Also, If I remove that line, I get array filled with 0s Please help me here asked 02 Jan '17, 21:11 115●2●10 accept rate: 0%

 2 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. answered 02 Jan '17, 23:44 6★meooow ♦ 7.1k●7●18 accept rate: 48% 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. (03 Jan '17, 15:30) 2 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. (04 Jan '17, 00:08) meooow ♦6★
 0 please provide full solution so I can understand your doubt. answered 02 Jan '17, 21:25 58●1●6 accept rate: 7%
 0 *n = k; updating a size of the usable data in result. prime numbers are in between result[0] and result[*n - 1]. answered 03 Jan '17, 06:44 1 accept rate: 0%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×62

question asked: 02 Jan '17, 21:11

question was seen: 544 times

last updated: 04 Jan '17, 00:08