Hey List,
I have what is hopefully a beginners C question which will be easily answered (this following little app is for converting watts, amps and volts);
I have the following code: http://pastebin.com/m30x4G17
The problem I have is although the code works, it is because I have used the strncmp function to compare the first pasted argument and a string value such as "-watts", "-volts" or "-amps" because without it I was getting errors about comparing integers and pointers but I am confused as to why C thinks this.
Using if(argv[1]=="-watts") produces no errors but the if statements are never hit, as in the if statement is never met and the code is never run. How should I be coding this to make these comparisons?
Hello James,
The first thing to know about C is that it doesn't have a built in string type, and strings are in fact an array of chars (characters). When you define a string type you use
char* s;
which is a pointer to a memory location containing an array of chars. If you use a constant string like "-watts" then C will represent this as an array of chars (char[]) internally. No even though the contents of the memory pointed at by the char* and the contents of the array might be the same the variables them self are a pointer (an int internally) and an array of chars. Hence you get your error when trying to compare the pointer (char*) to the constant string (char[]).
The only way to compare strings is the way that you have done, using strncmp.
If you don't want to use strcmp then you could use characters as the first argument rather than strings, though you would have to dereference the argc pointer.
i.e. if (*(argc[1]) == 'w') {} else if (*(argc[1] == 'v') {}
Hope this helps.
Torben
On 16 May 2010 02:04, James Bensley jwbensley@gmail.com wrote:
Hey List,
I have what is hopefully a beginners C question which will be easily answered (this following little app is for converting watts, amps and volts);
I have the following code: http://pastebin.com/m30x4G17
The problem I have is although the code works, it is because I have used the strncmp function to compare the first pasted argument and a string value such as "-watts", "-volts" or "-amps" because without it I was getting errors about comparing integers and pointers but I am confused as to why C thinks this.
Using if(argv[1]=="-watts") produces no errors but the if statements are never hit, as in the if statement is never met and the code is never run. How should I be coding this to make these comparisons?
-- Regards, James.
http://www.jamesbensley.co.cc/
main@lists.alug.org.uk http://www.alug.org.uk/ http://lists.alug.org.uk/mailman/listinfo/main Unsubscribe? See message headers or the web site above!
On 16 May 2010, at 02:04, James Bensley wrote:
Hey List,
I have what is hopefully a beginners C question which will be easily answered (this following little app is for converting watts, amps and volts);
I have the following code: http://pastebin.com/m30x4G17
The problem I have is although the code works, it is because I have used the strncmp function to compare the first pasted argument and a string value such as "-watts", "-volts" or "-amps" because without it I was getting errors about comparing integers and pointers but I am confused as to why C thinks this.
Using if(argv[1]=="-watts") produces no errors but the if statements are never hit, as in the if statement is never met and the code is never run. How should I be coding this to make these comparisons?
-- Regards, James.
http://www.jamesbensley.co.cc/
main@lists.alug.org.uk http://www.alug.org.uk/ http://lists.alug.org.uk/mailman/listinfo/main Unsubscribe? See message headers or the web site above!
Using strncmp() is the Right Way.
You can't use '==' to compare strings in C, because a string literal is just syntactic sugar for a statically allocated char *. So what you're doing when you say 'if(argv[1]=="-watts")' is "allocate memory for the string "-watts", and see if argv[1] points to the same place". If you've got a sane C compiler, this is guaranteed not to be true.
DC.
Thanks to both of you this has cleared things up nicely ta.
I knew it wasn't anything to insane but I think in my self teachings I have read the same instructions written by different people in different manners and got in a pickle but this all seems clear now thanks ;)
On 16/05/10 02:04, James Bensley wrote:
Hey List,
I have what is hopefully a beginners C question which will be easily answered (this following little app is for converting watts, amps and volts);
I have the following code: http://pastebin.com/m30x4G17
The problem I have is although the code works, it is because I have used the strncmp function to compare the first pasted argument and a string value such as "-watts", "-volts" or "-amps" because without it I was getting errors about comparing integers and pointers but I am confused as to why C thinks this.
Using if(argv[1]=="-watts") produces no errors but the if statements are never hit, as in the if statement is never met and the code is never run. How should I be coding this to make these comparisons?
Just to add a little to what others have said, == simply compares two numbers. In your fragment
argv[1] == "-watts"
The 'number' on the LHS is the address of the string passed on the command kine and the 'number' on the RHS is the address in memory of the string literal "-watts". Clearly these two addresses will never be the same so your if statement always fails.
Incidentally, this is such a common requirement ISTR there are some library functions that do these tests for you but I cannot for the life of me remember where they are!
Cheers
Ian
Hi Ian,
Thanks for the info. My problem which is probably obvious is that I am coming from other languages and so trying to teach my self by trying different things such as == with what I thought were two strings is all just trial and error but now I understand my mistakes....Oh, the pains of learning a new language :S
On 16/05/10 11:31, James Bensley wrote:
Hi Ian,
Thanks for the info. My problem which is probably obvious is that I am coming from other languages and so trying to teach my self by trying different things such as == with what I thought were two strings is all just trial and error but now I understand my mistakes....Oh, the pains of learning a new language :S
C was the first high level language a really learnt (ignoring the smattering of Fortran I needed once for a project in the 70s). The thing to remember about C is that, compared to many other languages, it is a relatively low level one, not least because it was designed to write an operating system. So ALL variables are just numbers of one kind or another and strings are simply a number that is a pointer to the memory where a string is stored.
In many ways the power (and dangers) of C lie in its simplicity. At one extreme you can create complex structures and simply refer to the whole by a number (a pointer). At the other extreme you can easily manipulate bits in a variable.
It is for this reason that the libraries exist, to provide the sort of functions that are naturally a part of other high level languages.
As an engineer I like C because it is a powerful tool that still lets me see under the hood.
Cheers
Ian