# Pointer

## Exercise 1

Below is a source code to show size of pointer variable. Write the source code and save it as "pointer1.c". After compilation, confirm by executing it.

```#include <stdio.h>

int main(void)
{
char    a, *pa;
int     b, *pb;
float   c, *pc;
double  d, *pd;

printf("size of a  = %dbyte\n", sizeof(a));
printf("size of pa = %dbyte\n\n", sizeof(pa));

printf("size of b  = %dbyte\n", sizeof(b));
printf("size of pb = %dbyte\n\n", sizeof(pb));

printf("size of c  = %dbyte\n", sizeof(c));
printf("size of pc = %dbyte\n\n", sizeof(pc));

printf("size of d  = %dbyte\n", sizeof(d));
printf("size of pd = %dbyte\n\n", sizeof(pd));

return (0);
}
```
```(Execution result)
\$ ./a.out
size of a  = 1byte
size of pa = 4byte

size of b  = 4byte
size of pb = 4byte

size of c  = 4byte
size of pc = 4byte

size of d  = 8byte
size of pd = 4byte
\$
```

## Exercise 2

Write a below source code and save it as "pointer2.c". Throught the execution of it, understand function of pointer variable.

```#include <stdio.h>

int main(void)
{
int a, b;
int *p;

a=100;
p=&a;
b=*p;
printf("Pointer 'p' is referring to address of variable 'a'\n");
printf("Address: &a=%X, &b=%X, p=%X\n", &a, &b, p);
printf("Value:      a=%d, b=%d, *p=%d\n\n", a, b, *p);

a=200;
b=300;
printf("Pointer 'p' is referring to address of variable 'a'\n");
printf("Address: &a=%X, &b=%X, p=%X\n", &a, &b, p);
printf("Value:      a=%d, b=%d, *p=%d\n\n", a, b, *p);

p=&b;
printf("Pointer 'p' is referring to address of variable 'b'\n");
printf("Address: &a=%X, &b=%X, p=%X\n", &a, &b, p);
printf("Value:      a=%d, b=%d, *p=%d\n\n", a, b, *p);

return (0);

}
```
```(Execution example)
\$ ./a.out
Pointer 'p' is referring to address of variable 'a'
Value:    a=100, b=100, *p=100

Pointer 'p' is referring to address of variable 'a'
Value:    a=200, b=300, *p=200

Pointer 'p' is referring to address of variable 'b'
Value:    a=200, b=300, *p=300

\$
```

## Exercise 3

Write a below source code and save it as "pointer3.c". Throught the execution of it, understand function of pointer variable.

```#include <stdio.h>

int main(void)
{
int  i;
char name[]="JERS-1/SAR";
char *p;

p=name;
printf("***** case 1 *****\n");
printf("Value:   name=%s, p=%s\n\n", name, p);

printf("***** case 2 *****\n");
putchar(*p);
putchar(*(p+1));
putchar(*(p+2));
putchar(*(p+3));
putchar(*(p+4));
putchar(*(p+5));
putchar(*(p+6));
printf("\n\n");

printf("***** case 3 *****\n");
i=0;
while (name[i]){
name[i] = name[i]+1;
i++;
}
printf("Value:   name=%s, p=%s\n", name, p);
printf("String length = %d\n\n", i);

printf("***** case 4 *****\n");
while (*p){
*p = *p-1;
++p;
}
printf("Value:   name=%s, p=%s\n\n", name, p);
printf("Value:   name=%s, p-%d=%s\n\n", name, i, p-i);

return (0);

}
```
```(Execution example)
\$ ./a.out
***** case 1 *****
Value:   name=JERS-1/SAR, p=JERS-1/SAR

***** case 2 *****
JERS-1/

***** case 3 *****
Value:   name=KFST.20TBS, p=KFST.20TBS
String length = 10

***** case 4 *****
Value:   name=JERS-1/SAR, p=
Value:   name=JERS-1/SAR, p-10=JERS-1/SAR

\$
```

## Exercise 4

Below is a source code to show each character of string. Write the source code and save it as "pointer4.c". Compare this source code with 'Exercise 7 in Array' by executing it.

```#include <stdio.h>

int main(void)
{
char name[] = "JERS-1/SAR";
char *p;

for(p=name; *p != '\0'; p++){
printf("%c\n", *p);
}

return (0);

}
```
```(Execution result)
\$ ./a.out
J
E
R
S
-
1
/
S
A
R
```

## Exercise 5

Below is a source code to show the length of string. Write the source code and save it as "pointer5.c". Compare this source code with 'Exercise 8 in Array' by executing it.

```#include <stdio.h>

int main(void)
{
char name[] = "JERS-1/SAR";
char *p;
int n=0;

for(p=name; *p != '\0'; p++) n++;

printf("n = %d\n", n);

return (0);
}
```

## Exercise 6

We want to show string repeatedly by shifting top character of string to right. Refer to a below execution example. Write a below source code, complement the expression hidden by "******" to meet this requirement, and save the source code as "pointer6.c". Confirm whether it can execute properly.

```#include <stdio.h>

int main(void)
{
char  str, *p;

printf("Input about 10 characters of string\n");
scanf("%s",str);

// Pointer variable refers to address of string
*****************************
*****************************

// Show string by shifting top character of string to right
*****************************
*****************************
*****************************
*****************************

return (0);
}
```
```(Execution example)
\$ ./a.out
Input about 10 characters of string
abcd1234EFGH
abcd1234EFGH
bcd1234EFGH
cd1234EFGH
d1234EFGH
1234EFGH
234EFGH
34EFGH
4EFGH
EFGH
FGH
GH
H

\$
```

## Exercise 7

Contrary to Exercise 6, here, we want to show string repeatedly by shifting top character of string from right to left. Refer to a below execution example. Write a below source code, complement the expression hidden by "******" to meet this requirement, and save the source code as "pointer7.c". Confirm whether it can execute properly.

```#include <stdio.h>

int main(void)
{
char  str, *p;

printf("Input about 10 characters of string\n");
scanf("%s",str);

// Pointer variable refers to address of string
*****************************
*****************************

// Show string by shifting top character of string from right to left
*****************************
*****************************
*****************************
*****************************

return (0);
}
```
```(Execution example)
\$ ./a.out
Input about 10 characters of string
abcd1234EFGH
H
GH
FGH
EFGH
4EFGH
34EFGH
234EFGH
1234EFGH
d1234EFGH
cd1234EFGH
bcd1234EFGH
abcd1234EFGH

\$
```

Lessons for C programming
Junichi Susaki, Kyoto University