首页 > 编程语言 > 详细

第五章 指针与数组

时间:2015-12-01 21:19:41      阅读:256      评论:0      收藏:0      [点我收藏+]

5.2 指针与函数参数

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
 
int getch(void);
void ungetch(int);
 
/*getint 函数:将输入中的下一个整型数赋值给*pn */
int getint(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;
}
 
int main()
{
    int n, array[10];
    for (n = 0; n < 10 && getint(&array[n]) != EOF; n++)
        ;
    for (n = 0; n < 10; n++)
        printf("%d\n", array[n]);
    return 0;
}

 

第五章 指针与数组

原文:http://www.cnblogs.com/try-again/p/5011214.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!