关于指针的问题

时间:2018-08-28 23:40:00

标签: c pointers

我想将hashnode与该节点链接。由于hashnodeNode**(指向节点指针的指针),因此hashnode的值应该是节点指针(即&node)的地址。我的代码在下面。

但是,这向我显示了一个错误,即从struct Node *(又名Node **)分配给struct Node **的指针类型不兼容;删除&。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct Hashnode
{
    int size;
    struct Node** hashnode;
}Hashtable;


typedef struct Node
{
    char* word;
    struct Node* next;
}Node;

int main(void)
{
    Node* node = malloc(sizeof(Node));
    node->word = "Elvin";
    node->next = NULL;
    printf("First Node created successfully!...\n");

    Hashtable hasht;

    hasht.size = 10;
    hasht.hashnode = malloc(sizeof(*hasht.hashnode)*hasht.size);
    for (int i = 0; i < hasht.size; i++)
    {
        hasht.hashnode[i] = NULL;
        printf("the address of the %i hashnode is: %p\n", i, hasht.hashnode[i]);
    }
    printf("The hashtable is created successfully!...\n");

    int key = 3;
    hasht.hashnode[key] = &node;
}

知道我做错了什么吗?我在概念上错了什么?

2 个答案:

答案 0 :(得分:1)

您已将// // ViewController.h // #import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UIImageView *imageViewInstance1; @end // // ViewController.m // #import "ViewController.h" #import "RTSPPlayer.h" RTSPPlayer *rtspPlayer; int frameIndex; NSTimer *timerRefresh; #define CAMERA_RTSP_ADDRESS "rtsp://..." #define CAMERA_FPS (30.0) #define TIMER_INTERVAL_SECONDS (1.0/CAMERA_FPS) @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSString* address = @CAMERA_RTSP_ADDRESS; rtspPlayer = [[RTSPPlayer alloc] initWithVideo:address usesTcp:false]; rtspPlayer.outputWidth = 640; rtspPlayer.outputHeight = 480; [rtspPlayer seekTime:0.0]; timerRefresh = [NSTimer scheduledTimerWithTimeInterval:TIMER_INTERVAL_SECONDS target:self selector:@selector(run:) userInfo:nil repeats:YES]; } -(void) run:(NSTimer *) timer { if(![rtspPlayer stepFrame]) { [timerRefresh invalidate]; [rtspPlayer closeAudio]; } UIImage* uiImage = rtspPlayer.currentImage; _imageViewInstance1.image = uiImage; frameIndex++; } @end 设置为指针数组。因此,您想将指针分配给给定的数组元素,而不是指针的地址:

hashnode

答案 1 :(得分:1)

  

知道我做错了什么吗?我在概念上错了什么?

尽管hashnode是指向struct Node的指针,但是hashnode[key]只是指向struct Node的指针。 但是&node还是指向struct Node的指针的地址(或指针)。因此,以下分配失败:

hasht.hashnode[key] = &node;

编译器抱怨正确。

您必须执行以下操作:

hasht.hashnode[key] = node;