>void main()
void main is wrong, main must return an int and nothing else or the program is undefined. The correct definition of main is
int main ( void )
You must also return a value at the end of main, 0 is a portable value for successful termination.
>str[i] = malloc(10);
You use malloc yet I don't see stdlib.h included anywhere, this is undefined behavior and is the cause of your warning. Since malloc is not defined, the compiler assumes it is a function that returns int, yet you assign the return value to a pointer. Include stdlib.h and the problem will be fixed.
>scanf("%s",str[i]) ;
scanf is really unsafe when used like this because there is no way to be sure that you aren't reading more than 10 characters. Buffer overflow is a very real problem. I recommend fgets for reading string data.
>printf(" the name of the first students is %s\n",*(str+0));
This is okay for practice programs, but pointer notation should be avoided when possible because array notation is simpler and easier to understand at a glance:
printf ( " the name of the first students is %s\n", str[0] );
Code:
#include <stdio.h>
#include <stdlib.h>
int main ( void )
{
char *str[3] = {0}; /* NULL the three pointers */
int i;
for ( i = 0; i < 3; i++ ) {
printf ( "Enter the name of a student: " );
if ( ( str[i] = malloc ( 10 ) ) == NULL ) {
printf ( "Error allocating memory\n" );
/*
** Don't do this, free the memory you've allocated
** before terminating the program.
*/
exit ( EXIT_FAILURE );
}
fgets ( str[i], 10, stdin );
}
printf ( "The name of the first student is %s\n", str[0] );
printf ( "The name of the second student is %s\n", str[1] );
printf ( "The name of the third student is %s\n", str[2] );
free ( str[0] );
free ( str[1] );
free ( str[2] );
printf ( "Hit return to exit" );
getchar();
return EXIT_SUCCESS; /* EXIT_SUCCESS is defined in stdlib.h */
}
-Prelude