구현부 선언부?

morgana의 이미지

안녕하세요.
공부라는걸 시작한지 얼마 안되는 학생입니다.
OS시간에 들은걸 간단하게 구현하라길래, 코딩해 보는도중 궁금한점이 생겨서 질문드립니다.

#include
#include
struct process{
char *name;
struct resource *assmnt;
struct resource *request;
};

struct resource{
char *name;
struct process *request;
struct process *assmnt;
};

void main(){
int size;
scanf("%d",&size); //333333333333
struct process *ob=(struct process *)malloc(sizeof(struct process)*size); //22222222222

}

왜 이렇게 코딩을 하면 안되는건가요?
2시간정도 이곳저곳을 검색해보니 선언부에서는 선언만 하고 구현은 안된다는 말씀들만 본거 같네요.
2번과 3번의 위치를 바꿔보면 컴파일은 되는데 원하는게 아니라서요.

구현부와 선언부에 관해서 간단하게 설명좀 부탁드리겠습니다.

cppig1995의 이미지

int size;
scanf("%d",&size); //333333333333
struct process *ob=(struct process *)malloc(sizeof(struct process)*size); //22222222222

대신

int size;
struct process *ob;
scanf("%d",&size); //333333333333
ob=(struct process *)malloc(sizeof(struct process)*size); //22222222222

이렇게 하셔야 합니다.

C++은 그렇지 않지만, C에서는 선언부와 구현부가 나누어져 있기 때문이죠.
선언부 두 줄은 int형 size와 struct process형 포인터 ob를 선언하고,
구현부에서는 각각 size에 값을 읽어들이는 일과, ob에 동적 메모리 할당을 합니다.

Real programmers /* don't */ comment their code.
If it was hard to write, it should be /* hard to */ read.

gamdora의 이미지

최신 C 표준인 C99에서는 C++처럼도 쓸 수 있다고 합니다.

cppig1995의 이미지

앗 실수.
'항상 ISO/IEC 9899:1999 표준을 생각하자'라는 C 프로그래밍 신념(?)을 세워놓고서도 까먹네요.

Real programmers /* don't */ comment their code.
If it was hard to write, it should be /* hard to */ read.

SoftOn의 이미지

정답 : 예전 컴파일러로 위 코드를 돌리면 컴파일 안된다;;

morgana의 이미지

답변해 주신분들 감사합니다.

언제나 느끼지만 기초가 중요하군요...
=======================================
아프다...

=======================================
.

댓글 달기

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