Help

G

Geoff

I was wondering if anyone could help me with a problem I am having. I
am trying to read in a list of numbers from a file and then sort them
using pointers and malloc() and free(). I know how to do it via
arrays but I am stumbled on how to implement this same solution with
pointers, "malloc" and "free" as I can't use arrays. Below is the
code I have to do it with arrays. If anyone can help give me some
tips on how to implement this code with pointers, "malloc" and "free"
it would be greatly appreciated. Thanks :)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define NUM 7
int main(void)
{

FILE *fp;
FILE *fp2;
int i;
int sorted, swaps = 0;

char empids[NUM][20]; /* I want to use pointers here instead of
arrays and use malloc and free */

char temp[20]; /* I want to use pointers here instead of arrays and
use malloc and free */
int k;

fp2 =fopen("source.dat", "r");
for (k=0; k<NUM; k++)
{
fscanf(fp2, "%s", &empids[k]);
printf("%s\n", empids[k]);
}
fp =fopen("dest.dat", "w");
printf("\n");
printf("\n");

puts("Original employee ids are:");
for (i = 0; i < NUM; i++)
puts(empids);


printf("\n");

/* Bubble sort: */

do {
for (i = 1, sorted = 1; i < NUM; i++)
{
if (strcmp(empids[i-1], empids) > 0)
{
sorted = 0;
strcpy(temp, empids[i-1]);
strcpy(empids[i-1], empids);
strcpy(empids, temp);
swaps++;
}
}
} while (!sorted);

/* Output sorted list: */

puts("Sorted in numerical order, the ids are:\n");
for (i = 0; i < NUM; i++)
{
puts(empids);
fprintf(fp,"%s\n",empids);
}


if (fclose(fp) != 0 || fclose(fp2) != 0)
fprintf(stderr, "Error in closing files\n");

printf("\n%i swaps were performed.\n", swaps);


return 0;
}
 
D

Dan Pop

In said:
I was wondering if anyone could help me with a problem I am having. I
am trying to read in a list of numbers from a file and then sort them
using pointers and malloc() and free(). I know how to do it via
arrays but I am stumbled on how to implement this same solution with
pointers, "malloc" and "free" as I can't use arrays. Below is the
code I have to do it with arrays. If anyone can help give me some
tips on how to implement this code with pointers, "malloc" and "free"
it would be greatly appreciated. Thanks :)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define NUM 7
int main(void)
{

FILE *fp;
FILE *fp2;
int i;
int sorted, swaps = 0;

char empids[NUM][20]; /* I want to use pointers here instead of
arrays and use malloc and free */

The FAQ explains how to do it.
char temp[20]; /* I want to use pointers here instead of arrays and
use malloc and free */

If you don't know how to do this, it's time to read about dynamic memory
allocation in your favourite C book.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,263
Messages
2,571,064
Members
48,769
Latest member
Clifft

Latest Threads

Top