2 Replies - 622 Views - Last Post: 28 November 2011 - 12:09 PM Rate Topic: -----

#1 Tcraw   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-November 11

Sorting names from within an array

Posted 28 November 2011 - 11:35 AM

I am trying to sort names in alphabetical order that have been read in from a file. I have having trouble with the sorting portion of the program. When I go to compile the program it gives me a warning that I am "making pointer from intiger without a cast" and then when I try to run the program it gives me a segmentation fault when I try to run the sorting portion. Any help would be greatly appreciated :smile2:

#include <stdio.h>
#include <string.h>
#define MAX_LEN 25
#define MAX_NAMES 10

int sortAlpha (char lName[], char fName[]);

int main(int argc, char* argv[])
{
 int inputNum,i,j;
 char fName[MAX_LEN];
 char lName[MAX_LEN];
 FILE* spInput;

 if (argc < 2)
    printf("Not enough arguments entered\n");
 else if (!(spInput = fopen("input.txt", "r")))
    printf("Unable to read file");
 else
  {
        for(i=0;i<10;i++)
        {
           fscanf(spInput, "%s%s", &lName[i], &fName[i]);
           printf("%s %s\n", lName[i], fName[i]);
        }
         
       printf("Please enter one of the following options\n");
       printf("1: Sort in ascending alphabetical order by last name\n");
       printf("2: Print the collection of names\n");
       printf("3. Quit\n");
       scanf("%d", &inputNum);
       
  }    
  if (inputNum == 1)
     sortACSII(lName, fName);
return 0;
}
 
int sortAlpha (char lName[], char fName[])
{
 int i,j;
 char temp[MAX_LEN];

 for(i=0;i < MAX_NAMES - 1; i++)
  {
    for(j=0; j < MAX_NAMES; j++)
      if (strcmp(lName[i], lName[i+1]) > 0)
        {
          strcpy(temp, lName[i]);
          strcpy(lName[i],lName[i+1]);
          strcpy(lName[i+1],temp);
        }
       printf("%s", lName[i]);
  }
  
} 



Is This A Good Question/Topic? 0
  • +

Replies To: Sorting names from within an array

#2 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7506
  • View blog
  • Posts: 15,556
  • Joined: 16-October 07

Re: Sorting names from within an array

Posted 28 November 2011 - 12:04 PM

This is an array of characters:
char fName[MAX_LEN];



Either of these would be an array of strings:
char fName[MAX_NAMES][MAX_LEN];
char *fName[MAX_NAMES];



You fscanf isn't reading strings, but characters. It is surprising it compiled, actually.

The most likely reason you're getting a segfault is because you're trying to copy a string that isn't there and has no null terminator.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7506
  • View blog
  • Posts: 15,556
  • Joined: 16-October 07

Re: Sorting names from within an array

Posted 28 November 2011 - 12:09 PM

I should probably add that parallel arrays are a very bad choice here. Go with an array of struct:
typedef struct {
	char fName[MAX_LEN], lName[MAX_LEN];
} Name;

typedef struct {
	Name items[MAX_NAMES];
	int size;
} Names;

void sortAlpha(Names *);
void printList(Names *);
void loadList(const char *filename, Names *);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1