pass by ref using Dynamic allocation

So we are using dynamic allocation now with pass by reference and I was starting to understand pass by ref but he wants me to pass by an allocated feature to print out a set of info and also to decide some other info. I'll post the assignment problem

Assignment problems:
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.

I've started it and I need a print function to pass the pMax allocation to the printcat.

Please help guide me in the right direction cause i know i have some pass by ref info wrong

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>

#define TRUE 1
#define FALSE 0


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

void printCat(CAT);

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

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

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

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

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


}
You seem to use plain C. C does not have references.
pass by ref is likely,

function :
1
2
3
void printCat(CAT &cat)	{
    //you have variable type CAT named cat here, what you do with it effected the cat outside.
}


main(or where calling function) :
1
2
3
CAT catty;
printCat(catty);
// you pass catty to be the cat in the function, your catty will changed following what cat changed. 


All CAT in your code is just Type, you didn't declare any variable for the function.
Last edited on
Topic archived. No new replies allowed.