Learning Sets : How do I place the asterisks and ampersands on function arguments and function calls?

97 views Asked by At

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

1

There are 1 answers

0
Shayne Kelly II On BEST ANSWER

The * is the dereference operator. Using it will give you the value/object that a pointer variable points to.

The & operator will give you the address of a variable. So using & on a pointer will give you the address of the pointer variable (NOT the address of the value of the pointer).

Your functions all take arguments that are pointers to a struct of type SET (SET *A). You declare two pointers to sets (SET *A, SET *B). So you should simply call the function without the * or & operators in the parameters, e.g. initializeSet(A, B).

If you want to access the pointer's value within the function, you should use *A. If you want to access the address of the pointer's value, you should use A.

However, I don't think defining SET as an int will give you the functionality you're looking for (a set of ints). You would have to do something like create a struct for each set element that can hold an INT variable and a pointer to the next element in the SET.