Thread: Binary search of an array of pointers to structs using pointer arithmetic

  1. #31
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    No they are not the same.

    The first one is a pointer to a pointer to a structure which is assigned the same value as array
    The second one is a pointer to a structure which is assigned the value of what array is pointing to.

    Since array holds pointers to structures when need to use on more level of indirection when doing the search.

    So if the array held integers you'd have int *.
    If the array had pointers to pointers to pointers to pointers to structures (bob ****) we'd need to use bob *****. Having fun yet?

  2. #32
    Registered User
    Join Date
    Jun 2004
    Posts
    17
    oh yeah .. this is gonna get real fun

    Thanks for the quick reply ...

    BTW .. is there another way of writing Bob **low = array;?

  3. #33
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Bob **low = &array[0];

  4. #34
    Registered User
    Join Date
    Jun 2004
    Posts
    17
    Hey Thantos .. I was wondering if you could help me with another problem. I searched the board but couldnt find an answer.

    I have this declared:
    Code:
    typedef char * String;
    I want to get the name of the file to open as a command line argument so I have this:
    Code:
    	if(argc == 2) {
    		fprintf(stderr, "%s \n", argv[1]); // to test
    		input = openFile(argv[1]);
    	}
    This is my parameter list for openFile():
    Code:
    FILE * openFile(String filename)
    In openFile I also print out the name of the file I am trying to open followed by a success or fail message. This is my output.

    Code:
    [mgimbl@localhost project1]$ Main test.dat
    test.dat
    Opening test.dat ... success!
    ****
    Only problem is it doesnt open the file. It says it does, but it really doesnt. When I go to print out what was opened, it says no data is in the program. I dont know what my problem is. Is it because I didnt malloc memory for a new String? When I malloc memory for a String, and then use strcpy I dont know how to dereference argv[1] to copy it into my temp String (and I dont know if that is the approach I am supposed to take) .. could you shed some light on this topic please.
    Last edited by mgimbl; 07-04-2004 at 03:08 PM.

  5. #35
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Can you show us the rest of openFile? From your description, it sounds like you're doing way more than you have to.
    My best code is written with the delete key.

  6. #36
    Registered User
    Join Date
    Jun 2004
    Posts
    17
    sure ...

    Code:
    FILE * openFile(String filename) {
    	fprintf(stderr, "Opening %s ... ", filename);
    	
    	FILE * ptr = fopen(filename, "r");
    	if(ptr == NULL){
    		fprintf(stderr, "failed!\n");
    		return NULL;
    	}
    	
    	fprintf(stderr, "success! \n");	
    	return ptr;
    }

  7. #37
    Registered User
    Join Date
    Jun 2004
    Posts
    17
    found one of my problems and edited it as necessary, but it still doesnt open the file for reading .. it says it does, but it really doesnt. Let me play around with it a little bit more and see if I cant find the other problem
    Last edited by mgimbl; 07-04-2004 at 03:11 PM.

  8. #38
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Okay, firstly, this isn't C (assuming you aren't compiling as C99) because C doesn't allow declarations to be interspersed with statements. All declarations must be at the beginning of a block.

    Secondly, a more flexible approach would be to allow the mode to be passed in as well so that you can do more than just read:
    Code:
    FILE *
    openFile(
      const char *filename,
      const char *mode
      )
    {
      FILE *in;
    
      printf("Opening: %s", filename);
      in = fopen(filename, mode);
      if (!in) {
        printf("...Failed!\n");
        exit(EXIT_FAILURE);
      }
      printf("...Success!\n");
    
      return in;
    }
    And lastly, your code works fine for me. Perhaps the problem is elsewhere and you should post a bare bones program that exhibits the problem.
    My best code is written with the delete key.

  9. #39
    Registered User
    Join Date
    Jun 2004
    Posts
    17
    I think I found it .. it was in another part of the program .. It was the first error that was getting me, sorry about that. Sometimes just taking a little break and coming back can make a world of difference

  10. #40
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    typedef char * String;
    Talk about a dangerous typedef.
    Why do you say its not really opening the file?

  11. #41
    Registered User
    Join Date
    Jun 2004
    Posts
    17
    I didnt declare the typedef char * String ... that was a given, but why would you say thats a dangerous typedef (sorry .. new to c, I used java before so I am not very familiar with this language yet .... as you could very well tell).

  12. #42
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but why would you say thats a dangerous typedef
    It hides the fact that it's a pointer. Typedefs that hide pointer types at all are generally unsafe and confusing, but in this case it's especially unsafe because strings in C are so common and the typedef could cause one to forget the unique attributes of C-style strings. That way lies chaos.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 06:04 AM
  3. binary search and search using binary tree
    By Micko in forum C++ Programming
    Replies: 9
    Last Post: 03-18-2004, 10:18 AM
  4. Array, Linked List, or Binary Tree?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 01-05-2002, 10:07 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM