Errors when compiling. its seem to be complaining about line 52 and i do not get output of second file.(data.words)
strarray[numStrings++]=strdup(word);

$ gcc -Wall -ansi prt.c -o prt
In function ‘main’:
warning: implicit declaration of function ‘strdup’
warning: assignment makes pointer from integer without a cast

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

int compare (const void * a, const void * b)
{
	const char **ia = (const char **)a;
  const char **ib = (const char **)b;
	return strcmp(*ia, *ib);
/*	return -1;*/
}

int main(int argc, char *argv[])
{
	
	if ( argc != 3)
	{
		printf("Usage:\n %s CharLimit File \n",argv[0]);
		
		printf("\tCharLimit is Character Limit b/w 25 & 100\n");
		printf("\tFile is Input File Name\n");
		printf("\n");
		return 0;
	} 
	
	int limit = atoi(argv[1]);
	FILE *fp;
	FILE *fpout;
	
	char **strarray = NULL;
	char word[50];
	int str_len=0;
	int length = 0;
	int count=0,numStrings=0;;

	fpout = fopen("data.out", "w");
	fp = fopen(argv[2], "r");
	if (fp == NULL) 
	{
		printf("file does not exist\n");
		return 0;
	}
	else
	{
		while (!feof(fp))
		{
			fscanf(fp,"%s",word);
			count = count + 1;
			str_len = strlen(word);

			strarray = (char **) realloc (strarray, (numStrings+1) * sizeof(char *));
			strarray[numStrings++]=strdup(word);
		
			length = length+str_len;
			if(length>limit)
			{
				
				fprintf(fpout,"%c",'\n');
				fprintf(fpout, "%s ", word);
				length = str_len;
				length++;
			}
			else
			{
				fprintf(fpout, "%s ", word);
				length++;
			}
		strcpy(word,"");
		}
	}
	fclose(fpout);
	fclose(fp);

	qsort (strarray, numStrings, sizeof(int), compare);
	int tempindex,i;
	char **temp = NULL;
	temp=(char **) realloc (temp, (numStrings) * sizeof(char *));

	FILE *fout;
	fout = fopen("data.words", "w");

	for(i=0;i<numStrings;i++){
		
		tempindex=1;
		while(1){

			if(i+1<numStrings && strcmp(strarray[i],strarray[i+1])==0){
				tempindex++;
				i++;
			}
			else{
			
				fprintf(fout,"%s - %d\n", strarray[i],tempindex);
				break;
			}
		}
	}

	fclose(fout);
return 0;
}

remove the -ansi flag and you won't get those warnings. The only reason I know of to use -ansi it to compile old lagecy code, written last century.

I already tried that, it works but I get seg fault. I am running on x86_64 x86_64 x86_64 GNU/Linux

kinda have to be executed with -ansi, per instructor. Any other ideas??

Where is strdup() defined? How is it defined? It's not an ANSI function.

ahh! something I never expected. Thanks for the tip

Where is strdup() defined? How is it defined? It's not an ANSI function.

strdup is part of the GNU C library which can be found basically every linux machine. It is a declared inside of string.h. It duplicates a string like strcpy, but uses malloc so it can be freed later.

I just tried to compile that code on Ubantu and got the same errors. If you look at /usr/include/string.h you will see that strdup() is conditionally defined

#if defined __USE_SVID || defined __USE_BSD || defined __USE_XOPEN_EXTENDED
/* Duplicate S, returning an identical malloc'd string.  */
extern char *strdup (__const char *__s)
     __THROW __attribute_malloc__ __nonnull ((1));
#endif

As Walt already said strdup() is not an ansi function. You will have to remove the -ansi flag from the command line options in order to use strdup(), or define one of the macros in the #if statement.

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.