hi everyone,
i was writing an application, with the c language, which asks the user to give the number of strings to sort, and then sort'em, but i had some problems that i can't figure out, would u please help me

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

void getting(char *words[], int num);
void sort(char *words[], int n);

int i=0;

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

	char (*word)[30]=NULL;
	int n=0;

	getting(word, n);
	sort(word, n);

	system("pause");
	return EXIT_SUCCESS;
}

void getting(char *words[], int num)
{
	printf("how many words u wanna sort\t");
	scanf("%ld", &num);
	getchar();

	words=(char *)calloc(num, sizeof(char));

	for(i=0; i<num; i++)
	{
		printf("enter the %ldth word : ", i);
		fgets(&words[i], 30, stdin);
	}

}

void sort(char *words[], int n)
{
	int k=1, sorted=1;
	char tmp[30];

	do
	{
		sorted=1;
		for(i=0; i<n-k; i++)
		{
			if(strcmp(words[i], words[i+1])>0)
			{
				strcpy(tmp, words[i]);
				strcpy(words[i], words[i+1]);
				strcpy(words[i+1], tmp);
				sorted=0;
			}
		}
		k++;
	}
	while(sorted!=1);

	for(i=0; i<n; i++)
		printf("%s\n", words[i]);
}

Recommended Answers

All 8 Replies

Looks like the variable "num" is not returned to main, the variable "n" is never passed the value of num.

That leaves your sorting function with 0 strings that are being asked to sort. Return num and assign it to n, and see if that doesn't help.

Looks like the variable "num" is not returned to main, the variable "n" is never passed the value of num.

That leaves your sorting function with 0 strings that are being asked to sort. Return num and assign it to n, and see if that doesn't help.

i passed num as a pointer but didnt work, my main problem i guess is when getting the strings, i found a solution using "char words[MAXWORDS][MAXLENGTH]" but i wanna do it "char *words[]",
thnx for ur help

If you want to use a pointer, then use a pointer, not the indices.

Read up on the -> operator, and you'll be off and running.

printf("how many words u wanna sort\t");
	scanf("%ld", &num);
	getchar();

	words=(char *)calloc(num, sizeof(char));

Put the above statements in the main() function and the code should be fine.

EDIT: You'll also adjust the scope of some variables. Do that, too. Post any further problems you have.

printf("how many words u wanna sort\t");
	scanf("%ld", &num);
	getchar();

	words=(char *)calloc(num, sizeof(char));

Put the above statements in the main() function and the code should be fine.

EDIT: You'll also adjust the scope of some variables. Do that, too. Post any further problems you have.

i tried it, but im having a break when it goes to "strcmp(words, words[i+1])", it tells me access reading violation or stuff like that


PS: i use visual C

You've got some memory allocation issues in your code. On top of that, you're passing a pointer of a pointer to fgets(), while fgets() expects a pointer to a null-terminated string.

As it’s been previously mentioned that, there are quite a few problems in your code. Which you might have to sit down and think about. I will point you out few error which you have made.

1.The memory allocation for char point word is not right.

Char (*word)[30];
	
	Has to be like this

	Word = malloc( sizeof (char *[30] ) * 10 );
	// Please note you need to do some error checking down, to avoid further problem

2.Reading string through fgets is really a good practice, but at the same time you need to understand the on how to use those function and their parameter list. The fgets function for a void * pointer, but not .void ** pointer. Which means that, the usage of the fgets function should be as follow

fgets( word[i], 30, stdin);

To makes more for easy and, I have written few sample codes which might give you some idea on how to read string in two ways .One is through a Array of char pointer methods and the other one through pointer to pointer methods. Have a look at it. You should get some idea on how to use them.

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

void readstring( char (*word)[30] )
{
     int i;
     for( i = 0; i < 10; i++ )
         fgets( word[i], 30, stdin );
}

int main()
{
    char (*word)[30];
    int i, num;
    
    printf("Enter the number of word you want to sort\n?");
    scanf("%d", &num );
    
    word = malloc( sizeof(char *[30]) * 10 );
    /* dont forget to do some error checking here */
      
    readstring( word );    
    
    for(i=0;i<num;i++)
       printf("%s\n", word[i] );

    free( word );
    getchar();
    return 0;
} 

/* ------------------------------------------------------------------ */

int main()
{
    char **word;
    int num, i;
    
    printf("Enter the number of word you want to sort\n?");
    scanf("%d", &num );
    
    word = malloc( sizeof( char * ) * num );
    /* dont forget to do some error checking here */
    
    for( i = 0; i < num; i++ )
         word[i] = malloc( sizeof( char *) * 30 );
         /* dont forget to do some error checking here */
         
    for( i = 0; i < num; i++ )
         fgets( word[i], 30, stdin );
        
    for(i=0;i<num;i++)
       printf("%s\n", word[i] );
 
    for(i=0;i<num;i++)
       free( word[i] );
    
    free( word );
    
    getchar();
    return 0;
}

ssharish

As it’s been previously mentioned that, there are quite a few problems in your code. Which you might have to sit down and think about. I will point you out few error which you have made.

1.The memory allocation for char point word is not right.

Char (*word)[30];
	
	Has to be like this

	Word = malloc( sizeof (char *[30] ) * 10 );
	// Please note you need to do some error checking down, to avoid further problem

2.Reading string through fgets is really a good practice, but at the same time you need to understand the on how to use those function and their parameter list. The fgets function for a void * pointer, but not .void ** pointer. Which means that, the usage of the fgets function should be as follow

fgets( word[i], 30, stdin);

To makes more for easy and, I have written few sample codes which might give you some idea on how to read string in two ways .One is through a Array of char pointer methods and the other one through pointer to pointer methods. Have a look at it. You should get some idea on how to use them.

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

void readstring( char (*word)[30] )
{
     int i;
     for( i = 0; i < 10; i++ )
         fgets( word[i], 30, stdin );
}

int main()
{
    char (*word)[30];
    int i, num;
    
    printf("Enter the number of word you want to sort\n?");
    scanf("%d", &num );
    
    word = malloc( sizeof(char *[30]) * 10 );
    /* dont forget to do some error checking here */
      
    readstring( word );    
    
    for(i=0;i<num;i++)
       printf("%s\n", word[i] );

    free( word );
    getchar();
    return 0;
} 

/* ------------------------------------------------------------------ */

int main()
{
    char **word;
    int num, i;
    
    printf("Enter the number of word you want to sort\n?");
    scanf("%d", &num );
    
    word = malloc( sizeof( char * ) * num );
    /* dont forget to do some error checking here */
    
    for( i = 0; i < num; i++ )
         word[i] = malloc( sizeof( char *) * 30 );
         /* dont forget to do some error checking here */
         
    for( i = 0; i < num; i++ )
         fgets( word[i], 30, stdin );
        
    for(i=0;i<num;i++)
       printf("%s\n", word[i] );
 
    for(i=0;i<num;i++)
       free( word[i] );
    
    free( word );
    
    getchar();
    return 0;
}

ssharish

thank you very very much, that was very helpful, now my lil program works much better, thnx again

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.