[C] 포인터 배열 활용을 할 때 궁금한점(?)

sarama의 이미지

우선 코드부터...

#include <stdio.h>

int main(void)
{
	char* arr[4]={
		"first",
		"second",
		"third"
	};
	printf ("%s \n", arr[0]);
	
	return 0;

}

그리고 다음의 경우

#include <stdio.h>

int main(void)
{
	int a=10, b=20, c=30;
	int* arr[]={&a, &b, &c};
	printf ("%d \n", *arr[0]);
	
	return 0;

}

첫 코드에서 메모리 주소가 가리키는 값에 접근할 때 *표시가 없었지만 두번째 코드에서는 *가 있어야 하네요...(printf에서 *arr[0]부분이요)

제 생각으로는 포인터 배열을 선언하는 과정에서 차이가 없어 보입니다.

"first"... 형식이던 "&a"... 형식이던 모두 주소값을 리턴하고 이것에 대한 배열 형식으로 선언된다고 보기 때문입니다.

그런데 *에 따라서 달라지니 궁금합니다.

제가 잘못 생각하고 있는것이 무엇인지 설명해주실분 안계신가요?

익명 사용자의 이미지

array of pointer to char와 array of pointer to pointer to int의 차이, 따라서 두번째 코드에서 원하시는 바를 얻기 위해서는 pointer to를 한번 지워줄 필요가 있습니다. 여기에 '*'가 필요하죠.

flyingykk의 이미지

1. char *arr[4] = {"first", "second", "third"};의 경우
"%s" 서식은 문자열 포인터를 인자로 취하는데 arr[0]에는 “first"로의 포인터가 들어 있으므로 * 불필요.

2. int *arr[] = {&a, &b, &c};의 경우
"%d" 서식은 int형을 인자로 취하는데 arr[0]에는 int로의 포인터(&a)가 들어 있으므로 꼭 *를 붙여야 합니다.

익명 사용자의 이미지

> "first"... 형식이던 "&a"... 형식이던 모두 주소값을 리턴하고 이것에 대한 배열 형식으로 선언된다고 보기 때문입니다.

많은 사람들이 종종 실수하는 것 중 하나가, 포인터는 다 같은 포인터라 생각하는 것입니다. 어떤 데이터형에 대한 포인터인가를 늘 생각하면서 짜야 실수를 줄일 수 있습니다.

> array of pointer to pointer to int의 ...

int* arr[]={&a, &b, &c};

arr은 array of pointer to int 입니다. %d가 요구하는 것은 int 형입니다. arr[0]의 데이터형은 pointer to int 입니다. 따라서 여기에 *가 더 붙어 *arr[0]이 되어야 합니다.

chronon의 이미지

%c 는 char 의 value, %s 는 char 의 pointer, %d 는 int의 value 를 받는다고 생각하면 간단합니다.

댓글 달기

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