help for argv argc

Hi C experts,

I have the following code for adding command line option for a program

int main (argc, argv)
int argc;
char *argv[];
{

char *mem_type; //memory type
char *name; //name of the memory
int addr; //address bits
int data; //data bits
int banks; //number of banks
int num_entries; //memory depth
int i; //internal variable
int cren; //cren variable for special cases
int y_add; //y-address bits for ldigram6tri
int x_add; //x-address bits for ldigram6tri
int sdata; //sdata bits for ldigram6tri

//Storing the memory models command line argument data into internal data variable.
for (i = 1; i < argc; i++)
{
if (!strcmp ((argv[i]) , "-type"))
mem_type = argv[i\+1];
/*
.other 2 options similar to above stncmp
.
*/
else if (argc < 9)
{
printf("\nWrong options and/or arguments used.\n");
printf("Please check Syntax and try again.\n\n");
print_message(argv[0]);
return 0;
}
}

//Selector for the memory type:

if \(!strcmp\(mem_type, "ra2_met"\)\) \{ gen\_samsung\_mem\_ra2_met\(name, num_entries, addr, data\);
 \}
else if \(!strcmp \(mem_type,  "ra2g_met"\)\)  \{ gen\_samsung\_mem\_ra2_met\(name, num_entries, addr, data\);
\}               
else \{     printf\("Invalid mem_type selected\\n"\);
       printf\("Please check Syntax and try again.\\n\\n"\);
       print_message\(argv[0]\);
       return 10;
\}

return 0;
}
/*******sub***/
void print_message (argv_t1)

 char argv_t1[];

{

        printf\("Usage: %s &lt;required/options : arguments&gt;\\n\\n", argv_t1\);
        printf\("required  : arguments\\n\\n"\);
        printf\("  -help  : print out command line options.\\n\\n\\n"\);

}

but always get one error or another.

In function �main':
warning: passing argument 1 of �strcmp' makes pointer from integer without a cast
warning: passing argument 1 of �strcmp' makes pointer from integer without a cast
warning: passing argument 1 of �strcmp' makes pointer from integer without a cast
implicit declaration of function �print_message'
At top level:
warning: conflicting types for �print_message'
warning: previous implicit declaration of �print_message' was here

What would be the best way to compare the command line arguments so that I don't get the errors/warning?

Thank you very much for the help.:slight_smile:

What language do you think you are writing in?

You have a K&R main prototype and are using C++ style comments. I recommend ANSI C unless you have a good reason to use K&R. In either case don't use C++ comments unless you are writing C++.

Are you including the correct headers?

Without line numbers it's difficult to do the matching, both "mem_type" and "argv[i]" should be treated as "char *" so I don't see an obvious problem.

If this is a copy and paste, I suggest you look at matching your parentheses where they are mismatched, for example:

if (!strcmp ((argv[i]) , "-type"))

Hi porter
The header that I am using are
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

could you please explain what do you mean by :

Could you please give me a suggestion to change it?
also the error for
warning: passing argument 1 of �strcmp' makes pointer from integer without a cast
is due to this string compare functions:
if (!strcmp ((argv[i]) , "-type"))
mem_type = argv[i\+1];

Any suggestion on that?

Thank you very much for your help and suggestions.

Hi reborg,
Thank you for the reply but I don't think that I have parenthesis problems,

I had no major problem with

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*******sub***/
void print_message (char argv_t1[])
{
	printf("Usage: %s <required/options : arguments>\n\n", argv_t1);
	printf("required : arguments\n\n");
	printf(" -help : print out command line options.\n\n\n");
}

int main(int argc, char *argv[])
{
char *mem_type; /* memory type */
char *name; /* name of the memory */
int addr; /* address bits */
int data; /* data bits */
int banks; /* number of banks */
int num_entries; /* memory depth */
int i; /* internal variable */
int cren; /* cren variable for special cases */
int y_add; /* y-address bits for ldigram6tri */
int x_add; /* x-address bits for ldigram6tri */
int sdata; /* sdata bits for ldigram6tri */

/* Storing the memory models command line argument data into internal data variable. */
	for (i = 1; i < argc; i++) 
	{
		if (!strcmp ((argv) , "-type"))
		{
			mem_type = argv[i+1];
		/*
		.other 2 options similar to above stncmp
		.
		*/
		}
		else if (argc < 9)
		{
			printf("\nWrong options and/or arguments used.\n");
			printf("Please check Syntax and try again.\n\n");
			print_message(argv[0]);
			return 1;
		}
	}

	/* Selector for the memory type: */

	if (!strcmp(mem_type, "ra2_met")) 
	{ 
		gen_samsung_mem_ra2_met(name, num_entries, addr, data);
	}
	else if (!strcmp (mem_type, "ra2g_met")) 
	{ 
		gen_samsung_mem_ra2_met(name, num_entries, addr, data);
	} 
	else 
	{ 
		printf("Invalid mem_type selected\n");
		printf("Please check Syntax and try again.\n\n");
		print_message(argv[0]);
		return 10;
	}

	return 0;
}

compiled as follows

$ gcc testx.c -Wall -Werror
testx.c: In function `main':
testx.c:51: warning: implicit declaration of function `gen_samsung_mem_ra2_met'
testx.c:19: warning: unused variable `banks'
testx.c:22: warning: unused variable `cren'
testx.c:23: warning: unused variable `y_add'
testx.c:24: warning: unused variable `x_add'
testx.c:25: warning: unused variable `sdata'

K&R refers to Kernighan and Ritchie C, the original C before standardised by ANSI.

Your prototypes were K&R, but it was ANSI C that introduced the 'void' type.

C++ comments start with

// a C++ comment

a C comment is

/* a proper C comment */

As gcc actually support C++ the preprocessor supports the C++ style comments. Other compilers will not be so lenient.

I haven't replicated your original problem though.

Hi porter

Yes as I am using gcc, I don't have the C/C++ problems that you mentioned that I could get from other compilers.
thank you for the suggestions.
Regards.