Thread: How to duplicating the Array & copy tokenPtr to an array

  1. #16
    Registered User
    Join Date
    Apr 2013
    Posts
    35

    Question 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.

  2. #17
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Arrays are not pointers

    "char s[]" is an array of chars, but "name[j]" is a pointer to char.

    You probably want:
    Code:
    char *s = name[j]
    Bye, Andreas

  3. #18
    Registered User
    Join Date
    Apr 2013
    Posts
    35
    After made the above changing, my code become:
    Code:
    // Read text from filenames entered by user OR text input from user and
    // sort in alphabetical format.
    
    
    #include <stdio.h>
    #include <string.h>
    #define SIZE 255
    #define LEN  255
    
    
    //void reverseWords( char *s );
    void reverseWords( char name[SIZE], int last );
    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
      	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		
    
    
    		reverseWords(name[j]);
    		namesort ( name, j-1 );		// Sort by a to z from the back
    
    
    		// 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
    /*	for (i = last ; i > 0 ; i--){
        	for (j = 1 ; j <= i ; j++){
    
    
    		}	// end j loop
    	}	// end i loop
    */
    // void reverseWords( char name[SIZE], int last ){
    
    
    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
    Is there anyone can help me with these errors so that this program can run properly?
    Thanks in advance.

    manual11.c: In function ‘main’:manual11.c:44:3: error: too few arguments to function ‘reverseWords’
    manual11.c:10:6: note: declared here
    manual11.c: At top level:
    manual11.c:88:6: error: conflicting types for ‘reverseWords’
    manual11.c:10:6: note: previous declaration of ‘reverseWords’ was here


  4. #19
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    The line numbers of the posted code don't match the line numbers of the compiler messages.

    Code:
    manual11.c: In function ‘main’:manual11.c:44:3: error: too few arguments to function ‘reverseWords’
    manual11.c:10:6: note: declared here
    manual11.c: At top level:
    manual11.c:88:6: error: conflicting types for ‘reverseWords’
    manual11.c:10:6: note: previous declaration of ‘reverseWords’ was here
    Line 10:
    Code:
    void reverseWords( char name[SIZE], int last );
    Line 44:
    Code:
    reverseWords(name[j]);
    What part of "too few arguments to function ‘reverseWords’ " you don't understand?

    The second error is similar.

    Bye, Andreas

  5. #20
    Registered User
    Join Date
    Apr 2013
    Posts
    35
    Sorry for late reply.

    Quote Originally Posted by AndiPersti View Post
    The line numbers of the posted code don't match the line numbers of the compiler messages.

    Code:
    manual11.c: In function ‘main’:manual11.c:44:3: error: too few arguments to function ‘reverseWords’
    manual11.c:10:6: note: declared here
    manual11.c: At top level:
    manual11.c:88:6: error: conflicting types for ‘reverseWords’
    manual11.c:10:6: note: previous declaration of ‘reverseWords’ was here
    Line 10:
    Code:
    void reverseWords( char name[SIZE], int last );
    Line 44:
    Code:
    reverseWords(name[j]);
    What part of "too few arguments to function ‘reverseWords’ " you don't understand?

    The second error is similar.

    Bye, Andreas
    manual11.c: In function ‘main’:
    manual11.c:44:3: error: too few arguments to function ‘reverseWords’
    manual11.c:10:6: note: declared here
    manual11.c: At top level:
    manual11.c:88:6: error: conflicting types for ‘reverseWords’
    manual11.c:10:6: note: previous declaration of ‘reverseWords’ was here

    Code:
    line 44:		reverseWords(name[j]);
    line 10: void reverseWords( char name[SIZE], int last );
    line 88: void reverseWords(char *s){

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What do you think the error messages are trying to tell you?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    Apr 2013
    Posts
    35
    Quote Originally Posted by laserlight View Post
    What do you think the error messages are trying to tell you?
    Just manage to correct the error with help from someone... But still not very understand about the correct way of link the data in between array to pointer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't a static array copy values from a dynamic array
    By c++noob145 in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2013, 10:25 AM
  2. Copy array into a Dynamic array
    By pantherman34 in forum C Programming
    Replies: 15
    Last Post: 05-01-2010, 10:58 PM
  3. Duplicating specific binary data in array
    By chris.lloyd in forum C Programming
    Replies: 1
    Last Post: 10-21-2006, 09:54 PM
  4. Copy multidimensional array to single array?
    By seepox in forum C Programming
    Replies: 9
    Last Post: 05-08-2006, 11:19 AM
  5. Help to copy Array a into first portion of array b
    By Anna Lane in forum C Programming
    Replies: 4
    Last Post: 11-25-2002, 09:38 PM

Tags for this Thread