-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory_prac.c
65 lines (54 loc) · 1.22 KB
/
memory_prac.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <ctype.h>
int x = 1, y = 2, z[10];
int *ip; /* ip is a pointer to int */
ip = &x; /* ip now points to &x */
y = *ip; /* y is now 1 */
*ip = 0; /* x is now 0 */
/* every pointer points to a specific data type */
/* a swapping routine is a good example of why pointers are important */
void swap(int x, int y){
int temp;
temp = x;
x = y;
y = temp;
}
/* this is a call by value, which C does not allow */
void swap(int *px, int *py)
{
int temp;
temp = *px;
*px = *py;
*py = temp;
}
int getch(void);
void ungetch(int);
/* getint: get next integer from input to *pn */
int getint(int *pn)
{
int c, sign;
while (isspace(c = getch()); /*this skips white space */
if (!isdigit(c) && c != EOF && c != '+' && c !='-'){
ungetch(c); /*its not a number....*/
return 0;
}
}
int getint(int *pn)
{
int c, sign;
while (isspace(c = getch()));
if (!isdigitc(c))&& c != EOF && c! = '+' && c != '-')
{
ungetch(c);
return 0; /* its not a number */
}
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;
}