//Implimentation
#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");
}
#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
ต้องการเข้าถึง arr in puts และรับ แต่ฉันไม่รู้ว่าต้องทำอย่างไร> ฉันได้ลองใช้ p->arr ปกติแล้ว แต่ดูเหมือนจะไม่ทำงาน แต่ฉันแค่ต้องใส่ฟังก์ชั่นและฉันได้โพสต์รหัสไดรเวอร์ parray.h และไฟล์การใช้งาน โดยพื้นฐานแล้วโปรเจ็กต์เป็นเพียงอาร์เรย์ของตัวชี้ความว่างเปล่า แต่ฉันไม่รู้ว่าทำไมมันถึงทำให้ฉันมีเวลาที่ยากลำบาก ขอขอบคุณสำหรับความผิดพลาดในการเรียนรู้ก่อนหน้านี้