Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 01-28-2006, 02:28 PM   #1 (permalink)
fp_unit
mike
 
Join Date: Jan 2005
Location: Ottawa, ON
Posts: 79
fp_unit is on a distinguished road
qsort() fails on an array of char* strings

I'm trying to sort an array of strings. I wrote my comparison function and tested it with single char* strings, but when I try and loop over an array of strings it fails, my code crashes internally in the qsort() function and I dont understand why.


Code:
/* sample.cpp */
#include <iostream>
using namespace std;

const int NUM_STRINGS = 10;
int compare(const void* p1, const void* p2);	// for qsort()

int main()
{
    char* arr_strs[] = { "test", "hi", "hello", "debug" };
    char buf[80];

    /* this part fails on the first iteration */
    for(int i = 0; i < NUM_STRINGS; i++)
        qsort(arr_strs[i], strlen(arr_strs[i]), sizeof(char), compare);

    /* but this would work */
    strcpy(buf, arr_strs[0]);
    qsort(buf, strlen(buf), sizeof(char), compare);

    /* I'm not sure if this would work, I havent tested it */
    {
        char* test = "helloworld";
        qsort(test, strlen(test), sizeof(char), compare);
    }
}

int compare(const void* p1, const void* p2)
{
    return (*(char*)p1 - *(char*)p2);
}
fp_unit is offline   Reply With Quote
Old 01-28-2006, 04:15 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Is this a nice hint?
(observe the details)

Code:
#include <iostream>
#include <cstdlib>

using namespace std;

static const int NUM_STRINGS = 4;
int compare(const void* p1, const void* p2);

int main()
{
  char* arr_strs[] = { "test", "hi", "hello", "debug" };

  qsort(arr_strs, 4 , sizeof(char*), compare);

  for(unsigned i = 0; i < NUM_STRINGS; ++i)
  {
    std::cout<<arr_strs[i]<<" "<<std::endl;
  }

  return 0;
}

int compare(const void* p1, const void* p2)
{
  return strcmp(*(char **)p1, *(char **)p2);
}
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Looping search strings. phill2000star PHP 4 07-18-2005 04:37 PM
Array to pointer to pointer frier Standard C, C++ 8 03-25-2005 06:57 PM
Sorting an array of any Comparable objects plague Java 1 03-14-2005 06:24 PM
Simple reverse programm silex Standard C, C++ 3 01-22-2005 08:03 AM
Array of chars of chars of, wait, uh... anon Standard C, C++ 15 01-11-2003 04:15 PM


All times are GMT -8. The time now is 10:07 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting