char message[] = "? ? ? everyone!";
replaceQ(&message[0]);
または、
char message[] = "? ? ? everyone!";
replaceQ(message);
または、
replaceQ("? ? ? everyone!");
のどれでも良い。他の文字列関数の例
#include <iostream.h>
int strlen(char *str) { /* 文字列の長さ */
int n = 0;
while(*str) {
n++;
str++;
}
return n;
}
void toUpper(char *str) {
while (*str) {
*str -= 32;
str++;
}
}
int main() {
char h[] = "hello";
cout << strlen(h) << "\n";
toUpper(h);
cout << h << "\n";
}
実行結果は
5 HELLOとなる。
graphfun.c,
ポインタのインクリメント(2)
char型と int型ではサイズが違う。
int data[] = {1, 9, 2, 7, 4};
int i;
bubble(data, 5)
/* この時点で data[]は {1, 2, 4, 7, 9}になっている。
main() {
double *px1, *px2;
niji(1, -2, 1, px1, px2); /* 間違い! */
...
}
double* niji(double a, double b, double c) {
double ans[2];
...
ans[0] = (-b+sd)/(2*a);
ans[1] = (-b-sd)/(2*a);
return ans; /* 間違い! */
}
// 構造体の宣言
struct 構造体名 {
型 変数名;
...
型 変数名;
};
// 変数の宣言
struct 構造体名 変数名;
point.h,
point.c
その他の構造体の例
struct person {
int ID;
char name[32];
int salary;
double height;
double weight;
};
ptr = (type*) malloc(sizeof(type)); ptr = (type*) malloc(n*sizeof(type));は、C++では
ptr = new type; ptr = new type[n];と書くことができる。