왕초보의 참조변수에 대한 질문입니다.

익명 사용자의 이미지

이 성적 짜는 프로그램에서 함수를 호출할 때
cal(sungjuk &temp)를 하는데 왜 prn(sungjuk temp)하지 않는지요?
컴파일 할 때 뒤에걸로 하니깐 에러가 나는데 왜 &붙여서 하는지 궁금합니
다.
그리고 그 밑의 prn(sungjuk temp)는 temp에 &를 안 붙이는데 왜 그런지
앞의 것과의 차이점을 설명해 주시면 공부하는데 대단히 큰 도움이 될 것
같습니다.

#include
struct sungjuk{
char name[20];
int kor;
int eng;
int mat;
int tot;
float ave;
};

void cal(sungjuk &temp);
void prn(sungjuk temp);

void main()
{

sungjuk temp;
cout<<"\n이름을 입력하세요 => " ;
cin>> temp.name;
cout<<"\n국어점수를 입력하세요 => " ;
cin>> temp.kor;
cout<<"\n영어점수를 입력하세요 => " ;
cin>> temp.eng;
cout<<"\n수학점수를 입력하세요 => " ;
cin>> temp.mat;
cal(temp);
prn(temp);

}

void cal(sungjuk &temp)
{
temp.tot = temp.kor + temp.eng + temp.mat;
temp.ave = temp.tot / 3;
}

void prn(sungjuk temp)
{
cout << "\n 이름 " << temp.name ;
cout << "\n 국어 " << temp.kor ;
cout << "\n 영어 " << temp.eng ;
cout << "\n 수학 " << temp.mat ;
cout << "\n 총점 " << temp.tot ;
cout << "\n 평균 " << temp.ave ;
cout << "\n";
}

익명 사용자의 이미지

참조변수(reference)의 개념을 묻는 예제로군요. ^ ^

함수의 원형이 다음과 같네요.

void cal(sungjuk &temp);
void prn(sungjuk temp);

cal함수는 인자로 temp 참조변수를 넣고, prn은 temp변수를 call by value

형태로 넘겨주고 있죠.

참조변수는 쉽게, 한 변수의 별칭이라고 생각하면 됩니다.

C에서 포인터를 넘겨주고, *로 역참조를 해서 변수의 값을 얻고, 넘겨주
는 것이

가능한데요. 이렇게 하다보면, 코드가 복잡해지는 걸 피할 수 없죠. ^^;;

cal함수 내부에서 temp로 받은 객체의 정보를 변경하면, 호출한 부분의

변수가 변경이 되구요.

prn의 경우 temp변수에 념겨준 구조체 자체가 복사가 되어서 다른 변수에

저장됩니다. 즉 호출한 부분에서 변수의 변경은 이루어지지 않죠.

prn같은 경우 성적 구조체의 값을 출력만 하고 있는데, 이때는

인자의 타입명 앞에 대개 const를 붙여서, 함수 내부에서 인자를 바꾸지
않을

것을 명시적으로 선언해 줍니다.

----------------------------
void prn(const sungjuk temp);
----------------------------

물론 prn도 참조 변수의 형태로 나타내면

------------------------------
void prn(const sungjuk& temp);
------------------------------

이렇게 할 수 있겠죠.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.