Dynamic Allocation

So I'm having a hard time getting the concept of this and not really sure where to go with this code. I know it is not right and I don't want to get too far with a ton of incorrect code.

I have worked on A, B, C, and very stuck on D.

The Assignment:

A. Define a struct that represents a CAT as follows:

A CAT has a weight, name, and a neutered flag, i.e., those are the components of the struct. Use a typedef.

B. Declare a CAT* pMax. Allocate space for the CAT from the heap – assign its components to 12 (his weight), “Max”, FALSE (he is not neutered). Look at the cat in the debugger.

C. Write a function called printCat that outputs a CAT neatly. Pass the CAT by REFERENCE. Call your function passing in pMax

D. Write a function called neuter that takes a pointer to a CAT. If the cat is already neutered, print a message to indicate that. If the cat is not neutered, output the comments that the cat might have (G-rated, as I am easily embarassed ) and change the cat’s properties so that it is now a neutered cat. Pass max to your neuter function, and then pass him to the printCat function again to verify that he is now neutered.

E. Declare an array of 10 CATs on the stack. Fill in the cats in a loop, by randomly assigning their properties. Each Cat will have one of these names (below), will weigh between 1 and 30 pounds (randomly), and will either be neutered or not (randomly).

char *names[6] = { “Fluffy” , “Tigger”, “Max”, “Betty”, “Cat-27” , “Jake”};

F. Output the cats by passing them to the printCat function one by one.

G. Neuter all the cats. Output the Cats again.


H. Allocate an array of CATs from the heap:
a. You will use the datatype CAT**.
b. Allow space for 5 CATs.
c. Write a factory function that will allocate the space for a CAT from the heap, and return a pointer to the CAT that was created.
d. Output all of the CATs
e. Neuter the array of cats that was dynamically allocated.
f. Output all of the cats again.


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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  #include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define TRUE 1
#define FALSE 0



typedef struct{
	int weight;
	char name[40];
	int neutered;
}CAT;

CAT* makeACat()	{
	CAT* result;

	result = malloc(sizeof(CAT));

	return result;
}

void printCat(CAT pMax);
void neuterACat(CAT pMax, CAT*);
void randomCat(CAT);


main()	{
	CAT** inventroy;
	size_t length = 39;
	CAT* pMax;

	srand(time(NULL));

	pMax = calloc(2, sizeof (CAT));

	pMax[0].weight = 12;
	strcpy_s(pMax[0].name, length, "Max");
	pMax[0].neutered = FALSE;

	inventory = calloc(10, sizeof(CAT*));

	
	printCat(*pMax, "neutered");
	neuterACat(*pMax, &pMax);
	printCat(*pMax, "neutered");

	system("pause");
}

void printCat(CAT pMax)	{
	if (pMax.neutered == TRUE) {
		pMax.neutered = "Yes";
	}
	if (pMax.neutered == FALSE){
		pMax.neutered = "No";
	}

	printf("				Cat Info\n");
	printf("				========\n\n");
	printf("   Name		       Weight		       Neutered?\n");
	printf("   ====		       ======		       =========\n\n");
	printf("%7s%22i%27s\n\n\n", pMax.name, pMax.weight, pMax.neutered);
}

void neuterACat(CAT pMax, CAT *pNeuter)	{
	if (pMax.neutered == FALSE)	{
		printf("Your Cat has all of its working parts\n\n");
	}
	pNeuter->neutered = TRUE;
}

/*void randomCat(CAT pMax)	{
	CAT
}*/


Any Help would be helpful to at least get me in the right direction
This is a C++ forum. You are posting C code.

The terminology in the instructions is a bit confusing. Read "pointer-to" anywhere you see "reference" in your instructions. So, printCat should take a pointer-to-CAT. If we want to be const correct, it would take a pointer-to-const CAT.

As per the description of neuter, it should take a single parameter of type pointer-to-CAT.
Topic archived. No new replies allowed.