Why doesnt this simple "if" work?  problem with char variable

Posted on 2004-11-13
Last Modified: 2011-09-20
I am learning C, and generally have a pretty good grasp on the basics.  However, the char variables are messing me up.  I dont have a firm grasp on how to use char[] arrays vs char *...

Why wont these if's validate?  The urgency is because I have an assignment due that is failing because of a problem like below
  float RateA, RateB, amount;
  char *MoneyTo_str, *MoneyFrom_str;

int main() {
  MoneyFrom_str = "DOLLARS";
  MoneyTo_str = "EUROS";

  if ( MoneyFrom_str == "DOLLARS") {
    RateA = 2.00; }
      printf(" error MoneyFrom_str\n");
  if ( MoneyTo_str == "EUROS") {
    RateB = 4.00; }
      printf(" error MoneyFrom_str\n");
Question by:vetto
    LVL 13

    Accepted Solution

    You should use strcmp to compare strings.. Check out the following FAQ page for details:

    LVL 55

    Assisted Solution

    by:Jaime Olivares
    >I dont have a firm grasp on how to use char[] arrays vs char *...

    Some hints:
    You must use char[] to create "buffers" where you want to store some strings (usually with strcpy).
    But, if strings previously exist (in another buffer) or is a literal like "DOLLARS", then you don't have to declare an array, just have to point it, a pointer, countersense buffers, don't have a space where to put the string.

    This kind of comparison is not valid in C:

    if ( MoneyTo_str == "EUROS") {

    because MoneyTo_str is a pointer to a string (maybe filled, maybe not), but represents a memory position like 0x12AC568F.
    The literal "EUROS" also represents a memory position, if both are pointing to the same memory position, the comparison will be true, but if both point to different memory positions, EVEN if both memory position contains the SAME string, comparison will be false.

    Author Comment

    Points to both, Gripe answered the question the way I giving me the "book" and saying "read this"

    jaime explained WHY this wont work (thus points)...I am pretty good at a few interpreted languages where a string value is exactly that, and a var is that value -  not a pointer to an area in memory, so you can see my confusion.  Thanks for the explaination.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now