C언어 포인터 관련 질문이요~

georgekyung의 이미지

int* arr=100 ; 이라고 쓰면,, 100이 어딘지도 모르는데 초기화 불가능 하다고 책에 나와있던데요..

char* str="ABCDE"; 는 문자열상수의 주소값이 반환되어 포인터 변수에 저장된다고 나와있는데...

왜 int* arr=100 ;는 안되는 건가요.?(100도 상수 아닌가요?..)

이상 C언어...를 배우는 ...초보자가... 질문드립니다...

kkb110의 이미지

#include

using namespace std;

int main()
{
int* a = (int*)100;
cout << (int)a;
return 0;
}

http://codepad.org/nnrgri1W

It works.

Crocess의 이미지

int *pA=100;은 100이란 상수를 저장하는게 아니라
주소값을 100으로 초기화 시키게 되기 때문에 그런 것 같네요.

포인터는 메모리의 주솟값을 가져야 해요.

georgekyung의 이미지

문자열 상수..의 경우도... 주소를 저장하는건데요..

그럼 똑같은 격 아닌가요...?

단지 100은 상수이고,,, "ABCDE"는 '문자열' 상수인데.........

.................................................
그냥 받아들여야 하는건가요 ;;?

Geor

cleol의 이미지

상수(literal)라고 다 같은 상수는 아닙니다 :)
문자 상수 'A' 는 한 개의 기본 개체(?? primitive type object? 정확한 명칭을 모르겠네요)를 나타내는 상수입니다. 100 같은 수 상수와 비슷합니다. 하지만 문자열 상수 "ABCD" 는 {'A','B','C','D','\0'} 와 같은 "배열" 상수입니다. 100 같은 수가 아니라 {100,200,300} 같은 배열입니다. 배열과 포인터 다루는 법을 좀 더 공부하시면 질문하신 내용이 이해하실 수 있을겁니다.
그리고 이런 질문은 "프로그래밍 Q&A" 게시판에...

kkb110의 이미지

윗분 말씀이 맞습니다.

스트링의 경우에는 "..." 일단 스트링이 어떤 메모리에 할당되고 그 주소가 반환되서 숫자나 문자상수랑은 좀 다릅니다.

c++스펙에는 어떻게 명시되있는지는 모르겠으나 (아마 undefined?) 반환된 메모리주소의 특성은 컴파일러마다 다를 수 있습니다.

#include

using namespace std;

void f()
{
char* x = (char*)"ABC";

2[x] = 2[x]+1;

cout << x << endl;
}

int main()
{
f();
f();
f();
}

쓰기가 안되는 주소이면 ABC만 출력되고,
쓰기가 되는 이면 함수 호출때마다 뒷자리 알파벳이 바뀔겁니다.

예전에 VC++에서 했을때는 증가했던거같던데... 정확히 기억은 안나네요.

cppig1995의 이미지

저 경우에는 항상 결과가 "ABC"나 "ABD"가 나와야 할 것 같습니다.
그렇지 않다면 초기화 시점에서 x가 초기값이라는 보장이 없다는 건데, 좀 무서운걸요?
표준 좀 찾아봐야겠습니다.

수정:

C 표준 중 다음 문구와

ISO/IEC 9899:1999 wrote:
6.4.5 String literals
6 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

C++ 표준 중 다음 문구가
ISO/IEC 14882:2003 wrote:
2.13.4 String literals
2 Whether all string literals are distinct (that is, are stored in nonoverlapping objects) is implementation-defined. The effect of attempting to modify a string literal is undefined.

해당 사항을 나타내고 있는 건가요? 정말 무섭군요.

> What is the difference between software and hard water?
} Bugs drown in hard water, but live forever in software.

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

winner의 이미지

'C 언어 펀더멘탈'과 C 표준을 읽어보시면 답이 나옵니다. :-)

Prentice의 이미지

<삭제>

cppig1995의 이미지

사용 가능합니다. 다만 시스템 프로그래밍이 아닌 이상 저렇게 쓸 일은 드물지 않나 싶네요.
사용이 불가능하다면 C로 memory-mapped IO라던지 참 많은 것들을 구현할 수 없겠죠.
게다가, int *nullptr = NULL; 같은 코드는 어떻게 동작할까요? NULL은 0 값을 가진 정수 상수인데요.

적절한 권한을 가진 moderator께서는 이 글타래를 프로그래밍 QnA로 옮겨주시길 부탁드리겠습니다.

> What is the difference between software and hard water?
} Bugs drown in hard water, but live forever in software.

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

georgekyung의 이미지

아하..이제 좀 알겠군요!

p.s. 프로그래밍 QNA로옮겼습니다 ㅎㅎ..(죄송합니다 ;;)

Geor

댓글 달기

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