ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I want to compare two strings in C, where one string is recieved from a program's argument, and the other comes from a variable. I want to execute a command when the strings are equal.
Can someone show me how that can be done please? I have tried several things but I can't get it to work.
#include <string.h>
int main( int argc, char*argv[] )
{
char *local_string;
/* Do something to put a value into local_string */
/* Check to make sure we actually got a commandline argument */
if( argc > 1 )
{
/* Compare the contents of the two strings */
if( strcmp( argv[1], local_string ) == 0 )
{
/* The two strings are identical here - Do your other stuff */
}
else
{
/* The two strings are different here */
}
}
}
I would like to assign the programs argument to a variable and then use that variable with the strcmp command, instead of using argv[1] directly, but I am unable to achieve that.
What is the proper way of doing this? I can't figure it out.
EDIT:
Of course, the above code assumes a number of things:
1. arg_copy is defined as a char *
2. arg_length is some sort of integer values (I always use "unsigned" )
3. the variable 'i' has been properly declared and initialized
4. there is a command-line argument corresponding to i's value
5. the malloc() call succeeds
6. the memset() call succeeds
7. the strncpy() call executes without error (i.e. all chars copied)
Each step can be verified, but I'm too lazy to add it in and it would obfuscate the whole point of the code anyway.
Last edited by Dark_Helmet; 02-27-2007 at 04:07 PM.
strcpy will take 2nd argument as *(argv+1) as strcpy takes a pointer as 2nd arg.
for variable var the size should be =
to strlen(*(argv+1))
I don't mean to keep butting in but the size of the destination buffer needs to be at least strlen(*(argv+1)) + 1 because strlen does not count the null terminator at the end of the string which you will need in your destination buffer,i.e var.
Code:
STRLEN(3) Linux Programmer's Manual STRLEN(3)
NAME
strlen - calculate the length of a string
SYNOPSIS
#include <string.h>
size_t strlen(const char *s);
DESCRIPTION
The strlen() function calculates the length of the string s, not including
the terminating `\0' character.
I would like to assign the programs argument to a variable and then use that variable with the strcmp command, instead of using argv[1] directly, but I am unable to achieve that.
What is the proper way of doing this? I can't figure it out.
Thanks
Ben
this is pretty much what strcpy does:
Code:
char out[256];
for(int i = 0; i < strlen(argv[1]); i++)
out[i] = argv[1][i];
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.