error with my bubble sort program dont know where im going wrong

mrGatesmrGates ireland
edited November 2013 in Beginner C/C++

hi , i am suppost to read in a user defined text file then, sort it using bubble sort then print the results into a newfile called sorted.txt created within the program, but when i compile i get error with strcpy, and sayas its expects char** but argument is of type char
here is my code

include <stdio.h>

include <string.h>

include <stdlib.h>

define SIZE 100 /* max size of array */

define MAX_FILENAME 20 /* max length of filename */

void Bubble_sort(char name [], int n);

typedef enum {false,true} bool;

int main()

FILE *pFile;
FILE *fptr;
int i;
char FileName[MAX_FILENAME+1];
char name[SIZE];
int last;

printf("Enter the name of the student records to be sorted\n");

pFile == fopen(FileName,"r"); /* read from the file */
printf("File Open Successful\n");

for(i = 0 ; !feof(pFile ) ;i++ ) {
/* Read the number of elements in the input file /
last = i-1;
if (i > SIZE){/
exit(EXIT_FAILURE); */
fclose (pFile);

    fptr == fopen("C:\\sorted.txt","w");  


void Bubble_sort(char name [SIZE], int i);

{ int i,j;
char temp;
bool done = false ;
while (!done)

  {   done = true;
          for (i = last; i > 0; i--)   
          for (j = 0; j <= i; j++)   
      if (strcmp(name[j],name[j-1]) < 0)  
    {   strcpy(temp,name[j]);                   
          strcpy(name[j],name[j - 1]);          
          strcpy(name[j - 1], temp);              
         done = false;                     

for (i = 0 ; i <= last ; i++)

return (0);



  • The str cpy/cmp error is caused by name being an array of characters. You're trying to compare single characters, but the functions want strings.

    If you're supposed to read many names and store them in an array in order to sort them, then you need another dimension in the declaration of name and in the function prototype.

    char name[MAX_NAMES][MAX_LENGTH] = { { 0 } };

    If you are just sorting an array of characters, then don't use str cmp/cpy

  • yeah im trying to sort an array of names thanks for your help

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!