[완료] 함수에 인자를 넘길때 call by value로 넘겼는데, 값이 변경되었습니다.

sia79의 이미지

안녕하세요. 지금 제가 ncurses를 좀 공부하고 있는데, 아래와 같은 소스가 있습니다.

#include <ncurses.h>                    /* ncurses.h includes stdio.h */                   
#include <string.h>                                                                        
 
int main()                                                                                 
{                                                                                          
 char mesg[]="Enter a string: ";                /* message to be appeared on the screen */ 
 char str[80];                                                                             
 int row,col;                           /* to store the number of rows and *               
                                         * the number of colums of the screen */           
 initscr();                             /* start the curses mode */                        
 getmaxyx(stdscr,row,col);              /* get the number of rows and columns */           
 mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg);                                           
                                /* print the message at the center of the screen */        
 getstr(str);                                                                              
 mvprintw(LINES - 2, 0, "You Entered: %s", str);                                           
 getch();                                                                                  
 endwin();                                                                                 
 
 return 0;                                                                                 
} 

보시면, int row, col 변수를 getmaxyx()함수에 넘길때 포인터형태로 넘기지 않았어도 값이 바뀝니다.

어떻게 값이 바뀔수 있는 것인지 궁금합니다.

(할까말까하다) 추가 질문인데요.
제가 ncurses 공부하고 있으니까 왜 이런걸 공부하냐고 심하게는 쓸데없는거 하지말고 다른거 하라고 들었었는데요.

제가 정말로 시대에 뒤떨어진 공부를 하고 있는건가요?

klara의 이미지

ncurses는 잘 모르므로 확실치 않지만, 아마 getmaxyx가 함수가 아니라 매크로일듯합니다.
매크로라면 단순히 치환되는 것이므로 변수에 직접 값이 대입될수 있으니까요.

cinsk의 이미지

함수가 아니라, 포인터이기 때문입니다. (man curs_getyx 참고)

커서를 직접 조작하는 Terminal용 프로그램을 작성할 일이 드물기 때문에 그런 조언을 한 것이라 생각하지만,
터미널 interface에 대해 잘 이해할 수 있으므로, 아주 쓸모없는 일은 아닙니다.

그리고 여전히 많은 프로그램들이 terminal interface를 쓰고 있으므로, 적어도 DOS용 conio나, Windows terminal interface보다는
더 쓸모있지 않을까요?

--
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Korean Ver: http://www.cinsk.org/cfaqs/

JN의 이미지

함수가 아니라, 포인터이기 때문입니다. (man curs_getyx 참고)

문장이 약간 이상합니다. "함수가 아니라, 매크로이기 때문입니다"를 잘못쓰신 건가요?

ncurses.h에서 getmaxyx를 찾아보면 아래와 같이 되어 있습니다.

#define getmaxyx(win,y,x)       (y = getmaxy(win), x = getmaxx(win))