Need help alphabetizing a Struct

This is a discussion on Need help alphabetizing a Struct within the C Programming forums, part of the General Programming Boards category; I copied this function directly from my programing book, but it still does not work. The Line temp = tbp->entry[j]; ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    3

    Need help alphabetizing a Struct

    I copied this function directly from my programing book, but it still does not work. The Line temp = tbp->entry[j]; gives me an error when compiling "cannot make assignment of this type"
    here is all the relevent code (I think ) If anyone can tell me what I am doing wrong i would appreciate it.

    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #include <stdlib.h>

    #define TEL_BOOK_SIZE 50
    #define MAX_STRING_SIZE 81
    struct tel_book_element
    {char *name;
    char *telNum;
    };
    typedef struct tel_book_element TelBookElement;
    struct tel_book
    {TelBookElement entry[TEL_BOOK_SIZE];
    int n;
    };
    typedef struct tel_book TelBook;

    ...

    void sort(TelBook *tbp)
    {
    int j,k, small;
    TelBook temp;
    for (j=0;j<tbp->n-1;j++)
    {small = j;
    for (k = j + 1; k < tbp->n;k++)
    if (strcmp(tbp->entry[k].name, tbp->entry[small].name) < 0)
    small = k;
    temp = tbp->entry[j];
    tbp->entry[j] = tbp->entry[small];
    tbp->entry[small] = temp;
    }
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    When you cannot make an assignment, it usually means that you are using incompatable types....

    struct tel_book
    {TelBookElement entry[TEL_BOOK_SIZE];
    int n;
    };

    So, entry[j] is a TelBookElement

    TelBook temp;

    and temp is a TelBook.

    They're incompatable types. One of these declarations will have to change.
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Sayeh
    Guest
    Well, it's not incompatible types. The problem is that 'temp' is already statically defined and allocated on your function's local stackframe. What you are doing is

    allocated struct = allocated struct

    You can't do that. both structs reside at different locations in RAM. The compiler can't say 4 = 5, or 0x00A73458 = 0x00F7394D. Understand? 4 will always be 4, and 5 will always be 5. The hex address will always be 0x00A73458-- it can never be 0x00F7394D.

    To do what you're trying to do, you need to use pointers. One pointer can change it's contents to equal that of another pointer

    The existing code will never work:

    ...
    temp = tbp->entry[j];
    tbp->entry[j] = tbp->entry[small];
    tbp->entry[small] = temp;
    ...

    All three lines are trying to copy entire structs-- C doesn't do that. You have to do that.

    Somehow, I think you have missed an asterisk (*) in the book here or there. If not, I would suggest changing the code to using pointers, rather than full structs.

    ----

    Also, learn to format your typedef struct's a little cleaner-- you're doing it like pascal'er. which is _prior_ to the v1.0 C K&R standard.

    Try this instead--

    struct tel_book_element
    {
    char *name;
    char *telNum;
    }TelBookElement;

    typedef struct tel_book
    {
    TelBookElement entry[TEL_BOOK_SIZE];
    int n;
    }TelBook;

    enjoy.

  4. #4
    Sayeh
    Guest
    Ooops, forgot a typedef--

    typedef struct tel_book_element
    {
    char *name;
    char *telNum;
    }TelBookElement;

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    Seems OK with a small change
    Code:
    void sort(TelBook *tbp) { 
        int j,k, small; 
    //    TelBook temp; 
        TelBookElement temp;
        for(j=0;j<tbp->n-1;j++) {
            small = j; 
            for(k = j + 1; k < tbp->n;k++) 
                if(strcmp( tbp->entry[k].name, tbp->entry[small].name ) < 0) 
                    small = k; 
                temp = tbp->entry[j]; 
                tbp->entry[j] = tbp->entry[small]; 
                tbp->entry[small] = temp; 
        } 
    }
    But check on that inner loop - your element swaps are NOT guarded by the strcmp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 07:13 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  4. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 07:18 AM
  5. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM

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