LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-24-2005, 10:54 AM   #1
thelonius
Member
 
Registered: Aug 2005
Location: Montréal
Distribution: Debian Testing, Slackware 10.2
Posts: 136

Rep: Reputation: 15
index of an element in the array ?


Hello,

is it (and how is it) possible in C to get the index of an element in the array ? for example, with the 'bsearch' function.

Thanks

well, i think the solution is quite simple:

Code:
int get_index( type element, type* array, int arraysize) {
  type *buffer;
  buffer = bsearch( &element, array, arraysize, sizeof( type), compare);
  if( buffer == NULL)
    return -1;
  return ((long) buffer - (long) array) / sizeof( type);
}

int compare( void* first, void* second) {
  if( (type*) first == (type*) second)
    return 0; 
  ...
}

Last edited by thelonius; 09-24-2005 at 11:52 AM.
 
Old 09-24-2005, 01:41 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,415

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
I assume this is no longer an open question then...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script Array index value Kedelfor Programming 10 04-29-2009 05:37 AM
number of element in a array of pointer? os2 Programming 7 03-09-2005 12:50 PM
arrays of elements with [gcc4]array type has incomplete element type lmmix Linux - Software 0 02-26-2005 09:07 AM
return index of element in java multidimensional array dave bean Programming 8 11-28-2003 12:00 PM
C++ Six Element Array petercool Programming 2 08-20-2003 12:08 PM


All times are GMT -5. The time now is 07:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration