質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

アルゴリズム

アルゴリズムとは、定められた目的を達成するために、プログラムの理論的な動作を定義するものです。

Q&A

解決済

1回答

744閲覧

C言語 初期化されていない配列要素に対してのNULL確認は妥当か?

Kchan_01

総合スコア110

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

アルゴリズム

アルゴリズムとは、定められた目的を達成するために、プログラムの理論的な動作を定義するものです。

0グッド

1クリップ

投稿2020/03/25 04:56

編集2020/03/25 04:57

C言語を学んでいる初心者です。
下記のサイトを使って、ハッシュのアルゴリズムを学んでいます。

Hash Table Program in C - Tutorialspoint

掲載されていたコードで違和感のある記述がされているところがありました。
whileの条件式で、配列の中身を確認している部分なのですが、初期化されていない配列に対してNULL確認を行っています。
この書き方はおかしいと思うのですが、私の認識は合っているでしょうか。

このサイトのコードはアルゴリズムを解説するためのコードで、厳密な書き方をされていない。という前提で、学習を進めたほうが良いでしょうか。

while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1)

c

1#include <stdio.h> 2#include <string.h> 3#include <stdlib.h> 4#include <stdbool.h> 5 6#define SIZE 20 7 8struct DataItem { 9 int data; 10 int key; 11}; 12 13// ここ!!!!! 14struct DataItem* hashArray[SIZE]; 15struct DataItem* dummyItem; 16struct DataItem* item; 17 18int hashCode(int key) { 19 return key % SIZE; 20} 21 22struct DataItem *search(int key) { 23 //get the hash 24 int hashIndex = hashCode(key); 25 26 //move in array until an empty 27 while(hashArray[hashIndex] != NULL) { 28 29 if(hashArray[hashIndex]->key == key) 30 return hashArray[hashIndex]; 31 32 //go to next cell 33 ++hashIndex; 34 35 //wrap around the table 36 hashIndex %= SIZE; 37 } 38 39 return NULL; 40} 41 42void insert(int key,int data) { 43 44 struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem)); 45 item->data = data; 46 item->key = key; 47 48 //get the hash 49 int hashIndex = hashCode(key); 50 51 // ここ!!!!!!!!!!!  52 //move in array until an empty or deleted cell 53 while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1) { 54 //go to next cell 55 ++hashIndex; 56 57 //wrap around the table 58 hashIndex %= SIZE; 59 } 60 61 hashArray[hashIndex] = item; 62} 63 64struct DataItem* delete(struct DataItem* item) { 65 int key = item->key; 66 67 //get the hash 68 int hashIndex = hashCode(key); 69 70 //move in array until an empty 71 while(hashArray[hashIndex] != NULL) { 72 73 if(hashArray[hashIndex]->key == key) { 74 struct DataItem* temp = hashArray[hashIndex]; 75 76 //assign a dummy item at deleted position 77 hashArray[hashIndex] = dummyItem; 78 return temp; 79 } 80 81 //go to next cell 82 ++hashIndex; 83 84 //wrap around the table 85 hashIndex %= SIZE; 86 } 87 88 return NULL; 89} 90 91void display() { 92 int i = 0; 93 94 for(i = 0; i<SIZE; i++) { 95 96 if(hashArray[i] != NULL) 97 printf(" (%d,%d)",hashArray[i]->key,hashArray[i]->data); 98 else 99 printf(" ~~ "); 100 } 101 102 printf("\n"); 103} 104 105int main() { 106 dummyItem = (struct DataItem*) malloc(sizeof(struct DataItem)); 107 dummyItem->data = -1; 108 dummyItem->key = -1; 109 110 insert(1, 20); 111 insert(2, 70); 112 insert(42, 80); 113 insert(4, 25); 114 insert(12, 44); 115 insert(14, 32); 116 insert(17, 11); 117 insert(13, 78); 118 insert(37, 97); 119 120 display(); 121 item = search(37); 122 123 if(item != NULL) { 124 printf("Element found: %d\n", item->data); 125 } else { 126 printf("Element not found\n"); 127 } 128 129 delete(item); 130 item = search(37); 131 132 if(item != NULL) { 133 printf("Element found: %d\n", item->data); 134 } else { 135 printf("Element not found\n"); 136 } 137}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

初期化されていない配列に対してNULL確認を行っています。

いえ、静的領域に配置される配列は初期値がゼロクリアされます。

投稿2020/03/25 04:57

編集2020/03/25 04:57
maisumakun

総合スコア145183

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Kchan_01

2020/03/25 05:47

ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問