1

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

Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70
  • `fflush(stdin);` is undefined behavior. – Sourav Ghosh Jun 16 '15 at 18:00
  • ... or implementation defined behaviour. – Weather Vane Jun 16 '15 at 18:14
  • See [Using `fflush(stdin)`](http://stackoverflow.com/questions/2979209/using-fflushstdin) for a more nuanced version of the comments above. – Jonathan Leffler Jun 16 '15 at 18:20
  • @JonathanLeffler thanks for the link, but as a side-issue, that type of thing is a difficulty. If I have language extensions which are intended to make life easy (for example `kbhit()` in `conio`) should I totally ignore them on the grounds that (brandishing a C motif) they are not standard? And what about TP libraries? Should we not use them on the grounds they might not be portable, or available for all possible platforms? At what point should we draw a line? – Weather Vane Jun 16 '15 at 18:36
  • @WeatherVane: If the code is using platform-specific features (such as `fflush(stdin)` or `kbhit()`), it is worth mentioning the platform so as to head off criticisms that whatever-feature-it-is won't work in general. It's hard for newcomers (to SO and/or programming) to recognize when what they're using is platform-specific, so we have to be graceful about it -- pointing to where good information can be found. Third-party libraries should be mentioned (URL to where to find them is often good); it inherently alters the generality of any solution. It's tricky! – Jonathan Leffler Jun 16 '15 at 18:40
  • Thank you! I didn't know about that. I always thought that it keeps the inputs and outputs from messing up. – aspiringOne Jun 16 '15 at 21:52

1 Answers1

0

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.

Shayne Kelly II
  • 153
  • 1
  • 6