if i create an array of pointers to a structure "struct node" as:
and create "n" number of "linked lists" and assign it to the various struct pointers r[i] using some function with a return type as structure pointer as:
is it wrong to then use the individual array members like r[0],r[1] (which are now pointers to various linked lists) etc. as parameters to a function that accept a structure pointers as parameter like::
the programme goes into an infinite loop whenever i try to display the list pointed by add_result..the functions add() and display() work fine in other cases..
i have a function:
char *pcCityIdToCountryName(ADMIN_DB_DATA *pstHEader, unit uiCityID)
this returns a pointer to CountryName if cityId is given.
to retrieve countryname i give:
char *CountryName;
CountryName = pcCityIdToCountryName(..................);
but when i compile it is giving :... (5 Replies)
All ..
I am having a pointer array . And trying to store the addess into that pointer array . please see below the problem i faced
code:
int cnt1;
char *t_array;
char *f_array;
for(cnt1=0; cnt1<1000; cnt1++)
{
t_array =... (1 Reply)
Dear friends,
First all I applogize for posting such a simple question , as I love this forum and forum participants thats why I am posting my problem here..
Why I am getting error when I remove the commented lines
typedef unsigned int U;
typedef struct {U c; U d;} vec32;
... (1 Reply)
Hello all
im trying to build function that will return void function pointer
what is mean is ( not working )
the main function
void * myClass::getFunction(int type){
if(type==1)
return &myClass::Test1;
if(type==2)
return &myClass::Test2;
}
void myClass::Test1(){... (1 Reply)
Hi.
Problem: I have to parse the payload of a packet. The payload could be in Big Endian Format (network byte order) or little. That depends on a flag present in the header of the packet.
Solution: A horrible solution could be to check for that flag everytime I have to read a field in the... (11 Replies)
Hello,
The purpose of the program is to print a sub string from the prompt inputs. I do not understand why char pointer does not work but char array will for line 40 and Line 41.
./a.out thisisatest 0 8
substring = "thisisat"And my code is:
#include <stdio.h>
#include <stdlib.h>
#include... (29 Replies)
I am struggling with the pointer to 2D-array (cf: 2D array of pointers). Can anybody help me elaborate how the pointer x moves in the memory to access the individual of y, especially the high lighted lines?
I have talked to one of the curators of the forum, but I am still not quite clear.
Here... (1 Reply)
Discussion started by: yifangt
1 Replies
LEARN ABOUT SUNOS
bsearch
bsearch(3C) Standard C Library Functions bsearch(3C)NAME
bsearch - binary search a sorted table
SYNOPSIS
#include <stdlib.h>
void *bsearch(const void *key, const void *base, size_t nel, size_t size, int (*compar)(const void *,const void *));
DESCRIPTION
The bsearch() function is a binary search routine generalized from Knuth (6.2.1) Algorithm B. It returns a pointer into a table (an array)
indicating where a datum may be found or a null pointer if the datum cannot be found. The table must be previously sorted in increasing
order according to a comparison function pointed to by compar.
The key argument points to a datum instance to be sought in the table. The base argument points to the element at the base of the table.
The nel argument is the number of elements in the table. The size argument is the number of bytes in each element.
The comparison function pointed to by compar is called with two arguments that point to the key object and to an array element, in that
order. The function must return an integer less than, equal to, or greater than 0 if the key object is considered, respectively, to be less
than, equal to, or greater than the array element.
RETURN VALUES
The bsearch() function returns a pointer to a matching member of the array, or a null pointer if no match is found. If two or more members
compare equal, which member is returned is unspecified.
USAGE
The pointers to the key and the element at the base of the table should be of type pointer-to-element.
The comparison function need not compare every byte, so arbitrary data may be contained in the elements in addition to the values being
compared.
If the number of elements in the table is less than the size reserved for the table, nel should be the lower number.
The bsearch() function safely allows concurrent access by multiple threads to disjoint data, such as overlapping subtrees or tables.
EXAMPLES
Example 1: Examples for searching a table containing pointers to nodes.
The example below searches a table containing pointers to nodes consisting of a string and its length. The table is ordered alphabetically
on the string in the node pointed to by each entry.
This program reads in strings and either finds the corresponding node and prints out the string and its length, or prints an error message.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node { /* these are stored in the table */
char *string;
int length;
};
static struct node table[] = { /* table to be searched */
{ "asparagus", 10 },
{ "beans", 6 },
{ "tomato", 7 },
{ "watermelon", 11 },
};
main()
{
struct node *node_ptr, node;
/* routine to compare 2 nodes */
static int node_compare(const void *, const void *);
char str_space[20]; /* space to read string into */
node.string = str_space;
while (scanf("%20s", node.string) != EOF) {
node_ptr = bsearch( &node,
table, sizeof(table)/sizeof(struct node),
sizeof(struct node), node_compare);
if (node_ptr != NULL) {
(void) printf("string = %20s, length = %d
",
node_ptr->string, node_ptr->length);
} else {
(void)printf("not found: %20s
", node.string);
}
}
return(0);
}
/* routine to compare two nodes based on an */
/* alphabetical ordering of the string field */
static int
node_compare(const void *node1, const void *node2) {
return (strcmp(
((const struct node *)node1)->string,
((const struct node *)node2)->string));
}
ATTRIBUTES
See attributes(5) for descriptions of the following attributes:
+-----------------------------+-----------------------------+
| ATTRIBUTE TYPE | ATTRIBUTE VALUE |
+-----------------------------+-----------------------------+
|Interface Stability |Standard |
+-----------------------------+-----------------------------+
|MT-Level |MT-Safe |
+-----------------------------+-----------------------------+
SEE ALSO hsearch(3C), lsearch(3C), qsort(3C), tsearch(3C), attributes(5), standards(5)SunOS 5.10 6 Dec 2004 bsearch(3C)