using pointer

answerme's Avatar, Join Date: Dec 2007
Ambitious contributor
want to print the sum of M to N numbers ,using pointer .Where am i getting wrong
Code:
{
	int m,n;
	int *p1,*p2,sum=0,*p;
	printf("Enter the m");
	scanf("%d",&m);
	printf("Enter the n");
	scanf("%d",&n);
	*p1=&m;
	*p2=&n;
	while(*p1<=*p2)
	{
		sum=sum+*p;
		(*p)++;
	}
	printf("%d",sum);
}

Last edited by shabbir; 17Dec2007 at 17:18.. Reason: Code block
kv.santosh's Avatar, Join Date: Dec 2007
Newbie Member
1. " *p1=&m; *p2=&n;" This is wrong. If you want to make pointer p1 to point to integer m, then it should be "p1 = &m".
2. Where did u initilize pointer p. it should be hoding some junk value!!!!!
3. I did not understand your concept of using a while loop for sumation. Moreover in this exercise your not getting any benifit of using pointers!!!!! If you really want to play arround with pointers, do something more complicated...a simple link list might be.
answerme's Avatar, Join Date: Dec 2007
Ambitious contributor
thanks i made some stupid mistakes