mohammadzahid
asked on
Passing enum constant as function arguments.
Hello Experts,
I am trying to understand the concept of using enum with functions to control program execution. Can someone please explain how can I use/pass enum constant to a function. What would be the function prototype and the syntax of function call.
Here is the code:
#include<stdio.h>
#define SQL1 "select username,user_id from dba_users";
/* Constants defined */
typedef enum oci {RUNSQL1=1} ociprogram;
/* Function prototypes */
int runSql(ociprogram, char *);
int main()
{
runSql(RUNSQL1,SQL1); /* Call to runSql - passing RUNSQL and SQL1 */
return 0;
}
int runSql(ociprogram RUNSQL1, char *ocisql1)
{
printf("RunSQL = %d\n");
printf("SQL statment = %s\n",ocisql1);
return 0;
}
Thanks!
I have not noticed another error in your function
int runSql(ociprogram RUNSQL1, char *ocisql1)
It is not convenient to name first argument as RUNSQL1, because will confuse you with enum value,
better you can name it 'value'. So your entire function must be:
int runSql(ociprogram value, char *ocisql1)
{
printf("RunSQL = %d\n", value);
printf("SQL statment = %s\n",ocisql1);
return 0;
}
int runSql(ociprogram RUNSQL1, char *ocisql1)
It is not convenient to name first argument as RUNSQL1, because will confuse you with enum value,
better you can name it 'value'. So your entire function must be:
int runSql(ociprogram value, char *ocisql1)
{
printf("RunSQL = %d\n", value);
printf("SQL statment = %s\n",ocisql1);
return 0;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Jaime for quick response and appreciate your help.
Now I am getting error
e:\vstudionet\Myprojects\t estprogram s\testprog rams\main. c(13) : error C2143: syntax error : missing ')' before ';'
at the function call when calling function
runSql(RUNSQL1,SQL1); /* RUNSQL1 is the first const of enum && SQL1 is a string value */
from the main ( ).
Can you please provide syntax of the function call if you don't mind.
Thanks again.
Now I am getting error
e:\vstudionet\Myprojects\t
at the function call when calling function
runSql(RUNSQL1,SQL1); /* RUNSQL1 is the first const of enum && SQL1 is a string value */
from the main ( ).
Can you please provide syntax of the function call if you don't mind.
Thanks again.
The origin of the problem is in the #define
#define SQL1 "select username,user_id from dba_users"; <---- remove semicolon here
#define SQL1 "select username,user_id from dba_users"; <---- remove semicolon here
ASKER
That solved the problem. Thanks again.
Thread closed.
Thread closed.
enum oci {RUNSQL=1}; <--- Defines an enumeration called oci with just one value (RUNSQL)
then funtion prototype but be:
int runSql(enum oci, char *)
But, to avoid mention enum keyword every time, it is define with a typedef:
typedef enum oci {RUNSQL1=1} ociprogram;
now you have a new data type: ociprogram, similar to int or char
Thus, the function declaration is now:
int runSql(ociprogram, char *)
That's means that first argument of this function can be ONLY enumerated values, in this case, unique possibility is RUNSQL1, or 1.
Inside runSql there is a bug, first line must be:
printf("RunSQL = %d\n", RUNSQL1);
It will demonstrate that when you pass RUNSQL1 as first argument, you are really passing value 1.
Hope this help.
Good luck,
Jaime.