How can I cycle through the array using a for loop and the reference operator?
for (i = 0; i < university->size; i++)
{
if (university->arr->MarkA <= 100)
{
sum += university->arr->MarkA;
count++;
}
Where should I insert the i?
These are the structures:
typedef struct Student
{
char *name;
long ID;
int MarkA, MarkB, HW;
}stud;
typedef struct University
{
stud *arr;
int size;
}Uni;
I used a function to create a University array
void build_Uni(FILE* in, Uni* university)
{
int i = 0;
stud temp;
char Name[100];
while (!feof(in))
{
fscanf(in, "%s%li%d%d%d", Name, &temp.ID, &temp.MarkA, &temp.MarkB, &temp.HW);
i++;
}
university->size = i;
rewind(in);
university->arr = (stud*)malloc(university->size * sizeof(stud));
if (university->arr == NULL) Get_Lost("Memory allocation failed!");
else for (i = 0; i < university->size; i++)
fill_Uni(in, university->arr + i);
}
void fill_Uni(FILE* in, stud* student)
{
char Name[100];
fscanf(in, "%s%li%d%d%d", Name, &student->ID, &student->MarkA, &student->MarkB, &student->HW);
student->name = (char*)malloc((strlen(Name) + 1) * sizeof(char));
if (student->name == NULL) Get_Lost("Error allocatig memory");
strcpy(student->name, Name);
}
In the main function I called the above functions to work on this structure
Uni university;
How can I cycle through the array using a for loop and the reference
operator?
I think you mean the dereference operator -> but referencing and dereferencing will have nothing to do with cycling through the array; that's what your for-loop is for.
Where should I insert the i?
This:
for (i = 0; i < university->size; i++)
{
if (university->arr->MarkA <= 100)
{
sum += university->arr->MarkA;
count++;
}
}
Should be this:
for (i = 0; i < university->size; i++)
{
if (university->arr[i].MarkA <= 100)
{
sum += university->arr[i].MarkA;
count++;
}
}
Concerning your original question, you should access it as follows:
University university;
...
for (i = 0; i < university.size; i++) {
if (university.arr[i].MarkA <= 100)
{
sum += university.arr[i].MarkA;
count++;
}
...
}
If university is of type University*, however, the code changes a little bit, since you then have to use operator -> to access the university-members:
for (i = 0; i < university->size; i++) {
if (university->arr[i].MarkA <= 100)
{
sum += university->arr[i].MarkA;
count++;
}
...
}
Related
i have this typedef of two dinantial array, and strust the keep his adress
typedef double mat[MAT_SIZE][MAT_SIZE];
typedef struct matList {
char *name;
mat *matrix;
} matList;
and pointer to arry of matList
mat MAT_A, MAT_B, MAT_C, MAT_D, MAT_E, MAT_F;
matList* mats[MAT_COUNT];
int i;
for (i = 0; i < MAT_COUNT; i++) {
mats[i] = NULL;
}
mats[0] = create_mat("MAT_A", &MAT_A);
mats[1] = create_mat("MAT_B", &MAT_B);
mats[2] = create_mat("MAT_C", &MAT_C);
mats[3] = create_mat("MAT_D", &MAT_D);
mats[4] = create_mat("MAT_E", &MAT_E);
mats[5] = create_mat("MAT_F", &MAT_F);
i want to have a function that ill be able to get to the mat and put valuse inside the two dimantial array
i wirte this one
void restart_mat(matList *mats[]) {
int i, j, k;
if (mats == NULL) {
return;
}
for (k = 0; k < MAT_COUNT; k++) {
if (mats[k] != NULL) {
for (i = 0; i < MAT_SIZE; i++) {
for (j = 0; j < MAT_SIZE; j++) {
*(mats[k]->matrix)[i][j] = 0;
}
}
}
}
}
it dose what i ask to but then i get -
*** stack smashing detected ***: ./mainmat terminated
Aborted (core dumped)
The data member matrix has the pointer type double ( * )[MAT_SIZE][MAT_SIZE].
typedef struct matList{
char *name;
mat *matrix;
}matList;
So the pointer matrix must point to a valid two-dimensional array of the type double [MAT_SIZE][MAT_SIZE] and dereferencing the pointer you will get lvalue of the array. Thus the loops will look like
for(k=0; k<MAT_COUNT;k++){
if(mats[k] !=NULL){
for(i=0; i<MAT_SIZE; i++){
for(j=0; j<MAT_SIZE; j++){
( *mats[k]->matrix)[i][j]=0;
^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
}
}
}
Instead of the statement
( *mats[k]->matrix)[i][j]=0;
you may also write
mats[k]->matrix[0][i][j]=0;
As for this statement
*(mats[k]->matrix)[i][j]=0;
then it is equivalent to
*(mats[k]->matrix[i][j] ) = 0;
because the postfix subscript operator [] has a higher precedence than the dereference operator *.
Pay attention to that the function create_mat must return a pointer to a dynamically allocated object of the type matList.
The 2D arrays MAT_A, MAT_B, MAT_C, MAT_D, MAT_E, MAT_F are defined as local objects with automatic storage in the function that calls create_mat. A pointer to these arrays is set in the allocated matList structure. These arrays must no longer be referenced after the function returns.
Unless there is a compelling reason to allocate the 2D arrays and the matList structures separately, you should define the matrix as a struct member instead of a pointer:
#include <stdio.h>
#include <stdlib.h>
typedef double mat[MAT_SIZE][MAT_SIZE];
typedef struct matList {
const char *name;
mat matrix;
} matList;
void init_mat(mat m) {
for (int i = 0; i < MAT_SIZE; i++) {
for (int j = 0; j < MAT_SIZE; j++) {
m[i][j] = 0;
}
}
}
matList *create_mat(const char *name) {
matList *tempMat = malloc(sizeof(*tempMat));
if (tempMat != NULL) {
tempMat->name = name;
init_mat(tempMat->matrix);
}
return tempMat;
}
// return non zero if successful
int allocate_matList(matList *mats) {
for (int i = 0; i < MAT_COUNT; i++) {
mats[i] = NULL;
}
mats[0] = create_mat("MAT_A");
mats[1] = create_mat("MAT_B");
mats[2] = create_mat("MAT_C");
mats[3] = create_mat("MAT_D");
mats[4] = create_mat("MAT_E");
mats[5] = create_mat("MAT_F");
return mats[0] && mats[1] && mats[2] &&
mats[3] && mats[4] && mats[5];
}
void restart_matList(matList *mats) {
if (mats != NULL) {
for (int k = 0; k < MAT_COUNT; k++) {
if (mats[k] != NULL)
init_mat(mats[k]->matrix);
}
}
}
Been working on this for 3 days and no progress, I'm still new to C.
What I'm trying to do is after getting data and stored into the struct RiderInfo.
void ridertopsort() is sorting based on the time.
void gettop() is to read what the racelength is and then store the data into tb struct. For now I'm trying to just figure out how to store the name only.
the 2nd and 3rd else if statement are bit different because I tried both ways in trying to copy name from Riderinfo to tb.
I'm not sure what I'm getting wrong, I can run the code but it exits directly without giving me any results and I know the problem is within the gettop() function.
struct shorrt
{
char topname[3][81];
int topagegroups[3];
int tophrs[3];
int topmin[3];
char botname[3][81];
int botagegroups[3];
int bothrs[3];
int botmins[3];
};
struct medium
{
char topname[3][81];
int topagegroups[3];
int tophrs[3];
int topmin[3];
char botname[3][81];
int botagegroups[3];
int bothrs[3];
int botmins[3];
};
struct lonng
{
char topname[3][81];
int topagegroups[3];
int tophrs[3];
int topmin[3];
char botname[3][81];
int botagegroups[3];
int bothrs[3];
int botmins[3];
};
struct tb
{
struct shorrt sr;
struct medium mr;
struct lonng lr;
};
struct RiderInfo
{
char name[81];
int age;
char raceLength;
int startTime;
int mountainTime;
int finishTime;
int withdrawn;
};
void ridertopsort(struct RiderInfo info[])
{
int i, j;
struct RiderInfo sorttemp;
for (i = 0; i < 5000; i++) {
for (j = i + 1; j < 5000; j++) {
if ((info[j].finishTime - info[j].startTime) < (info[i].finishTime - info[i].startTime))
{
sorttemp = info[j];
info[j] = info[i];
info[i] = sorttemp;
}
}
}
}
void gettop(struct RiderInfo info[], struct tb topbtm[])
{
int a,i,j,k,m;
m = 0;
i = 0;
a = type();
while(m == 0)
{
if (a == 1 && (info[i].raceLength == 's') || (info[i].raceLength == 'S'))
{
for (j = 0; j < 3; j++)
{
for (k = 0; k < 81; k++)
{
strcpy(info[i].name, topbtm->sr.topname);
if (j == 2)
{
m = 1;
}
}
}
}
else if (a == 2 && (info[i].raceLength == 'm') || (info[i].raceLength == 'M'))
{
for (j = 0; j < 3; j++)
{
for (k = 0; k < 81; k++)
{
strcpy(info[i].name, topbtm->mr.topname);
if (j == 2)
{
m = 1;
}
}
}
}
else if (a == 2 && (info[i].raceLength == 'l') || (info[i].raceLength == 'L'))
{
for (j = 0; j < 3; j++)
{
for (k = 0; k < 81; k++)
{
strcpy(info[i].name[k], topbtm->lr.topname[j][k]);
if (j == 2)
{
m = 1;
}
}
}
}
i++;
}
}
You need to address the fields of the instances of your structure, or use memcpy(). For example, when you write,
void ridertopsort(struct RiderInfo info[])
{
struct RiderInfo sorttemp;
// ...
You either can copy each field, with something like
memcpy(&sorttemp.name, &info[j].name, sizeof(sorttemp.name));
sorttemp.age = info[j].age;
// ...
Or copy the entire structure, with
memcpy(&sorttemp, &info[j], sizeof(sorttemp));
Also consider whether you really want three identical struct declarations. if you really, truly want to keep that, you probably want to code defensively, with a few lines such as,
static_assert( sizeof(sorttemp) == sizeof(info[0]), "" );
This will at least protect you from changing the size of one of your structures in a way that breaks existing code, although not from breaking layout compatibility while leaving the size the same.
I want to produce a random sequence of numbers between a range, for example 100 to 200.
After a while, depending on some events, I want to produce a new sequence between the same range (100 to 200), but this time I want to exclude some numbers. For example I don't want [150,165,170].
And the next time, these excluded numbers may or may not be included in the sequence.
One possible approach could be an array of numbers like this:
int rndm[] {100,101,102,103,...};
and use the index of the array to generate a random number at a time:
random(rndm[0-99]);
But I need to use as few instruction/data structures as possible in order to achieve performance.
I am using C for this code and I use random() or randomSeed(seed) and I want to know what the most efficient approach to handle this issue is, in terms of data structures should be used for the speed and memory.
This solution is efficient in the case that there are not many exclusions during the lifetime, once the exclusion function is quadratic.
There is a struct called RandomArray that holds a pointer to and array with size N. N is the desired size of the sequence. The time and space complexity is linear O(N) for the create function.
When an event happens it shall call the function excludeValue, with a time complexity of O(N) and space complexity of 1.
If it is desired to exclude a bunch of values, the function excludeValues (pay attention to s at the end) shall be called. In this case the complexity is O(N x K) and the space complexity is 1. K is the amount of values that shall be excluded.
#include <stdio.h>
#include <stdlib.h>
struct RandomArray {
int *pData;
size_t dataLen;
int excludedIdx;
};
struct RandomArray *excludeValue(struct RandomArray *pAr, int val) {
size_t i;
for (i = 0; i < pAr->excludedIdx; ++i) {
if (pAr->pData[i] == val) {
pAr->excludedIdx--;
int tmp = pAr->pData[i];
pAr->pData[i] = pAr->pData[pAr->excludedIdx];
pAr->pData[pAr->excludedIdx] = tmp;
// Do test again the position
--i;
}
} return pAr;
}
struct RandomArray *excludeValues(struct RandomArray *pAr, int *pVals, size_t len) {
size_t i;
for (i = 0; i < len; ++i)
excludeValue(pAr, pVals[i]);
}
struct RandomArray *destroyRandomArray(struct RandomArray *pAr) {
if (pAr) {
if (pAr->pData)
free(pAr->pData);
pAr->dataLen = 0;
}
return pAr;
}
struct RandomArray *createRandomArray(
struct RandomArray *pAr,
size_t dataLen,
int lowLimit, int highLimit) {
int i;
int range = (highLimit - lowLimit);
pAr->pData = (int*)malloc(sizeof(int) * dataLen);
pAr->dataLen = dataLen;
srand(time(NULL));
for (i = 0; i < dataLen; ++i) {
pAr->pData[i] = rand() % (range + 1) + lowLimit;
}
// Clear excluded indexs
pAr->excludedIdx = pAr->dataLen; return pAr;
}
void printRandomArray(struct RandomArray *pAr) {
size_t i;
printf("Random Array (len = %d): ", pAr->dataLen);
for (i =0; i < pAr->dataLen; ++i) {
printf(" %d", pAr->pData[i]);
}
printf("\n");
}
void printValidRandomArray(struct RandomArray *pAr) {
size_t i;
printf("Valid Random Array (len = %d): ", pAr->excludedIdx);
for (i =0; i < pAr->excludedIdx; ++i) {
printf(" %d", pAr->pData[i]);
}
printf("\n");
}
void printExcludedRandomArray(struct RandomArray *pAr) {
size_t i;
printf("Excluded Random Array (len = %d): ", pAr->dataLen - pAr->excludedIdx);
for (i = pAr->excludedIdx; i < pAr->dataLen; ++i) {
printf(" %d", pAr->pData[i]);
}
printf("\n");
}
void printAllRandomArray(struct RandomArray *pAr) {
printRandomArray(pAr);
printValidRandomArray(pAr);
printExcludedRandomArray(pAr);
}
int main() {
int lowLimit = 100;
int highLimit = 105;
int arrayLen = 10;
struct RandomArray myAr;
createRandomArray(&myAr, arrayLen, lowLimit, highLimit);
printAllRandomArray(&myAr);
printf("\n");
excludeValue(&myAr, 100);
printAllRandomArray(&myAr);
printf("\n");
excludeValue(&myAr, 101);
printAllRandomArray(&myAr);
printf("\n");
excludeValue(&myAr, 102);
printAllRandomArray(&myAr);
printf("\n");
excludeValue(&myAr, 103);
printAllRandomArray(&myAr);
printf("\n");
excludeValue(&myAr, 104);
printAllRandomArray(&myAr);
printf("\n");
excludeValue(&myAr, 105);
printAllRandomArray(&myAr);
destroyRandomArray(&myAr);
createRandomArray(&myAr, arrayLen, lowLimit, highLimit);
printf("\n\n\n");
printAllRandomArray(&myAr);
printf("\n");
int vals[] = { 102, 105, 104 };
excludeValues(&myAr, vals, sizeof(vals) / sizeof(vals[0]));
printAllRandomArray(&myAr);
destroyRandomArray(&myAr);
}
This was asked here on the Arduino forum but I saw it here too. My answer is in Arduino's flavor of C++ since it was posted there...
Of course, performance varies as the set of excluded numbers grows relative to the set of numbers to be used to create your "new sequence."
void setup() {
Serial.begin(115200);
randomSeed(analogRead(A0));
}
void loop() {
// create an arbitray sized array to be filled with unique values to exclude from desired array
const int arraySize = 5;
int exclusions[arraySize];
for (int i = 0; i < arraySize; i++) {
// fill the array with unique values...
int val;
do {
val = random(100, 200);
} while([&]() {
for (auto j : exclusions) {
if (val == j) {
return true;
}
}
return false;
}());
exclusions[i] = val;
}
Serial.print(F("Exclusion Array: "));
for (int i = 0; i < arraySize; i++) {
Serial.print(exclusions[i]);
if (i < arraySize - 1)
Serial.print(F(", "));
}
Serial.println();
// create a new array of arbitrary length of unique random numbers in >>>the same<<< range as above (but not necessary)
Serial.print(F("Starting...\n"));
uint32_t start = millis();
const int listSize = 32;
int list[listSize];
for (int i = 0; i < listSize; i++) {
// fill the array with unique values that will exclude exclusions[]
int val;
do {
val = random(100, 200);
} while([&]() {
for (auto j : list) {
if (val == j) {
return true;
}
for (auto k : exclusions) {
if (val == k) {
return true;
}
}
}
return false;
}());
list[i] = val;
}
uint32_t end = millis();
Serial.println(end - start);
// OPTIONAL -> lets sort the final arry to make spotting the duplicates easier:
for (int i = 0; i < listSize; i++) {
for (int j = i + 1; j < listSize; j++) {
if (list[j] < list[i]) {
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
// output the final array
Serial.print(F("Final Array: "));
for (int i = 0; i < listSize; i++) {
Serial.print(list[i]);
if (i < listSize - 1)
Serial.print(F(", "));
}
Serial.print(F("\n\n\n"));
delay(1000);
}
I'm trying to make a rehashing function that will work on very large hash tables (with more than 1 million entries) and my current method is very inefficient. Here are my structures
typedef struct {
int id, count, correct, valid;
char* word;
} Entry;
typedef struct {
Entry* arr;
int size, numValid;
} Hash;
Here is how it functions now (very slowly, not using memcpy):
void rehash(Hash* hash) {
Entry* tempArr;
int i;
tempArr = calloc(hash->size, sizeof(Entry));
for(i = 0; i < hash->size; i++) {
if(hash->arr[i].count) {
tempArr[i].count = hash->arr[i].count;
tempArr[i].correct = hash->arr[i].valid;
tempArr[i].word = malloc(strlen(hash->arr[i].word) + 1);
strcpy(tempArr[i].word,hash->arr[i].word);
tempLen++;
}
memcpy(&tempArr[i],&hash->arr[i],sizeof(Entry));
tempArr[i] = hash->arr[i];
}
removeAllEntries(hash);
resize(hash);
for(i = 0; i < (hash->size / 2); i++) {
if(tempArr[i].count > 0) {
addEntry(hash,tempArr[i].word,tempArr[i].count);
/*printf("Added %s with count %d\n",tempArr[i].word,tempArr[i].count);*/
free(tempArr[i].word);
}
}
free(tempArr);
}
I'd prefer to use memcpy, but I can't for the life of me get it to work correctly. Here's what I'm trying (this is the code that doesn't work, and what I'm looking for help with):
void rehash(Hash* hash) {
Entry* tempArr;
int i;
tempArr = calloc(hash->size, sizeof(Entry));
fprintf(stderr,"size: %d\n",hash->size * sizeof(Entry));
memcpy((tempArr),(hash->arr),hash->size * sizeof(Entry));
removeAllEntries(hash);
resize(hash);
for(i = 0; i < (hash->size / 2); i++) {
if(tempArr[i].count > 0) {
addEntry(hash,tempArr[i].word,tempArr[i].count);
/*printf("Added %s with count %d\n",tempArr[i].word,tempArr[i].count);*/
free(tempArr[i].word);
}
}
free(tempArr);
}
I'm sure it's an easy, one-line fix, but I just can't get myself to see it.
void addEntry(Hash* hash, char* tag, int count) {
int value = CHV(hash->size, tag), flag = 1, iter = 0;
int possIndex = findEntry(hash, tag);
/*fprintf(stderr,"AddEntry...\n");*/
if(possIndex >= 0) {
(hash->arr[possIndex].count)++;
return;
}
if((hash->size - hash->numValid) < ((double)hash->size / 10))
{
rehash(hash);
}
while(flag) {
iter++;
if(!(hash->arr[value].valid)) {
hash->arr[value].word = calloc(strlen(tag) + 1, sizeof(char));
strcpy(hash->arr[value].word, tag);
wordsAlloced++;
hash->arr[value].valid = 1;
hash->arr[value].correct = 1;
hash->arr[value].count = count;
flag = 0;
}
else {
value++;
if(value == hash->size) {
value = 0;
}
}
}
hash->numValid++;
}
I think that memcpy is no problem. problem is free all Entry by removeAllEntries(hash); then tempArr[i].word is the double free in free(tempArr[i].word); Because it's copied pointer. Also Using tempArr[i].word in addEntry(hash,tempArr[i].word,tempArr[i].count); is invalid. It's already free'd.
One solution proposes the use of realloc.
replace
void resize(Hash* hash) {
free(hash->arr);
hash->size *= 2;
hash->arr = calloc(hash->size, sizeof(Entry));
//TOTALALLOC += (hash->size * sizeof(Entry));
}
with
void resize(Hash* hash) {
Entry* tempArr;
if((tempArr = realloc(hash->arr, 2 * hash->size * sizeof(Entry)))==NULL){
fprintf(stderr,"failed realloc in resize.\n");
return ;
}
hash->size *= 2;
hash->arr = tempArr;
//TOTALALLOC += (hash->size * sizeof(Entry));
}
Does not rehash for resizing purpose.
Another solution
If rehashing is necessary for some reason. to change in the following
void rehash(Hash* hash) {
Entry* tempArr;
int i;
tempArr = malloc(hash->size * sizeof(Entry));//Initialization isn't required because it is replaced by memcpy.
//fprintf(stderr,"size: %d\n",hash->size * sizeof(Entry));
memcpy(tempArr,hash->arr, hash->size * sizeof(Entry));
//To replicate word
for(i = 0; i < hash->size; i++) {
if(hash->arr[i].count) {
tempArr[i].word = malloc(strlen(hash->arr[i].word) + 1);
strcpy(tempArr[i].word, hash->arr[i].word);
}
}
removeAllEntries(hash);
resize(hash);
for(i = 0; i < (hash->size / 2); i++) {
if(tempArr[i].count > 0) {
addEntry(hash,tempArr[i].word, tempArr[i].count);
/*printf("Added %s with count %d\n",tempArr[i].word,tempArr[i].count);*/
free(tempArr[i].word);
}
}
free(tempArr);
}
If numValid is the represent the valid registration number, I think that it is sufficient to save only a word and count.
typedef struct{
int number;
char name[100];
} Apple
typedef struct{
Apple *apple;
int bit[2];
} AppleArray;
int main(){
AppleArray *aArray;
loadApple(&aArray);
}
loadApple(AppleArray **aArray){
*aArray = NULL;
for(i=0; i<100; i++){
*aArray = realloc(*aArray, (i+1) * sizeof(AppleArray));
/*SEGFAULT here*/
aArray[i]->apple = NULL;
for(j=0; j<2; j++){
aArray[i]->apple = realloc(aArray[i]->apple, sizeof(Apple) * (j+1))
}
}
}
I want to have an AppleArray with specific size. Each AppleArray will have two Apple. However, I get segfault when i assign NULL to aArray[i]->apple. What is the problem there?
EDIT :
loadApple(AppleArray **aArray){
*aArray = malloc(100 * sizeof(AppleArray));
for(i=0; i<100; i++){
/*SEGFAULT here*/
aArray[i]->apple = NULL;
for(j=0; j<2; j++){
aArray[i]->apple = realloc(aArray[i]->apple, sizeof(Apple) * (j+1))
}
}
}
You can call realloc() only on an address that was returned to you by an prior memory allocation function like malloc()or calloc() otherwise it gives you Undefined Behavior.
C99 Standard 7.20.3.4-3: The realloc function:
void *realloc(void *ptr, size_t size);
If ptr is a null pointer, the realloc function behaves like the malloc function for the
specified size. Otherwise, if ptr does not match a pointer earlier returned by a memory
management function, or if the space has been deallocated by a call to the free or
realloc function, the behavior is undefined.
Apart from what Als has mentioned above, you are also having an array out-of-bounds read problem as after your loop, i value will be 100 and you are trying to access aArray[100] whereas in your logic (though incorrect) your would have allocated memory only till aArray[99].
Your loadApple function can be rewritten as below:
loadApple(AppleArray **aArray)
{
*aArray = NULL;
*aArray = malloc(100 * sizeof(AppleArray));
//I have shown index as 0 here just as an example.
(*aArray)[0].apple = NULL;
(*aArray)[0].apple = malloc(2 * sizeof(Apple));
}
This code should also give the same behavior which the logic of your code is trying to achieve.
<pre>
#include "stdio.h"
#include "stdlib.h"
#define MAX_SIZE 5
#define NAME_SIZE 100
#define APPLE_NUM 2
typedef struct
{
int number;
char name[NAME_SIZE];
}Apple;
typedef struct
{
Apple* apple;
int bit[2];
}AppleArray;
void printApple(AppleArray** aArray)
{
int i;
for (i = 0; i < MAX_SIZE; ++i)
{
fprintf(stderr, "%4d: bit[0] = %d, bit[1] = %d\n", i, (*aArray + i)->bit[0], (*aArray + i)->bit[1]);
int j;
for (j = 0; j < APPLE_NUM; ++j)
{
fprintf(stderr, "\tapple[%d]: number = %d, name = %s\n",
j,
(*aArray + i)->apple[j].number,
(*aArray + i)->apple[j].name);
}
printf("\n");
}
}
void loadApple(AppleArray **aArray)
{
*aArray = NULL;
int i;
for(i = 0; i < MAX_SIZE; i++)
{
AppleArray* tmp = (AppleArray*)realloc(*aArray, (i+1) * sizeof(AppleArray));
if (tmp != NULL)
{
*aArray = tmp;
}
else
{
//error
free(*aArray);
*aArray = NULL;
exit(0);
}
/*SEGFAULT here*/
//aArray[i]->apple = NULL;
(*aArray + i)->apple = NULL;
(*aArray + i)->bit[0] = i;
(*aArray + i)->bit[1] = i + 1;
/*
int j;
for (j = 0; j < 2; j++)
{
(*aArray + i)->apple = realloc(aArray[i]->apple, sizeof(Apple) * (j+1));
}
*/
(*aArray + i)->apple = (Apple*)realloc(NULL, sizeof(Apple) * APPLE_NUM);
int j;
for (j = 0; j < APPLE_NUM; ++j)
{
(*aArray + i)->apple[j].number = j;
snprintf( (*aArray + i)->apple[j].name, NAME_SIZE, "apple_%d_%d", i, j);
}
}//for
}
void destroyApple(AppleArray* thiz)
{
if (thiz == NULL)
{
return;
}
int i;
for (i = 0; i < MAX_SIZE; ++i)
{
free(thiz[i].apple);
thiz[i].apple = NULL;
}
free(thiz);
}
int main()
{
AppleArray *aArray;
loadApple(&aArray);
printApple(&aArray);
destroyApple(aArray);
return 0;
}