11. ポインタの応用と構造体#
Source code#
ポインタ引数で値を戻す
#include <stdio.h>
void func(int *dt);
int main(void)
{
int nn;
nn = 1234;
printf("main: nn=%d\n", nn);
func(&nn);
printf("main: nn=%d\n", nn);
return 0;
}
void func(int *dt)
{
*dt = 5678;
printf("func: *dt=%d\n", *dt);
}
ポインタで配列を関数に渡す
#include <stdio.h>
void func1(int d[], int len)
{
int i;
for (i = 0; i < len; i++)
printf("%d ", d[i]);
printf("\n");
}
void func2(int *d, int len)
{
int i;
for (i = 0; i < len; i++)
printf("%d ", *(d + i));
printf("\n");
}
int main(void)
{
int nn[4] = {10, 20, 30, 40};
func1(nn, 4);
func2(nn, 4);
return 0;
}
ポインタで文字列を関数に渡す
#include <stdio.h>
void strout_p(char *ss);
int main(void)
{
char st[] = "ABCDEF";
strout_p(st);
return 0;
}
void strout_p(char *pp)
{
printf("pp=%s\n", pp);
while (*pp)
{ /* *pp!='\0'でも同じ */
printf("%X ", *pp);
++pp;
}
printf("\n");
}
Problem#
Problem 1. 下線部(4カ所)を埋めて,プログラムを完成させよ.
#include <stdio.h>
// void cmp(char ____, int *r)
{
char *s2 = "microwave oven\n";
// *r = ____;
while (*s1 || *s2)
{
if (*s1 != *s2)
return;
// ____++;
s2++;
}
*r = 0;
}
int main(void)
{
char s[80];
int result;
fgets(s, 80, stdin);
// cmp(s, ____);
if (result == 1)
printf("No\n");
else
printf("Yes\n");
return 0;
}
Problem 2. 文字列を渡すと逆順にする関数reverse()を作れ.
#include <stdio.h>
#include <string.h> /*for strlen()*/
void reverse(char *s);
int main(void)
{
}
void reverse(char *s)
{
int wk, n1 = 0, n2;
n2 = strlen(s) - 1;
while (n1 < n2)
{
wk = *(s + n1);
*(s + n1) = *(s + n2);
*(s + n2) = wk;
++n1;
--n2;
}
}
Problem 3. 文字列と繰り返す文字を,渡した文字数分,入力した文字列を繰り返し表示する関数を作れ.
#include <stdio.h>
void repeat(char *strp, int n);
int main(void)
{
}
void repeat(char *strp, int n)
{
int i;
char *p = strp;
for (i = 0; i < n; i++)
{
if (*strp == '\0')
strp = p;
printf("%c", *(strp++));
}
printf("\n");
}
Problem 4. 下線部を埋めて,プログラムを完成させよ.
#include <string.h>
struct myst
{
char name[40];
int age;
};
int main(void)
{
struct ____ Taro, Hanako;
strcpy(______, "Tanaka Taro");
___ = 18;
strcpy(______, "Yamada Hanako");
______ = 16;
printf("%s is %d years old.\n", ______, ______);
printf("%s is %d years old.\n", ______, ______);
return 0;
}
Solutions#
#include <stdio.h>
void cmp(char *s1, int *r)
{
char *s2 = "microwave oven\n";
*r = 1;
while (*s1 || *s2)
{
if (*s1 != *s2)
return;
s1++;
s2++;
}
*r = 0;
}
int main(void)
{
char s[80];
int result;
fgets(s, 80, stdin);
cmp(s, &result);
if (result == 1)
printf("No\n");
else
printf("Yes\n");
return 0;
}
#include <stdio.h>
#include <string.h> /*for strlen()*/
void reverse(char *s);
int main(void)
{
char str[] = "program";
reverse(str);
printf("str=%s\n", str);
return 0;
}
void reverse(char *s)
{
int wk, n1 = 0, n2;
n2 = strlen(s) - 1;
while (n1 < n2)
{
wk = *(s + n1);
*(s + n1) = *(s + n2);
*(s + n2) = wk;
++n1;
--n2;
}
}
#include <stdio.h>
void repeat(char *strp, int n);
int main(void)
{
char str[100];
printf("Input a string:");
scanf("%s", str);
repeat(str, 20);
return 0;
}
void repeat(char *strp, int n)
{
int i;
char *p = strp;
for (i = 0; i < n; i++)
{
if (*strp == '\0')
strp = p;
printf("%c", *(strp++));
}
printf("\n");
}
#include <stdio.h>
#include <string.h>
struct myst
{
char name[40];
int age;
};
int main(void)
{
struct myst Taro, Hanako;
strcpy(Taro.name, "Tanaka Taro");
Taro.age = 18;
strcpy(Hanako.name, "Yamada Hanako");
Hanako.age = 16;
printf("%s is %d years old.\n", Taro.name, Taro.age);
printf("%s is %d years old.\n", Hanako.name, Hanako.age);
return 0;
}