using pointer

Discussion in 'C' started by answerme, Dec 17, 2007.

  1. answerme

    answerme New Member

    Joined:
    Dec 17, 2007
    Messages:
    114
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Dec 17, 2007
  2. kv.santosh

    kv.santosh New Member

    Joined:
    Dec 13, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Hyderabad
    Home Page:
    http://kv.santosh.googlepages.com/
    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.
     
  3. answerme

    answerme New Member

    Joined:
    Dec 17, 2007
    Messages:
    114
    Likes Received:
    0
    Trophy Points:
    0
    thanks i made some stupid mistakes
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice