debugging

i want step by step explanation of the
function "fun"

please help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
#include<string.h>
void fun(int * p);
void main()
{
	int arr[20]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,18};
	int a;
	fun(&arr[5]);
	for(a=0;a<20;a++)
		printf("%d  ",arr[a]);
}
void fun(int *p)
{	
	*(p+4)=*(p+2)+ *(p+1);
	p=p+2;
	p+1;
	p=p+2;
	p[3]=p[11];
	*(p+3)=p[3]+3;
	*(p+3)=*p+3;
}
It's very simple. Which line do you not understand?
Well, there's a lot of fluff in there. For example line 16 doesn't do anything. But it's not the only line which has no meaningful outcome.
i have problem from line 15 to line 20
i am puzzled with addresses and values ,,
and what does p[3]+3 do and what will be p[3] in line 19 ??
Why don't you add some extra cout statements, so you can how each value changes each time.

Or learn how to use the debugger in your IDE, and step through the code with a watchlist.
But it's not the only line which has no meaningful outcome.


Yes it is. Every other line changes the pointer or the array.
Moschops wrote:
Yes it is. Every other line changes the pointer or the array.

You are correct.

But we may disagree not on what the program does, but on the meaning of the word "meaningful". Perhaps it was a poor choice of word on my part.
@shahzaib1111

If this is still unclear, I suggest you study each of the sections on arrays and pointers, and in particular, the section titled "Pointers and arrays" in the second reference.

http://www.cplusplus.com/doc/tutorial/arrays/
http://www.cplusplus.com/doc/tutorial/pointers/
Last edited on
Topic archived. No new replies allowed.