I am confused as to when to put *
and &
on function arguments and ampersands on function calls and especially confused on pointers. I would also like to do a dynamic allocation in initialize but is it allowed in sets? What I understand is that my set is only an array of integers. I am trying to create a program that has 2 SETS. The sets will store values and can be displayed.
Here is my newbie code :
#include<stdio.h>
#include<conio.h>
#define MAX 5
typedef int SET;
void initializeSet(SET *A, SET *B);
void insertElem(SET *C);
void displayElem(SET *C);
int main()
{
SET *A, *B;
int choice,ins,dis;
do{
printf("\n\nOperation:");
scanf("%d", &choice);
fflush(stdin);
switch(choice){
case 1:
initializeSet(A, B);
break;
case 2:
printf("\nWhich SET would you like to insert? (A or B):\n");
ins = getchar();
fflush(stdin);
toupper(ins);
if(ins == 'A'){
insertElem(A);
break;
}
else if(ins == 'B')
insertElem(B);
else
printf("\nWrong input!");
break;
case 3:
printf("\nWhich SET would you like to display? (A or B):\n");
dis = getchar();
fflush(stdin);
toupper(dis);
if(dis == 'A'){
displayElem(A);
break;
}
else if(dis == 'B')
displayElem(B);
break;
case 4:
exit(0);
default :
printf("\nInvalid input. Please try again.\n");
break;
}
}while(choice!=4)
getch();
return 0;
}
void initializeSet(SET *A, SET *B)
{
// how do I access it here?
}
void insertElem(SET *C)
{
//how do I assign values here?
}
void displayElem(SET *C)
{
//by displaying..does it really require pass by address or is by reference enough?
}
The program is supposed to run like this. After compiling, the 1st operation will be initialize to allocate dynamic space to the 2 sets. Then insert finite values to a given set (passed). And then be displayed what is inside a given set (passed). If only someone can verify if my function calls, function arguments, function prototype argument casting and other stuff I am doing wrong, I can start on programming the functions myself. THANKS