
이번 글을 통해 배워갈 내용
- 배열을 함수에 인자로 넣는 방법에 대해서 알아보겠습니다.
여기 배열이 있습니다.
(배열을 선언하고 1,2,3,4,5를 넣어줍니다)
int myArr[] = { 1, 2, 3, 4, 5 };
배열을 출력해보겠습니다.
for (int x : myArr)
cout << x << endl;
결과
1
2
3
4
5
그렇다면 여기서 문제 들어갑니다
#include <iostream>
using namespace std;
int main()
{
int myArr[] = { 1, 2, 3, 4, 5 };
myDoubleFunc(myArr);
for (int x : myArr)
cout << x << endl;
return 0;
}
myDoubleFunc라는 함수를 만들어서 배열안에 각 원소들을 두배해봅시다.
배열을 인자로 전달하는 방법을 모르신다면 문제를 풀기전에
아래 글을 한번 읽어보시길 바랍니다.
중요한 내용입니다
배열은 pass by address로 값을 전달한다!
pass by address를 모르신다면 아래 글을 참조해주시면 됩니다
https://codemasterkimc.tistory.com/15
과학적으로 배우는 pass by value, pass by reference, pass by address
이번 글을 통해 배워갈 내용 값에 의한 전달 (pass by value) 주소에 의한 전달 (pass by address) 참조에 의한 전달 (pass by reference) 값에 의한 전달 (pass by value), 주소에 의한 전달 (pass by address),..
codemasterkimc.tistory.com
주소로 배열을 전달하는 것에 대해 이해 하셨고 위에 문제를 아직 못푸셨다면
예시 1과 예시 2를 타이핑 하신뒤 위에 문제를 다시 풀어보시길 바랍니다.
예시1과 예시2를 보시면
myArr[] 라는 인자와 *myArr라는 인자로 배열의 2번째 값과 3번째 값을 15와 1로 변경하는 것을 알수 있습니다.
예시1
void myFunc1(int myArr[])
{
myArr[1] = 15;
myArr[2] = 1;
}
int main()
{
int myArr[3] = {0,1,2};
myFunc1(myArr);
for(int x:(myArr))
cout<<x;
}
예시2
void myFunc2(int *myArr)
{
myArr[1] = 15;
myArr[2] = 1;
}
int main()
{
int myArr[3] = {0,1,2};
myFunc2(myArr);
for(int x:(myArr))
cout<<x;
}
결과
15 1 2
그렇다면 문제는 어떻게 풀수 있을까요?
"Q : myDoubleFunc라는 함수를 만들어서 배열안에 각 원소들을 두배해봅시다."
저는 아래와 같이 풀었습니다.
#include <iostream>
using namespace std;
void myDoubleFunc(int myArr[])
{
for (int i = 0; i < 5; i++)
{
myArr[i] *= 2;
}
}
int main()
{
int myArr[] = { 1, 2, 3, 4, 5 };
myDoubleFunc(myArr);
for (int x : myArr)
cout << x << endl;
return 0;
}
풀고나니까 길이를 미리 구해서 넣는 방법이 생각나더라구요
#include <iostream>
using namespace std;
void myDoubleFunc(int myArr[], int length)
{
for (int i = 0; i < length; i++)
{
myArr[i] *= 2;
}
}
int main()
{
int myArr[] = { 1, 2, 3, 4, 5 };
int myArrLength = (sizeof(myArr) / sizeof(*myArr));
myDoubleFunc(myArr, myArrLength);
for (int x : myArr)
cout << x << endl;
return 0;
}
역시 세상만사에 정답은 없고 해답만 있다는 생각이 듭니다.
여러분들은 어떻게 푸셨나요?
아래에 댓글로 알려주세요~ :)
오늘도 즐거운 코딩하시길 바랍니다 ~ :)
참조 및 인용
https://stackoverflow.com/questions/14309136/passing-arrays-to-function-in-c
Passing Arrays to Function in C++
#include using namespace std; void printarray (int arg[], int length) { for (int n = 0; n < length; n++) { cout << arg[n] << " "; cout << ...
stackoverflow.com
https://www.geeksforgeeks.org/how-arrays-are-passed-to-functions-in-cc/
How arrays are passed to functions in C/C++ - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
www.geeksforgeeks.org
https://stackoverflow.com/questions/4108313/how-do-i-find-the-length-of-an-array
How do I find the length of an array?
Is there a way to find how many values an array has? Detecting whether or not I've reached the end of an array would also work.
stackoverflow.com
'C++ > C++ 기타' 카테고리의 다른 글
| C++ 실행중에 이중배열 크기를 정의 하고 사용하는 한가지 방법 (1) | 2021.07.02 |
|---|---|
| C++ 동적으로 배열 크기를 늘리는 세가지 방법 (1) | 2021.06.21 |
| 과학적으로 배우는 pass by value, pass by reference, pass by address (1) | 2021.06.15 |
| C++ 과 C에서 화살표 함수 -> 와 .의 차이에 대해서 알아보겠습니다 (1) | 2021.06.09 |
| Visual Studio Install 비쥬얼 스튜디오 설치하는 법 (1) | 2021.06.04 |