C++) 행렬을 함수의 인자로 받을때

익명 사용자의 이미지

int matrix[MAX][MAX]; 로 지정해둔 후 메인에서 scanf를 통해
행렬값을 받았는데요, 프림 알고리즘을 사용하고싶어서
따로 void prim함수를 만들어서 인자로 matrix를 넘겨주고싶습니다

그런데 void(int matrix[][]) 이런식으로 하니까 불가능하다고 떠서요
어떻게 해야할지 막막하네요ㅠㅠ

tyhan의 이미지

void(int matrix[MAX][])

 의 이미지

#include <iostream>
using namespace std;
 
const int MAX = 10; // 컴파일 타임 상수여야만 하지요.
 
// void tyhan(int matrix[MAX][]){ 틀림!
void tyhan(int matrix[][MAX]){ // 첫째 []만 비울 수 있습니다. 그 뒤부터는 채워 줘야 함
	cout << "tyhan" << endl; // 고차원 배열의 구현원리를 알면 이해할 수 있죠.
	return;
}
 
void func1(int (&a)[MAX][MAX]){ // 좀더 C++답게 짤 수도 있어요.
	cout << "func1" << endl;  // 전달하고자 하는 타입의 참조자로 만드는 거죠.
	return;
}
 
void func2(const int (&b)[MAX][MAX]){ // 파라미터를 바꾸게 하고 싶지 않다면,
	cout << "func2" << endl; // const가 좋은 선택입니다.
	return;
}
 
typedef int array_2d_t[MAX][MAX]; // 누구나 복잡한 C++ 선언 문법을 100% 아는 건 아니죠.
void func3(array_2d_t &c){ // 초보자를 배려합시다. 이러면 읽기도 더 쉽고...
	cout << "func3" << endl;
	return;
}
 
typedef const int const_array_2d_t[MAX][MAX]; // 마찬가지.
void func4(const_array_2d_t &d){
	cout << "func4" << endl;
	return;
}
 
int main() {
	int arr[MAX][MAX];
	tyhan(arr);
	func1(arr);
	func2(arr);
	func3(arr);
	func4(arr);
	return 0;
}

실행 결과: http://ideone.com/C7zg0b
tyhan의 이미지

그렇네요. 제가 헷갈렸씁니다.

사족을 달자면 저는 아래와 같이 쓰는데 질문의 내용을 보아 포인터를 없앤다는게....

void tyhan(int *matrix[MAX])

아직 눈컴파일은 무리인가 보네요 ㅠㅠ

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.