Originally Posted by
Martin_T
I'm struggling with the whole pointer concept at the moment. For example I have a piece of code for testing the first char of a command line arguement but I can't get it to work.
I have tried different ways but at the minute I have
Code:
#include <stdio.h>
int main(int argc, char *argv[])
{
char input = argv[1];
printf("\n\nInput : %s\n\n", input);
if(input[0]=="M")
{
printf("\nThis is a message\n");
}
else if(input[0] == "L")
{
printf("\nThis is a log-in\n");
}
else
{
printf("\n\nwe have no match\n\n");
}
}
Could someone try and explain to me where I'm going wrong? This version says :
Thanks!
You are having some problems in your code
first of all argv[0], argv[1] are pointer to char array means you need a char array to store argv[0] or argv[1] and so on....
like
Code:
char input[100];
strcpy( input, argv[0]); // copy the whole argv[0] string to input string
and other one is if you are comparing string like input with "M" as i think you know in C/C++ string means double quotes " " and a single char means ' ' single quotes.
So you should use strcmp to compare the strings
Code:
if (strcmp(input, "M") == 0) {
// true
} else {
// false
}