Michael said:
Michael Mair said:
Michael said:
Hi,
I am trying to pass a function an array of strings, but I am having
trouble getting the indexing to index the strings rather than the
individual characters of one of the strings.
I have declared an array as:
char *stringArray[] = {"one","two","three","a"};
When I pass the array using:
CompareStringArray(*stringArray, 3, min); /*I know I have a magic
number here*/
Passing *stringArray is equivalent to passing stringArray[0]
which is the start address of a string containing "one".
Then try to access the elements with
func(array+i, array+result); /* func(char *,
char *) */
the array+i is indexing through the first string, rather than through the
array of strings.
If you posted actual code, then I would not have to guess whether
func is supposed to be CompareStringArray() even though the number
of parameters changed.
The correct prototype for CompareStringArray() has
"char *StringArray[]" or "char **StringArray" as first parameter.
I dont understand why array+1 = "ne" and not "two"
Probably because you passed the address of "one"...
Cheers
Michael
Ok, following is what I have written.
When I originally wrote this I had **stringArray, not *stringArray in my
function call, but the compiler complained:
"passing arg 1 of CompareStringArray makes pointer from integer without a
cast".
I know I have some magic numbers and stuff, I'm really just trying to sort
out the array stuff though.
int main(){
char *stringArray[] = {"one","two","three","four","five"};
int smallest;
int largest;
smallest = CompareStringArray(*stringArray, 4, min);
largest = CompareStringArray(*stringArray, 4, max);
if(smallest > -1){
printf("\nsmallest is %s at position
%d",stringArray[smallest], smallest);
}else {
printf("\nempty array");
}
if(largest > -1){
printf("\nlargest is %s at position
%d",stringArray[largest], largest);
}else {
printf("\nempty array");
}
fgetc(stdin);
return 0;
}
int max(char *string1, char *string2){
int result = FALSE;
if(strlen(string1) > strlen(string2)){
result = TRUE;
}
return result;
}
int min(char *string1, char *string2){
int result = FALSE;
if(strlen(string1) < strlen(string2)){
result = TRUE;
}
return result;
}
int CompareStringArray(char *array, int size, int (*func)(char*, char*)){
int i;
int result;
if(!strlen(array)){
result = 0; /*arbitrary starting point
for result*/
for(i = 0; i < size; i++){
if(func(array+i, array+result)){ /*check if current is longer
than longest found yet*/
result = i;
}
}
} else {
result = -1; /*zero length array*/
}
return result;
}
/* BEGIN ptr_sort.c */
/*
** lencomp() compares pointers to strings,
** according to string length.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SORT_FUNCTIONS { \
no_sort, "This is the original order of the test array:",\
si_sort, "Stable insertionsort:", \
s38sort, "Nonstable Shellsort, four is after " \
"nine, and eight is before three:", \
q_sort, "Standard library qsort, " \
"unspecified ordering of equal keys:", \
}
#define NUMBERS { \
{"one"},{ "two"},{"three"},{"four"},{"five"}, \
{"six"},{"seven"},{"eight"},{"nine"},{ "ten"}, \
}
#define GT(A, B) (lencomp((A), (B)) > 0)
#define NMEMB (sizeof numbers / sizeof *numbers)
#define SORTS (sizeof s_F / sizeof *s_F)
#define str(s) # s
#define xstr(s) str(s)
#define E_TYPE char *
typedef E_TYPE e_type;
int lencomp(const void *, const void *);
void no_sort(e_type *, size_t);
void si_sort(e_type *, size_t);
void s38sort(e_type *, size_t);
void q_sort(e_type *, size_t);
int main(void)
{
size_t element, sort;
struct sf {
void (*function)(e_type *, size_t);
const char *description;
} s_F[] = SORT_FUNCTIONS;
const e_type numbers[] = NUMBERS;
e_type copy[NMEMB];
puts("/* BEGIN output from ptr_sort.c */\n");
puts("Arrays of type " xstr(E_TYPE)
",\nare being sorted by string length.");
for (sort = 0; sort != SORTS; ++sort) {
putchar('\n');
puts(s_F[sort].description);
memcpy(copy, numbers, sizeof copy);
s_F[sort].function(copy, NMEMB);
for (element = 0; element != NMEMB; ++element) {
puts(copy[element]);
}
}
puts("\n/* END output from ptr_sort.c */");
return 0;
}
int lencomp(const void *a, const void *b)
{
const size_t a_len = strlen(*(e_type *)a);
const size_t b_len = strlen(*(e_type *)b);
return b_len > a_len ? -1 : a_len != b_len;
}
void no_sort(e_type *array, size_t nmemb)
{
array, nmemb;
}
void si_sort(e_type *array, size_t nmemb)
{
e_type temp, *base, *low, *high;
if (nmemb-- > 1) {
base = array;
do {
low = array++;
if (GT(low, array)) {
high = array;
temp = *high;
do {
*high-- = *low;
if (low == base) {
break;
}
--low;
} while (GT(low, &temp));
*high = temp;
}
} while (--nmemb != 0);
}
}
void s38sort(e_type *array, size_t nmemb)
{
e_type temp, *i, *j, *k, *after;
after = array + nmemb;
if (nmemb > (size_t)-1 / 3) {
nmemb /= 3;
}
do {
for (i = array + nmemb; after != i; ++i) {
j = i - nmemb;
if (GT(j, i)) {
k = i;
temp = *k;
do {
*k = *j;
k = j;
if (nmemb + array > j) {
break;
}
j -= nmemb;
} while (GT(j, &temp));
*k = temp;
}
}
nmemb = nmemb != 2 ? 3 * nmemb / 8 : 1;
} while (nmemb != 0);
}
void q_sort(e_type *array, size_t nmemb)
{
qsort(array, nmemb, sizeof *array, lencomp);
}
/* END ptr_sort.c */