메모리 동적할당하는 방법

k960903의 이미지

밑에 코드는 궁금한 부분만 따로 편집한 부분입니다.
배열크기를 100으로 임의로 잡아주지 않고 입력받은 people만큼 배열크기를 잡고싶습니다!
동적할당을 해야한다는데.. 어떻게 해야 할까요!!

#include <iostream>
 
using namespace std;
 
int main(){
	int people;
	int su;
	cout<<"사람을 몇 명 넣으시겠습니까? (정수입력, 최대 100명)";
	cin>>people;//인원 수
	cout<<"과목을 몇 과목 넣으시겠습니까? (정수입력, 최대 6개)";
	cin>>su;//과목 수
	const int const *p=&su;
	int jumsu[100][15+2]={0,};//이 부분에서 100으로 배열 방 잡아주지 않고 입력받은 people 갯수 만큼 배열크기를 잡고싶습니다!
	char name[100][10]={0,};//이름
k960903의 이미지

이 파워포인트를 보면

동적배열 생성의 예를

typedef double * DoublePtr;
DoublePtr d;
 d = new double[10]; //Size in brackets

이런 식으로 주고 있는데 저는 배열크기를 cin을 통해 입력받으려는데 어떻게 해야 되나요?ㅠㅠ
그리고 d는 2차원 배열로 주고 싶습니다.

익명 사용자의 이미지

#include <iostream>
 
using namespace std;
 
int main(){
	int people;
	int su;
	cout<<"사람을 몇 명 넣으시겠습니까? (정수입력, 최대 100명)";
	cin>>people;//인원 수
	cout<<"과목을 몇 과목 넣으시겠습니까? (정수입력, 최대 6개)";
	cin>>su;//과목 수
	const int const *p=&su;
	int jumsu = new int[peopl][su];
	char name[100][10]={0,};//이름

k960903의 이미지

제가 처음에 쓴 방법입니다.
people이 상수가 아니라서 배열생성 시에 사용할 수 없다고 나옵니다...ㅠ

kgcrom의 이미지

http://gamjabi.tistory.com/16

참고하시면 될것같습니다.

검색어는 "이차원 포인터 동적할당"으로 했습니다.

흘러가고있는 지금 이 시간에 충실하자.

k960903의 이미지

여지껏 해보았던 다양한 방법들 중에 가장 간단하네요!!
정말 감사합니다!^^

kgcrom의 이미지

조금만 검색하고, 책 찾으면 나오는것들이에요.
재밌는 공부 하시길 바래요~ : )

흘러가고있는 지금 이 시간에 충실하자.

dorothy7의 이미지

malloc 과 relloc 을 사용하셔요!

댓글 달기

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