-やりたいことのイメージ-
配列a[2][5]={ { 1, 2, 3, 4, 5 },
{ 6, 7, 8, 9, 10 }}
配列b[5]= {11,,12, 13, 14, 15}
---配列a[0][0]のアドレスを配列bのアドレスに置き換える---
配列a[2][5]={ { 11, 12, 13, 14, 15 },
{ 6, 7, 8, 9, 10 }}
自分の限界
/*Main.cpp*/
#include <stdio.h>
#include "Header.h"
int main(void){
int a[][5] =
{
{ 1, 2, 3, 4, 5 },
{ 6, 7, 8, 9, 10 }
};
// aのポインタと中身を表示
printf("aのアドレス%d\n", a);
printf("a=");
for (int i = 0; i < 2; i++){
for (int j = 0; j < 5; j++){
printf("%d, ", a[i][j]);
}
printf("\n");
}
// bの中身を出力
output();
printf("\n配列aの先頭アドレスを配列bの先頭アドレスで書き換える\n\n");
// 配列aのポインタを投げる
setPointer(a);
printf("a=");
for (int i = 0; i < 2; i++){
for (int j = 0; j < 5; j++){
printf("%d, ", a[i][j]);
}
printf("\n");
}
return 0;
}
/*Sub.cpp*/
#include <stdio.h>
int *pointer;
int b[] = {11,12,13,14,15};
void setPointer(int p[2][5]){
p[0][0] = b[0];
p[0][1] = b[1];
p[0][2] = b[2];
p[0][3] = b[3];
p[0][4] = b[4];
}
void output(){
printf("bのアドレス = %d\n", b);
printf("b=");
for (int i = 0; i < 5; i++){
printf("%d, ", b[i]);
}
printf("\n");
}
ポインターでポンと代入できた方が,見た目がスッキリして,もっと大きな配列を扱う場合でも処理が重くならないし,bの値を書き換えるたびに代入する必要がなくなる思うので,いいと思うのですが・・・.