승한ì�´ 질문 페ì�´ì§€ ¶
~cpp
void Test(int * Ascores)
{
cout << "in function : ";
cout << sizeof(Ascores) << " " << sizeof(Ascores[0]) << endl;
}
void main()
{
int scores[4]={1,2,3,4};
cout<<"in main : ";
cout << sizeof(scores) << " " << sizeof(scores[0]) << endl;
Test(scores);
}
strlen 처럼 int 형 배열� 길�를 구하는 함수를 짜�중 �해 안�는 부분.
ë©”ì�¸ì—�서 들어간 sizeof(scores)는 ë°°ì—´ ì „ì²´ í�¬ê¸°ë¥¼ 리턴하는 반면ì—� 함수ì—� 들어간 sizeof(scores)는 int* 형ì�˜ í�¬ê¸°ë¥¼ 리턴한다.
ë©”ì�¸ì—�서 처럼 ì „ì²´ ë°°ì—´í�¬ê¸°ë§Œí�¼ 리턴시킬수 있는 방법ì�€ 없나요??
ë©”ì�¸ì—�서 들어간 sizeof(scores)는 ë°°ì—´ ì „ì²´ í�¬ê¸°ë¥¼ 리턴하는 반면ì—� 함수ì—� 들어간 sizeof(scores)는 int* 형ì�˜ í�¬ê¸°ë¥¼ 리턴한다.
ë©”ì�¸ì—�서 처럼 ì „ì²´ ë°°ì—´í�¬ê¸°ë§Œí�¼ 리턴시킬수 있는 방법ì�€ 없나요??
다른 질문.
참조를 �용해 배열� 함수� 넘길수는 없는건가요?? 배열� 잘 넘어가지 않네요;;
vsì—�서 타ì�´í•‘하는 ë°©ì‹�ì�´ì™¸ì—� ìž�ë�™ìœ¼ë¡œ 함수를 ìƒ�성해주는 바법사를 ì�´ìš©í•´ firend 함수를 ìƒ�ì„±í• ìˆ˜ëŠ” 없나요??
참조를 �용해 배열� 함수� 넘길수는 없는건가요?? 배열� 잘 넘어가지 않네요;;
vsì—�서 타ì�´í•‘하는 ë°©ì‹�ì�´ì™¸ì—� ìž�ë�™ìœ¼ë¡œ 함수를 ìƒ�성해주는 바법사를 ì�´ìš©í•´ firend 함수를 ìƒ�ì„±í• ìˆ˜ëŠ” 없나요??
ê°•í�¬ê²½ì�˜ 나름대로ì�˜ 답변 ¶
아마 승한ì�´ê°€ ì›�하는 답ì�€ ì•„ë‹ˆê² ì§€ë§Œ 한번 ì �어본다. 지금 함수ì—� ì „ë‹¬í•˜ëŠ” 것ì�€ ì�¸íŠ¸í˜•ì�˜ í�¬ì�¸í„°ì�´ê¸° 때문ì—� 함수는 지금 ë°›ì�€ 것ì�´ 숫ìž�ì�¸ì§€ ë°°ì—´ì�¸ì§€ 알 수 ê°€ 없지. 게다가 주소를 참조하게 ë�˜ì„œ 함수 안ì—�서 ê·¸ ê°’ì�„ 변경해주면 ì›�본 ê°’ë�„ 변하게 ë�˜ê³ . 그래서 나는 함수 안ì—� 멤버 변수를 만들어서 ì „ë‹¬ ê°’ì�„ 복사해서 ì“°ê±°ë“ . 보통 ì „ë‹¬ì�¸ìˆ˜ë¥¼ ë°›ì�„ 때 컴파ì�¼ëŸ¬ì—�서 ê·¸ ì›�본 ê°’ì�„ ì“°ì§€ ì•Šê³ ë³µì‚¬ê°’ì�„ 사용하는 ê±´ 알지? ê·¸ 작업ì�„ 프로그래머가 해주는 것ì�´ì§€. ë°‘ì�€ 여태 ë§�한대로 êµ¬í˜„í•˜ê³ ê°„ë‹¨í•˜ê²Œ 테스트한 소스야.
~cpp
#include<iostream>
using namespace std;
void Test(int *aArray, int aLength)
{
int *copyArray = new int[aLength];//ì „ë‹¬ ë°°ì—´ê³¼ ê°™ì�€ í�¬ê¸°ì�˜ 새로운 ë°°ì—´ ìƒ�성
for(int index = 0; index < aLength; index++)
copyArray[index] = aArray[index];//값 복사
cout << "in function : ";
cout << sizeof(copyArray[0])*aLength << " " << sizeof(copyArray[0]) << endl;
//여기부터는 테스트를 위해 ìž„ì�˜ë¡œ ë§Œë“ ì½”ë“œ!
cout << "\n변경 ì „ì�˜ copyArray : ";//변경 ì „ì�˜ ê°’ì�€ ì „ë‹¬ ë°°ì—´ê³¼ 같다
for(index = 0; index < aLength; index++)
cout << copyArray[index] << " ";
cout << endl;
for(index = 0; index < aLength; index++)//copyArrayì�˜ ëª¨ë“ ê°’ì�„ 0으로 초기화
copyArray[index] = 0;
cout << "변경 후� copyArray : ";
for(index = 0; index < aLength; index++)
cout << copyArray[index] << " ";
cout << endl;
delete []copyArray;//메모리 í•´ì œëŠ” 필수!!!
}
void main()
{
int arrayLength;
int scores[4]={1,2,3,4};
cout<<"in main : ";
cout << sizeof(scores) << " " << sizeof(scores[0]) << endl;
arrayLength = sizeof(scores)/sizeof(scores[0]);
Test(scores, arrayLength);
//검사!!
cout << "\n함수ì—� ì „ë‹¬ë�œ ê°’ì�´ 변해ë�„ scoresì�˜ ê°’ì�€ 변하지 않는다.\nscores: ";
for(int index = 0; index < arrayLength; index++)
cout << scores[index] << " ";
cout << endl;
}










