pointer and function arguments

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tan007
    New Member
    • Sep 2011
    • 6

    #1

    pointer and function arguments

    Code:
    #include<stdio.h>
    #include<ctype.h>
    int getch(void);
    void ungetch(int);
    int getline(int *);
    
    int main()
    {
    	int i;
    	printf("%d\n",getline(&i));
    	return 0;
    }
    
    int getline(int *pn)
    {
    	int c,sign;
    
    	while(isspace(c=getch()))
    		;
    	if(!isdigit(c) && c!=EOF && c!='+' && c!='-')
    	{
    		ungetch(c);
    		return 0;
    	}
    	sign=(c=='-') ? -1:1;
    	if(c== '+' || c=='-')
    		c=getch();
    	for(*pn =0;isdigit(c);c=getch())
    		*pn=10 * *pn +(c- '0');
    	*pn *=sign;
    	if(c !=EOF)
    		ungetch(c);
    	return c;
    }
    #define BUFSIZE 100
    char buf[BUFSIZE];
    int bufp=0;
    
    int getch(void)
    {
    	return (bufp >0) ? buf[--bufp] :getchar();
    }
    void ungetch(int c)
    {
    	if(bufp >=BUFSIZE)
    		printf("ungetch:too many characters\n");
    	else
    		buf[bufp++]=c;
    }
    guys getint function performs free-format input conversion by breaking stream of characters into integer values,one integer per call.....but m always getting the output as 10....for all the inputs...cal u help..
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    the 10 is your enter key.

    getines needs an array and not just a single int.

    Comment

    Working...