Thread: Structures & Comparing

  1. #1
    Registered User
    Join Date
    Aug 2001

    Question Structures & Comparing

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


    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??


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > What am I missing?
    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.

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, 02:15 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Comparing structures?
    By in forum C Programming
    Replies: 2
    Last Post: 11-07-2004, 08: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