如何使用指向结构体的指针访问指向结构体中数组的双指针
头文件:
#include <stdbool.h>
#ifndef PARRAY_H
#define PARRAY_H
typedef struct patype *parray;
parray newParray(int lower,
int upper); // returns a parray indexed with
// values in {lower,...,upper} where
// lower < upper; if lower is not less
// than upper print the warning message
// "Warning - illegal bounds for array\n"
// and return NULL
void freeParray(parray); // frees up all of parray as needed
bool put(parray p, int i, void *item); // equivalent to p[i] = item; does
// bounds checking and does nothing
// returning false if index is out
// of bounds, returns true if successful
void *get(parray p,
int i); // returns p[i] if index is in bounds of array;
// operation is undefined if index is out of bounds.
// Print "Warning - illegal index\n" if index is out of
// bounds.
int lb(parray); // returns lower bound of array
int ub(parray); // returns upper bound of array
int size(parray); // returns size of array
C 文件:
// Implementation
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
// void *vptr
typedef struct patype *parray;
typedef struct patype {
int count;
int upper;
int lower;
void **arr;
} patype;
parray newParray(int lower, int upper) // creates the parray
{
if (lower > upper) {
printf("Warning - illegal bounds for array\n");
return NULL;
}
void *data = malloc(sizeof(struct patype));
((patype *)data)->upper = upper;
((patype *)data)->lower = lower;
((patype *)data)->count = upper - lower;
((patype *)data)->arr = malloc(sizeof(void *) * upper + lower);
return data;
}
int lb(parray p) // returns lower bound
{
return p->lower;
}
int ub(parray p) // returns upper bound
{
return p->upper;
}
int size(parray p) // returns size
{
return p->count;
}
bool put(parray p, int i, void *item) // need to see if array at i is = to item
{
for (k = 0; k < p->count; k++) {
printf("%d\n", *(int *)p);
if (p == item) return true;
}
return false;
}
// void *get(parray p, int i) //Need to return the actual array value if its in
// bounds but not working
// {
// p[i] = i;
// return p[i];
// }
void freeParray(parray p) // frees memory
{
free(p);
}
#include <stdio.h>
#include <stdlib.h>
#include "parray.h"
// Driver Code
int main(void) {
parray pa;
pa = newParray(5, 20);
int *p1 = malloc(sizeof(int));
float *p2 = malloc(sizeof(float));
char p3[20];
put(pa, 5, p1);
put(pa, 6, p2);
put(pa, 7, p3);
// get(pa,5);
ub(pa);
size(pa);
lb(pa);
printf("Lower Bounds %d\n", lb(pa));
printf("Upper Bounds %d\n", ub(pa));
// printf("%d",size(pa));
printf("Check puts %d\n", put(pa, 7, p1));
printf("Check puts %d\n", put(pa, 6, p2));
printf("Check puts %d\n", put(pa, 7, p3));
// printf("%d get\n",*(int*)get(pa,5));
freeParray(pa);
printf("(program compiled & ran)\n");
}
需要访问 put 和 get 中的 arr,但我不知道如何访问。
我尝试了通常的 p->arr
但我似乎无法让任何东西发挥作用。但我只需要使函数 put 和 get 我已经发布了驱动程序代码 parray.h
和实现文件。
该项目基本上只是一个空指针数组,但我不知道为什么它给了我如此艰难的时间,感谢您对早期学习中的错误表示歉意。
Header file:
#include <stdbool.h>
#ifndef PARRAY_H
#define PARRAY_H
typedef struct patype *parray;
parray newParray(int lower,
int upper); // returns a parray indexed with
// values in {lower,...,upper} where
// lower < upper; if lower is not less
// than upper print the warning message
// "Warning - illegal bounds for array\n"
// and return NULL
void freeParray(parray); // frees up all of parray as needed
bool put(parray p, int i, void *item); // equivalent to p[i] = item; does
// bounds checking and does nothing
// returning false if index is out
// of bounds, returns true if successful
void *get(parray p,
int i); // returns p[i] if index is in bounds of array;
// operation is undefined if index is out of bounds.
// Print "Warning - illegal index\n" if index is out of
// bounds.
int lb(parray); // returns lower bound of array
int ub(parray); // returns upper bound of array
int size(parray); // returns size of array
C file:
// Implementation
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
// void *vptr
typedef struct patype *parray;
typedef struct patype {
int count;
int upper;
int lower;
void **arr;
} patype;
parray newParray(int lower, int upper) // creates the parray
{
if (lower > upper) {
printf("Warning - illegal bounds for array\n");
return NULL;
}
void *data = malloc(sizeof(struct patype));
((patype *)data)->upper = upper;
((patype *)data)->lower = lower;
((patype *)data)->count = upper - lower;
((patype *)data)->arr = malloc(sizeof(void *) * upper + lower);
return data;
}
int lb(parray p) // returns lower bound
{
return p->lower;
}
int ub(parray p) // returns upper bound
{
return p->upper;
}
int size(parray p) // returns size
{
return p->count;
}
bool put(parray p, int i, void *item) // need to see if array at i is = to item
{
for (k = 0; k < p->count; k++) {
printf("%d\n", *(int *)p);
if (p == item) return true;
}
return false;
}
// void *get(parray p, int i) //Need to return the actual array value if its in
// bounds but not working
// {
// p[i] = i;
// return p[i];
// }
void freeParray(parray p) // frees memory
{
free(p);
}
#include <stdio.h>
#include <stdlib.h>
#include "parray.h"
// Driver Code
int main(void) {
parray pa;
pa = newParray(5, 20);
int *p1 = malloc(sizeof(int));
float *p2 = malloc(sizeof(float));
char p3[20];
put(pa, 5, p1);
put(pa, 6, p2);
put(pa, 7, p3);
// get(pa,5);
ub(pa);
size(pa);
lb(pa);
printf("Lower Bounds %d\n", lb(pa));
printf("Upper Bounds %d\n", ub(pa));
// printf("%d",size(pa));
printf("Check puts %d\n", put(pa, 7, p1));
printf("Check puts %d\n", put(pa, 6, p2));
printf("Check puts %d\n", put(pa, 7, p3));
// printf("%d get\n",*(int*)get(pa,5));
freeParray(pa);
printf("(program compiled & ran)\n");
}
Need to access the arr in put and get but I have no idea how to.
I've tried the usual p->arr
but I can't seem to get anything to work. But I just need to make functions put and get I've posted the driver code parray.h
and the implementation file.
The project is basically just an array of void pointers but I don't know why its giving me such a rough time thank you sorry for the mistake earlier learning.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论