Line 7: | Line 7: | ||
Lecture 2: | Lecture 2: | ||
− | [[Image: | + | [[Image:Yuanhua 2-1.png|500x700px]] |
− | [[Image: | + | [[Image:Yuanhua 2-2.png|500x700px]] |
+ | |||
+ | = '''Lecture (1-26-2011):'''<br> = | ||
+ | |||
+ | #include <stdio.h> | ||
+ | |||
+ | File *fhd;<br>int numRow=0;<br>int numCol=0;<br>int col = 0;<br>int ch; | ||
+ | |||
+ | |||
+ | |||
+ | if (arg<2)<br>{<br> /*error*/<br>} | ||
+ | |||
+ | fhd = fopen(argv[1]."r");<br>if(fhd==0)<br>{<br> /*error*/<br>}<br> while(!feof(fhd))<br> {<br> ch = fgetc(fhd);<br> if(ch=='\n')<br> {<br> numRow++;<br> }<br> if(num Col<col)<br> {<br> numCol = col;<br> col = 0;<br> }<br> col++;<br> }<br>printf("width=%d,height=%d\n",numCol,numRow); | ||
+ | |||
+ | gcc prog.c -o prog | ||
+ | |||
+ | We need to know the enviornment and the direction we are going to take:<br>Need to have a way to remember where we are located: | ||
+ | |||
+ | Pointers:<br>int x;<br>x=5 | ||
+ | |||
+ | Value ---> 800<br>address 650y | ||
+ | |||
+ | int *y;<br>/* y is a pointer to integer*/<br>y = 'x'; <-- warning message<br>take x's addr | ||
+ | |||
+ | *y = LHS left hand side take y's value as an adr, modify the value at that addr. | ||
+ | |||
+ | = *y Right hand side (RHS) ---> Means assign <br>Take y's value as an addr, read from that addr. | ||
+ | |||
+ | *y = 50;<br>y = &z;<br>*y = 35; ---> z = 35 | ||
+ | |||
+ | Goo mainstreet ---> address<br>family Johnson ----> value | ||
+ | |||
+ | int a[10];<br> ^-- 3<br>if(ch=='A')<br> ^-- 65 ascII | ||
+ | |||
+ | memory<br>one - dimension array<br>int *a;<br>a = malloc(numElem *sizeof(int));<br>^---a's value is the start addr of a piece of memory <br>a[0] = ____<br>x = a [0];<br>.<br>.<br>.<br>free(a); <--- lose 50 points RELEASE THE MEMORY (Memory Leak if not there) | ||
+ | |||
+ | Stack Heap<br>a 1000 | ||
+ | |||
+ | C does not specify sizeof(int)<br>Portable: Run on different machine | ||
+ | |||
+ | malloc(#elem*sizeof(one elem));<br>^--- modify the value at that addr | ||
+ | |||
+ | if(a==0)<br>{<br> /* malloc fails */<br> //if malloc fails a is 0<br>} | ||
+ | |||
+ | you already know the size of the maze. | ||
+ | |||
+ | int array[numRow]; The program does not know when you compile the program!<br> |
Revision as of 20:49, 30 April 2011
Lecture 1:
Lecture 2:
Lecture (1-26-2011):
- include <stdio.h>
File *fhd;
int numRow=0;
int numCol=0;
int col = 0;
int ch;
if (arg<2)
{
/*error*/
}
fhd = fopen(argv[1]."r");
if(fhd==0)
{
/*error*/
}
while(!feof(fhd))
{
ch = fgetc(fhd);
if(ch=='\n')
{
numRow++;
}
if(num Col<col)
{
numCol = col;
col = 0;
}
col++;
}
printf("width=%d,height=%d\n",numCol,numRow);
gcc prog.c -o prog
We need to know the enviornment and the direction we are going to take:
Need to have a way to remember where we are located:
Pointers:
int x;
x=5
Value ---> 800
address 650y
int *y;
/* y is a pointer to integer*/
y = 'x'; <-- warning message
take x's addr
- y = LHS left hand side take y's value as an adr, modify the value at that addr.
= *y Right hand side (RHS) ---> Means assign
Take y's value as an addr, read from that addr.
- y = 50;
y = &z;
*y = 35; ---> z = 35
Goo mainstreet ---> address
family Johnson ----> value
int a[10];
^-- 3
if(ch=='A')
^-- 65 ascII
memory
one - dimension array
int *a;
a = malloc(numElem *sizeof(int));
^---a's value is the start addr of a piece of memory
a[0] = ____
x = a [0];
.
.
.
free(a); <--- lose 50 points RELEASE THE MEMORY (Memory Leak if not there)
Stack Heap
a 1000
C does not specify sizeof(int)
Portable: Run on different machine
malloc(#elem*sizeof(one elem));
^--- modify the value at that addr
if(a==0)
{
/* malloc fails */
//if malloc fails a is 0
}
you already know the size of the maze.
int array[numRow]; The program does not know when you compile the program!