## Discussion 2

9/3/02 Tue

### Introduction of pointers

#### Pointer variables point to the address on which a variable resides. The address a pointer variable points to can be set by initialization or assignment. A pointer variable can take the address of a variable by using the 'address of (&)' operator. The value of memory address a pointer variable points to can be evaluated using the 'derefence (*)' operator.

```int main()
{
int x = 0, y = 1;
int *ptx = &x;	/* pointer variables are set at initialization */
int *pty;
printf("x = %d, *ptx = %d\n", x, *ptx);
x++;
printf("After increment, x = %d, *ptx = %d\n", x, *ptx);
pty = &y;
printf("y = %d, *pty = %d\n", y, *pty);
*pty += 3;
printf("After adding 3, y = %d, *pty = %d\n", y, *pty);

return 0;
}```

#### Result:

```> ./pointer1
x = 0, *ptx = 0
After increment, x = 1, *ptx = 1
y = 1, *pty = 1
After adding 3, y = 4, *pty = 4
```

### Pointer and array

#### When an array variable is defined, the space for array entries are reserved and the name of the array points to the address of the first entry of the array. Actually the name of an array is a pointer variable and the address it points to is constant.

```int main()
{
int a[5] = {0, 1, 2, 3, 4};
int i;
int *p;
for (i = 0; i < 5; i++) {
printf("a[%d]: %d\n", i, a[i]);
}
p = &a[0];
printf("a[0] : %d\n", *p);
p = a;
printf("a[0] : %d\n", *p);

return 0;
}```

#### Result:

```> ./pointer1
a[0]: 0
a[1]: 1
a[2]: 2
a[3]: 3
a[4]: 4
a[0] : 0
a[0] : 0
```

### Pointer arithmetic

#### Pointer variables can be used in arithmetic, but the kind of operations allowed is restricted compared to integer variables.

```int main()
{
int a[5] = {0, 1, 2, 3, 4};
int i;
int *p;
for (p = a, i = 0; i < 5; p++, i++) {
printf("a[%d]: %d\n", i, *p);
}
p = a;
for (i = 0; i < 5; i++) {
printf("a[%d]: %d\n", i, *(p+i));
}
p = a;
for (i = 0; i < 5; i++) {
printf("a[%d]: %d\n", i, *p++);
}

return 0;
}```

#### Result:

```> ./pointer1
a[0]: 0
a[1]: 1
a[2]: 2
a[3]: 3
a[4]: 4
a[0]: 0
a[1]: 1
a[2]: 2
a[3]: 3
a[4]: 4
a[0]: 0
a[1]: 1
a[2]: 2
a[3]: 3
a[4]: 4
```

### Pointer and string

#### Unlike Java, C doesn't have a separate type for string. Instead, an array of 'char' is used to represent a string. Because there is no way of knowing the size of an array in C, a special character '\0' is used to represent the end of a string.

```int main()
{
char str1[] = "This is a test.";
char str2[] = { 'T', 'h', 'i', 's', ' ', 'i', 's',
' ', 'a', ' ', 't', 'e', 's', 't', '.' };
char str3[32];
char *p, *q;
int i;

printf("str1: %s\n", str1);
printf("str2: %s\n", str2);
for (p = str1, q = str3; *p != '\0'; p++, q++) {
*q = *p;
}
printf("str3: %s\n", str3);

return 0;
}```

#### Result:

```> ./point1
str1: This is a test.
str2: This is a test.
str3: This is a test.```

### Multidimensional array

#### Multidimensional array is represented with the multiple indirection.

```int main()
{
int a[5][5] = { {1, 0, 0, 0, 0},
{0, 1, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
{0, 0, 0, 0, 1}};
int *c[5];
int i, j;
int *p;
char *strArr[3] = { "This is a test",
"of multidimensional",
"array" };
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
printf("%d ", a[i][j]);
}
printf("\n");
}
for (i = 0; i < 5; i++) {
c[i] = a[i]
for (j = 0; j < 5; j++) {
printf("%d ", c[i][j]);
}
printf("\n");
}
for (i = 0; i < 3; i++) {
printf("%s\n", strArr[i]);
}

return 0;
}```

#### Result:

```> ./pointer1
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
This is a test
of multidimensional
array
```

### Pointer and function all

#### This is an example of how functions are called with pointer variables.

```double average(double arr[], int n)
{
int i;
double result = 0.0;
for (i = 0; i < n; i++) {
result += arr[i];
}
result = result / n;
return result;
}

void increment_int(int *p)
{
(*p)++;
}

int main()
{
double arr1[5] = { 3.54, 2.45, 1.2, 5.67, 8.8 };
int i, j;

printf("The average of ");
for (j = 0; j < 5; j++) {
printf("%lf ", arr1[j]);
}
printf(" is %lf\n", average(arr1, 5));

i = 3;
printf("i is %d\n", i);
increment_int(&i);
printf("After increment, i is %d\n", i);

return 0;
}```

#### Result:

```> ./pointer1
The average of 3.540000 2.450000 1.200000 5.670000 8.800000  is 4.332000
i is 3
After increment, i is 4
```

### Short quiz

#### 1. Write a code to find the length of a string.

```int strlen(char *str)
{
```
` `
`}`