C 포인터 및 구조체를 제대로 이해하고 있는지 모르겠습니다.

익명 사용자의 이미지

struct nodeType{
	char* name;
	nodeType* link;
};

연결 리스트를 공부하다가 자료형을 적을 위치에 구조체의 이름을 적은 뒤 *를 넣어 포인터로 만든 link라는 항목이 이해가 잘 안됐습니다.
제가 포인터를 이해한대로 nodeType의 주소를 받을 기억 장소의 주소를 받을 link라는 변수(항목)라고 이해를 하고 넘겼습니다.

void linkfun(){
	nodeType* newnode;
	nodeType* head;
	nodeType* current;
	nodeType* prenode;
 
	head=NULL;
	char* name;
	int cho;
 
	printf("\n insert=1, delete=2, search=3, traverse=4, quit=9 : ");
	scanf("%d",&cho);
	while(cho!=9){
		switch(cho){
			case 1: {
				printf("Type insert name : ");
				name = (char*)malloc(20); 
				scanf("%s",name);
 
				newnode=(nodeType*)malloc(sizeof(nodeType));
				newnode->name=name;
				newnode->link=NULL;
				if(head==NULL){
					head=newnode; 
				}
				else{
					current=head; 
					while(current!=NULL){
						prenode=current;
						current=current->link;
					};
					prenode->link=newnode;
				}
				break;

그 후 nodeType*으로 선언?된 변수들이 4개가 더 나오고 head=NULL이 정확히 무슨 의미인지 모르겠고
newnode->name=name 이 부분도 따로 뭐 struct nodeType *newnode 이렇게 한 것도 없는데 바로 저렇게 가는 이유도 모르겠습니다.
그리고 head=newnode 이렇게 하면 어떤 값이 어떻게 전달 되는지와 current=current->link; 이 부분도 이해가 전혀 되지 않습니다.
개념이 부족해서 그런지 제가 무엇을 어떻게 모르는지도 정확히 모르는 상태라 용어가 틀리거나 제가 개념을 잘못 이해한 부분이 있을 수도 있습니다. 최대한 제가 아는 선에서 제가 무엇을 모르는지 서술해본 것이니 틀린 점이 있다면 꼭 지적 부탁드립니다.

익명 사용자의 이미지

1. C언어 코드가 아니라 C++언어 코드인 듯하군요.

2. <code> 태그로 감싸주세요. 그래야 보기 편합니다.

익명 사용자의 이미지

struct nodeType{
	char* name;
	nodeType* link;//nodetype의 주소만을 수록 받을 기억장소의 주소를 받는다// 
};

void linkfun(){
	nodeType* newnode;
	nodeType* head;
	nodeType* current;
	nodeType* prenode;
 
	head=NULL;
	char* name;
	int cho;
 
	printf("\n insert=1, delete=2, search=3, traverse=4, quit=9 : ");
	scanf("%d",&cho);
	while(cho!=9){
		switch(cho){
			case 1: {
				printf("Type insert name : ");
				name = (char*)malloc(20); 
				scanf("%s",name);
 
				newnode=(nodeType*)malloc(sizeof(nodeType));
				newnode->name=name;
				newnode->link=NULL;
				printf("newnode name,link,address: %s %d %d\n",newnode->name,newnode->link,newnode);
				if(head==NULL){
					head=newnode;//newnode-1이 지적하는 주소를 받음 
					printf("head name,link,address: %s %d %d\n",head->name,head->link,head);
				}
				else{
					printf("head name,link,address: %s %d %d\n",head->name,head->link,head);
					current=head;//head는 newnode-1의 name과 link를 계속 유지, head가 지적하는 주소를 받음 
					printf("current name,link,address: %s %d %d\n",current->name,current->link,current);
					while(current!=NULL){
						prenode=current;//current가 지적하는 주소를 받음 
						printf("prenode name,link,address: %s %d %d\n",prenode->name,prenode->link,prenode); 
						current=current->link;
						printf("current address: %d\n",current); 
					};
					prenode->link=newnode; 
					printf("out prenode name,link,address: %s %d %d\n",prenode->name,prenode->link,prenode);
				}
				break;
}

이렇게 하면 되나요??

raymundo의 이미지

종이에 펜으로 그림은 그려보고 계신가요? 코드만 들여다보는 것보다 그림 한 번 그려보며 생각하는 게 큰 도움이 될 것 같습니다.

예를 들어
https://truecode.tistory.com/11

여기 있는 그림 같은 거요. 중간에보면 pNode = pNode->pNext 의 경우도 나와 있네요 (이 글에서 current = current->link )

그리고 그림에도 단지 사각형과 화살표만 그리는 게 아니라, 구체적으로 노드 하나가 100번지부터 110번지까지 차지하고 있다고 하면 그 노드를 가리키는 포인터 변수에는 100이 들어가 있겠죠. 다음 노드가 120번지부터 위치하고 있다면 첫번째 노드의 link 에는 120이 들어가 있을 거고요. 이렇게 값도 채워넣어가면 더 나을 것 같고요.

좋은 하루 되세요!

rajakym의 이미지

그 후 nodeType*으로 선언?된 변수들이 4개가 더 나오고
=> 이해하신 것처럼 nodeType 의 구조체를 포인팅할 수 있는 포인터 4개를 더 선언한 것 입니다.

head=NULL이 정확히 무슨 의미인지 모르겠고
=> 그냥 변수 초기화 입니다. 나중에 if (head == NULL) 쓸려면 NULL 초기화 해줘야 합니다. (쓰래기값 제거)

newnode->name=name 이 부분도 따로 뭐 struct nodeType *newnode 이렇게 한 것도 없는데 바로 저렇게 가는 이유도 모르겠습니다.
=> 바로 위에 보시면 malloc 으로 메모리 할당하고 있습니다.
newnode=(nodeType*)malloc(sizeof(nodeType));

댓글 달기

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