B
bpascal123
Hi,
I'm learning and I entered twice the code below and it 's not working
with the same error messages.This code is a little above my
understanding of C however I'd like to understand the part that talks
about getting inputs into "unsized" char arrays of pointers.
/*This prg manages 10 character strings and sort the total of string
from a to z ...*/
#include <stdio.h> /* printf, gets */
#include <string.h> /* strlen, strcpy, strcmp */
#include <stdlib.h> /* malloc, free, exit */
#include <conio.h> /* getche */
int main(void)
{
char *list[10] ; /* 10 pointers array */
char *temp ; /* for sorting */
char buffer[128] ; /* input */
int i = 0 ; /* count strings */
int m, n, rep ; /* for control */
printf("\n\nINITIALISATION OF A SORTED LIST\n") ;
printf("Maximum 10 entries. Input order :\n") ;
printf("\tLast name, first name (country)\n\n") ;
do /* String entries */
{
printf("\nPerson %d (quit with \'0\'(zero) : ", i+1 ) ;
/* 1° the code first says \"0\" - i changed it to \'0\' - i'm not
sure */
gets(buffer) ;
if (strcmp(buffer, '0') ) /* same as above 1° */
{
if ((list = (char *) malloc(strlen(buffer) + 1)) == NULL) /* +1
because it takes '\0' fromlist */
{
printf("\nMemory full.\n");
if ( i > 0 )
{
printf("\nDisplay current listing ? (o/n) ") ;
rep = getche() ;
if (rep == 'o')
break ;
}
exit(1) ;
}
strcpy(list, buffer) ;
i++ ;
}
} while ( i < 10 && strcmp(buffer, '0') ) ; /* same as 1° */
/***Array sorting ***/
for ( m = i - 1 ; m > 0 ; m-- )
for ( n = 0 ; n < m ; n++ )
if ( strcomp(list, list[n+1]) > 0 )
{
temp = list ;
list = list[n+1] ;
list[n+1] = temp ;
}
/***Display result***/
if ( i > 0 )
{
printf("\n\nLIST OF ENTRIES : \n") ;
for ( m = 0 ; m < i ; m++ )
printf("%d\n", list[m]) ;
}
/***Free memory***/
for ( m = 0 ; m < i ; m++ )
free(list[m]) ;
}
I'm using djgpp on Windows :
I get in function main : line 25and 45 parsing argument 2 of strcmp
makes integer without a cast
line 55 assignement type from incompatible pointer type
line 56 error : incompatible type in assignement
Thanks,
Pascal
I'm learning and I entered twice the code below and it 's not working
with the same error messages.This code is a little above my
understanding of C however I'd like to understand the part that talks
about getting inputs into "unsized" char arrays of pointers.
/*This prg manages 10 character strings and sort the total of string
from a to z ...*/
#include <stdio.h> /* printf, gets */
#include <string.h> /* strlen, strcpy, strcmp */
#include <stdlib.h> /* malloc, free, exit */
#include <conio.h> /* getche */
int main(void)
{
char *list[10] ; /* 10 pointers array */
char *temp ; /* for sorting */
char buffer[128] ; /* input */
int i = 0 ; /* count strings */
int m, n, rep ; /* for control */
printf("\n\nINITIALISATION OF A SORTED LIST\n") ;
printf("Maximum 10 entries. Input order :\n") ;
printf("\tLast name, first name (country)\n\n") ;
do /* String entries */
{
printf("\nPerson %d (quit with \'0\'(zero) : ", i+1 ) ;
/* 1° the code first says \"0\" - i changed it to \'0\' - i'm not
sure */
gets(buffer) ;
if (strcmp(buffer, '0') ) /* same as above 1° */
{
if ((list = (char *) malloc(strlen(buffer) + 1)) == NULL) /* +1
because it takes '\0' fromlist */
{
printf("\nMemory full.\n");
if ( i > 0 )
{
printf("\nDisplay current listing ? (o/n) ") ;
rep = getche() ;
if (rep == 'o')
break ;
}
exit(1) ;
}
strcpy(list, buffer) ;
i++ ;
}
} while ( i < 10 && strcmp(buffer, '0') ) ; /* same as 1° */
/***Array sorting ***/
for ( m = i - 1 ; m > 0 ; m-- )
for ( n = 0 ; n < m ; n++ )
if ( strcomp(list, list[n+1]) > 0 )
{
temp = list ;
list = list[n+1] ;
list[n+1] = temp ;
}
/***Display result***/
if ( i > 0 )
{
printf("\n\nLIST OF ENTRIES : \n") ;
for ( m = 0 ; m < i ; m++ )
printf("%d\n", list[m]) ;
}
/***Free memory***/
for ( m = 0 ; m < i ; m++ )
free(list[m]) ;
}
I'm using djgpp on Windows :
I get in function main : line 25and 45 parsing argument 2 of strcmp
makes integer without a cast
line 55 assignement type from incompatible pointer type
line 56 error : incompatible type in assignement
Thanks,
Pascal