error: invalid initializer with char s[] = name[j];
Code:
#include <stdio.h>
#include <string.h>
#define SIZE 255
#define LEN 255
void reverseWords(char *s);
void reverse(char *begin, char *end);
void namesort ( char name[LEN][SIZE], int last );
void showSortingResult( char name[LEN][SIZE], int last );
// Function main begins program execution
int main( int argc, char *argv[])
{
char name[LEN][SIZE];
char copyofname[LEN][SIZE];
int i,j=0, x=0; // indices of array
int f; // file counter
int last; // index of last item in array
FILE *fpPtr; // fpPtr is pointer
// if there is a filename from commandline
if ( argc > 1 ) {
for (f=1; f<=argc-1; f++){
// fopen opens file, Read text from a filename entered by user
fpPtr = fopen ( argv[f], "r" );
// Read from a file entered by user
while(fgets( name[j], SIZE, fpPtr )!=NULL){
// printf( "name array = %s", name[j] ); // Verified
strcpy( copyofname[x], name[j] ); // Copy string name -> secondname
// printf( "secondname = %s\n", secondname[x] ); // Verified secondname == name
j++; // array increment
x++; // array increment
}
fclose( fpPtr ); // Closes the file
} // end for
// Sent to namesorting Function Prototype to sort by symbol, A-Z. a-z
printf( "\n---- Sorting Process ---- \n" ); //Verification
// last = j-1;
// for (i = last ; i > 0 ; i--){
// for (j = 1 ; j <= i ; j++){
// char s[] = "i.like.this.program.very.much";
char s[] = name[j]; // this part contain error!!
char *temp = s;
printf("Before reverse: %s\n", s);
reverseWords(s);
printf("1st time reverse: %s\n", s);
reverseWords(s);
printf("2nd time reverse: %s\n", s);
getchar();
// } // end j loop
// } // end i loop
// Output result using Function prototype
printf( "\n---- Display the result ---- \n" ); //Verification
showSortingResult( name, j-1 );
printf( "\n---- End of file input part ---- \n" );//Verification
} // end if
else {
// Get manually entered data from user
fpPtr = stdin;
for(j = 0 ; !feof( fpPtr ) ; j++ ){
// Read next record
fgets( name[j], 255, fpPtr );
strcpy( copyofname[x], name[j] );
x++;
}
// Sent to namesorting Function Prototype to sort by symbol, A-Z. a-z
printf("\n---- Sorting Process ---- \n"); //Verification
// reverseWords( name, j-1 ); // Reverse words in the domain
// namesort ( name, j-1 ); // Sort by a to z from the back
// reverseWords( name, j-1 ); // Reverse back the domain (original state)
// Output result using Function prototype
printf("\n---- Display the result ---- \n"); //Verification
showSortingResult( name, j );
printf("\n---- End of manually input part ---- \n"); //Verification
} // end if else
return 0;
} // End Main
// Function to reverse words in the web domain address
void reverseWords(char *s)
{
char *word_begin = s;
char *temp = s; /* temp is for word boundry */
// STEP 1 of the above algorithm
while( *temp )
{
temp++;
if (*temp == '\0')
{
reverse(word_begin, temp-1);
}
else if(*temp == '.')
{
reverse(word_begin, temp-1);
word_begin = temp+1;
}
} /* End of while */
// STEP 2 of the above algorithm
reverse(s, temp-1);
} // End function prototype
// Function to reverse any sequence starting with
// pointer begin and ending with pointer end
void reverse(char *begin, char *end)
{
char temp;
while (begin < end)
{
temp = *begin;
*begin++ = *end;
*end-- = temp;
}
} // End function prototype
// Function Prototype to Sorting by symbol, A-Z, a-z THIS IS CASE SENSITIVE!!! CORRECT IT!!!
void namesort ( char name[LEN][SIZE], int last ){
int i,j; // indices of array
char hold[LEN];
for (i = last ; i > 0 ; i--){
for (j = 1 ; j <= i ; j++){
if (strcmp(name[j],name[j - 1]) < 0){
strcpy(hold,name[j]) ;
strcpy(name[j],name[j - 1]) ;
strcpy(name[j - 1],hold) ;
}
}
}
} // End function prototype
// Function to Printf result
void showSortingResult( char name[LEN][SIZE], int last )
{
int i; // indices of array
// Write array to output
for (i = 0 ; i <= last ; i++)
printf("%s",name[i]) ; // For checking
} // End function prototype
Dear all, my I know what's wrong with this error? I want to copy the value from name[j] to the pointer s.