pointers, calloc

May 2, 2012 at 3:59pm
Hi guys,
I wrote this function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pension.h"

// basic struct for the 3 types
struct _DOG1 {
	int dog_id;
	char* dog_name;
	char dog_gender;
	char* first_name;
	char* family_name;
	char* cell_phone;
};
struct _DOG2 {
	DOG1* dog_type2;
	char* date;
};
struct _DOG3 {
	DOG1* dog_type3;
	char* date;
	int days_num;
};
struct _PENSION {
	DOG1* p1;
	DOG2* p2;
	DOG3* p3;
};

PENSION* CreatePension() {
	DOG1* arr1;
	DOG2* arr2;
	DOG3* arr3;
	arr1 = (DOG1*) calloc(MAX_DOGS, sizeof(DOG1));
	arr2 = (DOG2*) calloc(MAX_DOGS, sizeof(DOG2));
	arr3 = (DOG3*) calloc(MAX_DOGS, sizeof(DOG3));
	if ((arr1 == NULL) || (arr2 == NULL) || (arr3 == NULL))
		return NULL;
	PENSION* p;
	p->p1 = arr1;
	p->p2 = arr2;
	p->p3 = arr3;
	return p;

}


There are no errors but when I run the program it stuck.
I understand that it happens because of a memory overflow but I can't figure out what is wrong with the pointers' referencing there.
Do you see any wrong thing?

Thanks,
Noha
May 2, 2012 at 4:07pm
Where does it get stuck, what do you mean?
May 2, 2012 at 4:09pm
I get a message saying: " proj has stopped working. windows is checking for a solution to the problem"

May 2, 2012 at 4:10pm
Try debugging line by line, report back.
May 2, 2012 at 4:27pm
The pointers' values there are zero that's the problem. I changed calloc to malloc but still the same problem.
Why their values are zero?

Thank you!
Last edited on May 2, 2012 at 4:27pm
May 2, 2012 at 4:45pm
Your pension pointer inside the create pension is being used without being initialized here:
p->p1 = arr1;
May 2, 2012 at 4:53pm
Thanks :-)
Topic archived. No new replies allowed.