Skip to content

Instantly share code, notes, and snippets.

@toanalien
Created June 5, 2015 17:21
Show Gist options
  • Save toanalien/67c7ce0142ead044eff8 to your computer and use it in GitHub Desktop.
Save toanalien/67c7ce0142ead044eff8 to your computer and use it in GitHub Desktop.
sort an array using pointer
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define MAXLEN 10
#define MAXNUM 5
int main()
{
char cname[MAXNUM][MAXLEN];
char *cptr[MAXNUM];
char *ctemp;
int *j;
int i, ij, icount = 0;
while (icount <MAXNUM)
{
printf("Nhap vao ten nguoi thu %d: ", icount + 1);
gets(cname[icount]);
cptr[icount++] = cname[icount];
}
for (i = 0; i < icount - 1; i++)
for (ij = i + 1; ij < icount; ij++)
if (strcmp(cptr[i], cptr[ij]) > 0)
{
ctemp = cptr[i];
cptr[i] = cptr[ij];
cptr[ij] = ctemp;
}
printf("Danh sachs sau khi da sap xep:\n");
for (i = 0; i < icount; i++)
printf("Ten nguoi thu %d: %s \n", i + 1, cptr[i]);
getch();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment