배열 포인터 선언에 대한 질문입니다.

skylandi의 이미지

int (*ap)[3];
ap=(int (*)[3])malloc(sizeof(int)*2*3);


int a[100];
int (*ap)[10];
ap=(int (*)[10])a;

와의 차이점은 무엇입니다.

미리 감사합니다.

익명 사용자의 이미지

skylandi wrote:
int (*ap)[3];
ap=(int (*)[3])malloc(sizeof(int)*2*3);


int a[100];
int (*ap)[10];
ap=(int (*)[10])a;

와의 차이점은 무엇입니다.

미리 감사합니다.


차이점은 모르겠고, 공통점은 둘다 정상적으로 작성한 코드가 아니라는 점입니다. 8)
wooix의 이미지

첫째 코드..

이건 잘못된 녀석인거 같네요..
malloc할때 (int (*)[3])을 제대로 해석을 못해주는군요..
g++ 2.95.4에서 테스트 했습니다. 근데 문맥상 int *ap를 3개의 배열로 만들었기 때문에 int *ap[3]과는 다른 문법을 따르더군요..
그렇다면 후자의 경우는 ap[0]~ap[2]까지를 각각 malloc해주면 되는데..
전자의 경우는 null포인터가 가르키는 곳에 3개의 배열이 있기때문에 주소를 찾아서 메모리를 할당한다는게 문맥상으론 좀 애매하네요..(단 이런 해석은 ()가 []보다 priority가 높다고 컴팔러가 해석한 경우입니다. )
복잡하네용..
알아서 해결해 보시길 권장합니다 ㅡㅡa

둘째 코드 --> 정상입니다. :)

int a[100]일때 address가 0xbffff60c이라면
int (*ap)[10]은 int [10]인지 (int *)[10]인지..어째든 decimal 40 hexadecimal 0x28의 크기단위로 pointer를 할당하는군요..
즉 &ap[0]-> 0xbffff634
&ap[1]-> 0xbffff65c
이렇게요..
그렇기 때문에 ap[0][0]~ ap[9][9]까지 a[0]~a[100]까지 값을 이용할수 있습니다.

크기 정의 안한 포인터다보니..사용에 주의 해야될듯 싶네요..

평온하다~

cleaneye의 이미지

만약 정상적으로 첫번째 프로그램을 코딩하였다면

두 코드의 의미는 동일합니다.

즉 아시다시피 x*3(or 10)의 크기를 가지는 이차배열의 포인터인 것이죠.

int (*ap)[3];
ap=(int (*)[3])malloc(sizeof(int)*2*3);
--> ap=(int (*)[3])malloc(sizeof(int (*)[3])*2*3);
: 2*3 array

int a[100];
int (*ap)[10];
ap=(int (*)[10])a;
--> 10*10 array

네 갈길을 가라! 남이야 뭐라든!

익명 사용자의 이미지

함고로 C를 처음 공부하는 분들이 불필요하게 복잡한 포인터 연산으로 시간과 정력을 소비하는 경우가 꽤 있는데, 위의 경우는 정상적인 프로그램에서는 절대 안쓰는 것이니 경시대회 출전을 위한 것이 아니라면 그냥 넘어가셔도 무방합니다.

댓글 달기

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