c에서 struct의 동적 할당 좀 알려주세요.

yamong의 이미지

간단한 것인데 방법을 몰라서 고민하고 있습니다. 사용자로부터 m값을 입력 받아서 다음과 같은 struct를 만드려고 합니다. 전역 변수이구요.

struct {
int arr[m];
} Data;

main() {
scanf("%d", m);
}

물론 위 소스는 에러 납니다. ㅡㅡㅋ
제가 묻고 싶은 것이 이해가 가시나요? 꼭 좀 빠른 답변 부탁합니다. 꾸벅.

keizie의 이미지

int *a를 주고,
m을 받아서 크기값이 생기면 malloc(sizeof(int)*m)인가로 정하면 되죠.

withlhw의 이미지

구조체를 동적으로 할당받는 다는것이 어떤 식의 동적인지 이해가 되지 않네요.

위의 구조체의 멤버중 배열이 하나 있는데.. 구조체 하나만 선언해서

사용하면 않되나요?, 아니면 동적으로 할당한다는 것이 값을 하나 받을때 마다

하나의 구조체를 생성해서 처리한다는 것인지.. 만약 이것이라면은

링크드 리스트를 이용하면은 됩니다.

yamong의 이미지

제가 동적으로 할당 한다는 의미는 요. 맞는 말인지 모르겠지만, m값을 받아서 m값만큼 배열을(구조체의 맴버)만들어야 하기 때문에 물은 것 입니다.
그냥 간단하게 입력 받은 수만큼 구조체에 있는 배열을 생성하면 되요.

맹고이의 이미지

위에분 말씀대로 배열말고 포인터를 쓰세요
int *arr;

xjiwoox의 이미지

이렇게 해보세요.

struct tagArray
{
    int anArray[1];
} sData, *psData;

int main(int argc, char **argv)
{
    psData = (struct tagArray *)calloc(sizeof(int) * m, 1);

/* 여기서 구조체 사용...(psData->anArray[0] ~ psData->anArray[m -1] */

    free(psData);
}

또는...

struct
{
    int *pnArray;
} sData, *psData;

int main(int argc, char **argv)
{
    sData.pnArray = (int *)calloc(sizeof(int) * m, 1);

/* 여기서 구조체멤버 사용...(sData.pnArray[0] ~ sData.pnArray[m -1] */

    free(sData.pnArray);
}

그런데.. 굳이 멤버변수를 하나만 쓰실거라면 구조체를 쓸 필요가 있는지는 --;

.지우.

s(˘∼˘*)z,·´″"`°³о$ √(´∀`√)... (˘ヘ˘ㆀ)a

yamong의 이미지

어떤 고마운 분께서 가르쳐 주셔서 해결했습니다. ^^

댓글 달기

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