How do I write this code without the malloc function?

// 222.cpp : Defines the entry point for the console application.
//

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

#define N 6
#define MAX_STRLEN 80

void sort(char **a, int n);
char *stringArray[N];

int main(int argc, char *argv[]) {
int i;


printf("Do You Have Any more words:\n");


for (i = 0; i < N; i++) {
	stringArray[i] = (char *) malloc (MAX_STRLEN);
strcpy(stringArray[i]," ");
printf(" ");
fgets(stringArray[i],MAX_STRLEN,stdin);
*strchr(stringArray[i],'\n') = '\0';

}
sort(stringArray,N);
printf("\nSorted:\n");
for (i = 0; i < N; i++) {
puts(stringArray[i]);
free(stringArray[i]);
}

return 0;
}

/* selection sort */
void sort(char **a, int n) {
int min,i,j;
char t[MAX_STRLEN];

for (i = 0; i < n; i++) {
min = i;
for (j = i+1; j < n; j++) {
if (strcmp(a[j],a[min]) < 0) min = j;
}
strcpy(t,a[min]);
strcpy(a[min],a[i]);
strcpy(a[i],t);
}
}

Recommended Answers

All 4 Replies

char stringArray[N][MAX_STRLEN];

char stringArray[N][MAX_STRLEN];

Thank u for your reply the program now crashes

post code and this time use code tags

And format your code. It's very difficult to follow.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.