-10

我想访问指向结构的指针数组的元素,但我不知道如何访问它。

#include<stdio.h>
int main()
{
struct hash
{
int pages;
int price;
};
struct hash *h[5]={1,2,3,4,5};//array of pointer to structure
printf("value =%d\n",h->pages);
// want to access and also tell me how to data will be write
}
  How we will access the element I tried it through pointer but it's showing error
4

3 回答 3

1

Here is a sample that compiles. Maybe this helps you get started.

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

struct hash
{
int pages;
int price;
};

struct hash h[5]=
{
    { 1, 1 },
    { 2, 2 },
    { 3, 3 },
};

int main(void)
{
    printf("pages: %d\n", h[0].pages);
    printf("pages2: %d\n", h[1].pages);

    return EXIT_SUCCESS;
}
于 2013-06-14T13:33:32.833 回答
1
#include <stdio.h>
#include <stdlib.h>

int main()
{
    struct hash {
        int pages;
        int price;
    };
    struct hash *h[2];
    h[0] = malloc(sizeof(struct hash));
    h[0]->pages = 1;
    h[0]->price = 2;
    h[1] = malloc(sizeof(struct hash));
    h[1]->pages = 3;
    h[1]->price = 4;
    printf("value = %d\n", h[0]->pages);
}
于 2013-06-14T13:35:16.443 回答
1

我首先建议尝试让 astuct hash *h;指向单个变量。一旦它工作,你应该在它的基础上让数组struct hash *h[5]工作。

于 2013-06-14T13:35:20.177 回答