8-2. 関数とポインタポインタ[文法] ・形式 アドレス演算子&: &識別子 例 &a aのアドレスを得る ・形式 間接演算子*; *識別子 例 int *a; aはintへのポインタ |
|||||||
|
|||||||
#include<stdio.h> int main(void){ int x=1; int y=2; int *ip; printf("xのアドレス:%p\n", &x); ip = &x; printf("x:%p\n", *ip); y=*ip; *ip = 0; printf("x:%d, y:%d", x, y); return 0; } |
|||||||
|
|||||||
ポインタを引き数とする関数 例. 前回の関数Subssをポインタを引き数とする関数に変更する |
|||||||
|
|||||||
#include<stdio.h> void Subss(int *z, int *x, int *y){ int a = *x; int b = *y; *z = a*a + b*b; } int main(void){ int a, b, c, d, e, f, g; printf("Input b, c, e, f"); scanf("%d %d %d %d", &b, &c, &e, &f); Subss( &a, &b, &c ); Subss( &d, &e, &f ); g = a+d; printf("g=%d\n", g); } |
|||||||
|
|||||||
文字列 文字列は、文字の並びを表現する 末尾は、ナル文字 '\0'である 例。文字列の初期化と表示 char str[] = { 'A', 'B', 'C', '\n'} char str[] = "ABC" printf("%s", str); /* 表示 */ |
|||||||
|
|||||||
#include<stdio.h> int main(void){ char name[50]; printf("Name:"); scanf("%s", name); printf("Your name is %s", name); return 0; } |
|||||||
|
|||||||
文字列(配列)と関数 int a[5] = { 0, 1, 2, 3, 4} int *pa; pa = &a[0]
|
|||||||
|
|||||||
#include<stdio.h> int str_len(char *s){ int n; for(n=0; *s='\0'; s++) n++; return n } int main(void){ int n; char str[50]; scanf("%s", str); n = str_len(str); printf("String Length = %d", n); return 0; } |