【问题标题】:How to make changes in an array through a function如何通过函数对数组进行更改
【发布时间】:2017-11-20 06:23:21
【问题描述】:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 10

// A hashtable is a mixture of a linked list and array

typedef struct node NODE;

struct node{
    int value;
    NODE* next;
};

int hash(int);                                                   
void insert(int,NODE **);                                       

int main(){

    NODE* hashtable[SIZE];
    insert(12,&hashtable[SIZE]); 
    printf("%d\n",hashtable[5]->value);  


}

int hash(int data){
return data%7;
}

void insert(int value,NODE **table){

    int loc = hash(value);
    NODE* temp = malloc(sizeof(NODE));
    temp->next = NULL;
    temp->value = value;
    *table[loc] = *temp;
    printf("%d\n",table[loc]->value);


}

上面的代码打印: 12 和 27475674(可能是随机数。)

如何让它打印 12 和 12,即如何在数组中进行更改。我想用为存储值而创建的节点的位置填充数组 [5]。

【问题讨论】:

    标签: c arrays pointers hashtable


    【解决方案1】:

    表达式*table[loc] 等于*(table[loc]),这可能不是您想要的,因为那时您将取消引用未初始化的指针。

    然后赋值将*temp 的内容复制到一些看似随机的内存中。

    然后您丢弃刚刚分配的内存,导致内存泄漏。

    也没有尝试创建哈希桶的链表。

    尝试在 main 函数中使用 initialization 初始创建 hashtable 数组,以使所有指针指向 NULL

    NODE* hashtable[SIZE] = { NULL };  // Will initialize all elements to NULL
    

    那么在插入节点的时候,实际上是把它链接到bucket-list中:

    temp->next = table[loc];
    table[loc] = temp;
    

    【讨论】:

      【解决方案2】:

      这只是我对您的程序所做的一个简单更改,它将告诉您您实际上做错了什么。

          #include<stdio.h>
      #include<stdlib.h>
      #include<string.h>
      #define SIZE 10
      
      
      // A hashtable is a mixture of a linked list and array
      
      typedef struct node NODE;
      
      struct node {
          int value;
          NODE* next;
      };
      
      NODE *hashtable[SIZE] = { NULL };
      
      int hash(int);                                                   
      int insert(int); //, NODE **);                                       
      
      int main(void)
      {
          int loc = insert(12); //, &hashtable[SIZE]); 
          if (loc < SIZE) {
          if (hashtable[loc]) {
              printf("%d\n", hashtable[loc]->value);
          } else {
              printf("err: invalid pointer received\n");
          }
          }
      
          return 0;
      }
      
      int hash(int data)
      {
          return data%7;
      }
      
      int insert(int value) //, NODE *table[])
      {
          int loc = hash(value);
          printf("loc = %d\n", loc);
      
          if (loc < SIZE) {
          NODE *temp = (NODE *) malloc(sizeof(NODE));
          temp->value = value;
          temp->next  = NULL;
          hashtable[loc]  = temp; 
          printf("%d\n", hashtable[loc]->value);
          }
      
          return loc;
      }
      

      在这里,我已经在全局范围内声明了哈希表,以确保您尝试更新的值对两个函数都是可见的。这就是您的代码中的问题。无论您为 temp 分配的新地址都具有地址“x”,但是您正试图从主函数访问无效地址。我只是想给你提示。希望这对您有所帮助。享受吧!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-20
        • 1970-01-01
        • 2021-11-27
        • 2023-03-11
        • 1970-01-01
        • 2022-07-25
        • 1970-01-01
        • 2015-11-04
        相关资源
        最近更新 更多