Thread: Something I don't understand...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    39

    Something I don't understand...

    In an example program my professor gave me that reads data from a file about rooms. For example the data file reads:

    Bill Mueller
    129450.93 4
    2:300.5:Living Room
    1:107.0:Bedroom 1
    2:158.3:Master Bedroom
    3:98.3:Kitchen

    The last 4 lines are the lines that have to do with the rooms. The first number is the number of windows, the second (after the colon) is the area, and the last is the name of the room. I have to read data in, store the room info in a dynamically allocated array of structs, sort the array by area (from smallest area to largest), and then display the results.

    In main, he just has variable declarations and calls these 3 functions:

    Code:
    read_file(&rooms, &price, owner, &num, argc, argv);
    sort(rooms, num);
    show_results(rooms, price, owner, num);
    My question is regarding the sort() function. He uses an insertion sort algorithm, but I thought that you will need to pass the address of the rooms struct to sort()? Because you are changing the values of rooms[1], rooms[2], etc., so in order for show_results() to show the sorted results, I would need to pass the address of rooms (&rooms) to sort()? But this works, I just need a little explanation why you don't have to use pointers here. thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well that would depend on how rooms was declared to begin with.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In C, the name of the array, has the address of the first or base element, of the array. You can't increment or decrement it like a normal pointer, but that's why no address of "&" operator, or regular pointer, is needed.

    myarray = address of base element of myarray[]

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Without seeing the rest of the code, all we can do is speculate. rooms may be a pointer, that is assigned in the read_file function - thus making it a double pointer in read_file, then a single pointer is sufficient to modify the content in sort.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    39
    Oh I see, yes rooms is declared "Room *rooms" in main(). thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 10-14-2006, 04:38 AM
  2. Need software to help to understand C source code
    By Kincider in forum C++ Programming
    Replies: 1
    Last Post: 09-28-2006, 09:44 PM
  3. Replies: 13
    Last Post: 08-24-2006, 12:22 AM
  4. Need help to understand x["AC"] in the following code
    By jcourcel in forum C Programming
    Replies: 11
    Last Post: 06-06-2006, 01:13 AM
  5. I don't understand K&R example
    By rllovera in forum C Programming
    Replies: 8
    Last Post: 10-25-2004, 10:45 AM