Instructor: Professor Hong
#include <stdio.h>
int main()
{
int x=1, y=2, z[10];
int *ip; // ip is a pointer to an int
ip = &x; // ip now points to x
printf("ip=%x\n", ip); // prints address of x
y = *ip; // y is now 1
printf("y=%d\n", y);
*ip = 0; // x is now 0
printf("x=%d\n", x);
ip = &z[0]; // ip now points to z[0]
printf("ip=%x\n", ip);
return 0;
}
#include <stdio.h>
int main()
{
char *ptr = NULL; // Null pointer
printf("%c", *ptr);
return 0;
}
#include <stdio.h>
void printArray(int arr[])
{
for (int i=0; i<10; i++)
printf("%d ", arr[i]);
printf("\n");
}
void printArrayAddress(int arr[])
{
for (int i=0; i<10; i++)
printf("%x ", &arr[i]);
printf("\n");
}
int main()
{
int intArr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
printArray(intArr);
printArrayAddress(intArr);
int *intPtr = intArr; // defaults to &intArr[0]
printf("intPtr=%x\n", intPtr);
printf("*intPtr=%d\n", *intPtr);
int *intPtr2 = &intArr[2]; // address of intArr[2]
printf("intPtr2=%x\n", intPtr2);
printf("*intPtr2=%d\n", *intPtr2);
printf("\n");
*intPtr2 = *intPtr2 + 10; // adds 10 to arr[2]
printf("intPtr2=%x\n", intPtr2);
printf("*intPtr=%d\n", *intPtr2);
int y;
y = *intPtr2 + 1; // gets arr[2] and adds 1
printf("y=%d\n", y);
*intPtr2 += 10; // adds 10 to arr[2]
y = *intPtr2;
printf("y=%d\n", y);
++*intPtr2; // adds 1 to arr[2]
y = *intPtr2;
printf("y=%d\n", y);
(*intPtr2)++; // adds 1 to arr[2]
y = *intPtr2;
printf("y=%d\n", y);
*intPtr2++; // increments pointer to arr[3]! That's it!
y = *intPtr2;
printf("y=%d\n", y);
return 0;
}
#include <stdio.h>
// K&R Pg. 95
void swap(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
}
int main()
{
int x = 1;
int y = 2;
printf("before: x=%d, y=%d\n", x, y);
swap(x, y);
printf("after: x=%d, y=%d\n", x, y);
}
#include <stdio.h>
// K&R Pg. 96
void swap(int *px, int *py)
{
int temp;
temp = *px;
*px = *py;
*py = temp;
}
int main()
{
int x = 1;
int y = 2;
printf("before: x=%d, y=%d\n", x, y);
swap(&x, &y);
printf("after: x=%d, y=%d\n", x, y);
}
#include <stdio.h>
#include <ctype.h>
// K&R Pg. 97
int getch();
void ungetch(int);
#define BUFSIZE 100
char buf[BUFSIZE]; // buffer for ungetch
int bufp = 0; // next free position in buf
int getch(void) // get a (possibly pushed back) charater
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c) // push character back on input
{
if (bufp >= BUFSIZE)
printf("ungetch:too many characters\n");
else
buf[bufp++] = c;
}
// getint: get next integer from input into *pn
int getint(int *pn)
{
int c, sign;
while (isspace(c = getch())) // skip white space
;
if (!isdigit(c) && c != EOF && c != '+' && c != '-')
{
ungetch(c); // it's not a number
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;
}
int main()
{
int c, pn;
c = getint(&pn);
printf("c=%d pn=%d", c, pn);
return 0;
}
#include <stdio.h>
// K&R Pg. 99
// strlen:return length of string s
int strlen(char *s)
{
int n;
for (n=0; *s != '\0'; s++)
n++;
return n;
}
int main()
{
char str[] = "ABCDEFG";
printf("strlen=%d\n", strlen(str));
return 0;
}
#include <stdio.h>
// K&R Pg. 103
// strlen:return length of string s
int strlen(char *s)
{
char *p = s;
while (*p != '\0')
p++;
return p - s;
}
int main()
{
char str[] = "ABCDEFG";
printf("strlen=%d\n", strlen(str));
return 0;
}
#include <stdio.h>
// K&R Pg. 101
#define ALLOCSIZE 10000 // size of available space
static char allocbuf[ALLOCSIZE]; // storage for alloc
static char *allocp = allocbuf; // next free position
char *alloc(int n) // return pointer to n characters
{
if (allocbuf + ALLOCSIZE - allocp >= n) // it fits
{
allocp += n;
return allocp - n; // old p
}
else // not enough room
return 0;
}
void afree(char *p) // free storage pointed to by p
{
if (p >= allocbuf && p < allocbuf + ALLOCSIZE)
allocp = p;
}
int main()
{
char *spacePtr;
int n = 10;
spacePtr = alloc(n);
printf("spacePtr=%x, allocp=%x\n", spacePtr, allocp);
char *spacePtr2;
spacePtr2 = alloc(n);
printf("spacePtr2=%x, allocp=%x\n", spacePtr2, allocp);
afree(spacePtr2);
printf("spacePtr2=%x, allocp=%x\n", spacePtr2, allocp);
afree(spacePtr);
printf("spacePtr=%x, allocp=%x\n", spacePtr, allocp);
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char amessage[] = "now is the time"; // an array
char *pmessage = "now is the time"; // a pointer
printf("amessage=%s\n",amessage);
printf("pmessage=%x\n",pmessage);
printf("pmessage=");
for(; *pmessage!='\0'; ++pmessage)
printf("%c",*pmessage);
}
#include <stdio.h>
// K&R pg. 105
// strcpy: copy t to s; array subscript version
void strcpy(char *s, char *t)
{
int i;
i = 0;
while ((s[i] = t[i]) != '\0')
i++;
}
int main()
{
char str[10] = "My string";
char newStr[10];
strcpy(newStr, str);
printf("str=%s\n", str);
printf("newStr=%s\n", newStr);
return 0;
}
#include <stdio.h
// K&R pg. 105
// strcpy: copy t to s; poitner version 1
void strcpy(char *s, char *t)
{
while ((*s = *t) != '\0') {
s++;
t++;
}
}
int main()
{
char str[10] = "My string";
char newStr[10];
strcpy(newStr, str);
printf("str=%s\n", str);
printf("newStr=%s\n", newStr);
return 0;
}
#include <stdio.h>
// K&R pg. 106
// strcmp: return <0 if s<t, 0 if s==t, >0 if s>t
int strcmp(char *s, char *t)
{
int i;
for (i = 0; s[i] == t[i]; i++)
if (s[i] == '\0')
return 0;
return s[i] - t[i];
}
int main()
{
char s[] = "ABC";
char t[] = "ABC";
printf("%d", strcmp(s,t));
return 0;
}
#include <stdio.h>
// K&R pg. 106
// strcmp: return <0 if s<t, 0 if s==t, >0 if s>t
int strcmp(char *s, char *t)
{
for ( ; *s == *t; s++, t++)
if (*s == '\0')
return 0;
return *s - *t;
}
int main()
{
char s[] = "ABC";
char t[] = "ABC";
printf("%d", strcmp(s,t));
return 0;
}
#include <stdio.h>
// K&R pg. 111
static char daytab[2][13] = {
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
// day of year: set day of year from month & day
int day_of_year(int year, int month, int day)
{
int i, leap;
leap = year%4 == year%100 != 0 || year%400 == 0;
for (i = 1; i < month; i++)
day += daytab[leap][i];
return day;
}
// month_day: set month, day from day of year
void month_day(int year, int yearday, int *pmonth, int *pday)
{
int i, leap;
leap = year%4 == 0 && year%100 != 0 || year%400 == 0;
for (i = 1; yearday > daytab[leap][i]; i++)
yearday -= daytab[leap][i];
*pmonth = i;
*pday = yearday;
}
int main()
{
int day;
day = day_of_year(2018, 2, 15);
printf("day=%d\n", day);
int pmonth, pday;
month_day(2018, 46, &pmonth, &pday);
printf("pmonth=%d, pday=%d", pmonth, pday);
return 0;
}
#include <stdio.h>
// K&R pg. 113
// month_name: return name of n-th month
char *month_name(int n)
{
static char *name[] = {
"Illegal month",
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};
return (n < 1 || n > 12) ? name[0] : name[n];
}
int main()
{
char *name = month_name(2);
printf("name=%s", name);
return 0;
}
https://classroom.github.com/g/A0XqQ4C_
due 10/27 @ 11:59PM