Structures & Comparing

This is a discussion on Structures & Comparing within the C Programming forums, part of the General Programming Boards category; I have a structure into which I have wrote a record from a binary file e.g. struct customer{ char type; ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    20

    Question Structures & Comparing

    I have a structure into which I have wrote a record from a binary file

    e.g.

    struct customer{
    char type;
    float balance;
    };

    I am wanting to check if the type is I or R.

    Say customer_ptr is a pointer to the structure.

    strcmp won't work:

    strcmp(customer_ptr->type, 'I');

    because of the type mismatch in parameters.

    So what's the best way to do this?

    I'm also having a problem with strcpy. I'm wanting to copy a float value into 'balance' in the structure.

    strcpy(customer_ptr->balance, float);
    Again a type mismatch.

    What am I missing??

    Kitten

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > What am I missing?
    That
    char type;
    float balance;
    are not strings, so you need different comparison functions.

    > strcmp(customer_ptr->type, 'I');
    should be
      if ( customer_ptr->type == 'I' ) {
      // match
      }

    > strcpy(customer_ptr->balance, float);
    should be
      customer_ptr->balance = 123.45;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. comparing structures
    By sleepywalker in forum C Programming
    Replies: 15
    Last Post: 02-16-2009, 01:15 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Comparing structures?
    By difficult.name in forum C Programming
    Replies: 2
    Last Post: 11-07-2004, 07:05 PM
  4. Comparing strings w/in structures
    By vehl in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2001, 10:59 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21