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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    35

    Question How to duplicating the Array & copy tokenPtr to an array

    Dear fellows, I have few questions here.

    (1) Duplicating the Array
    This is part of my code for duplicating the array from
    name[j] to secondname[x]

    Code:
                // Read from a file entered by user          while(fgets( name[j], 255, fpPtr )!=NULL){    
                    printf( "Name %s\n", name[j] );                // Verfication
                    strcpy( name[j], secondname[x] );    
                    printf( "Second name %s\n", secondname[x] );    // Verification
                    j++;
                    x++;
                }
    When I run the program, how comethe string for secondname[x] does not contain any value inside?


    (2) Copy tokenPtr to an array

    In the followings function prototype, I want to copy the string contain inside the token pointer to an array named tempsort[x], but this process was not success when I use the strcpy method, kindly show me the correct way of dealing with token pointer in the strtok.


    Code:
    void namesort ( char name[LEN][SIZE], int last ){       int  i, j, x;         // indices of array
        char *tokenPtr;    // create char pointer;
        char tempsort[LEN][SIZE];
    
    
        for (i = last ; i > 0 ; i--){
    
    
            for (j = 0 ; j <= last ; j++){
                printf("sorting %s",name[j]);    // For checking
                  for (x = 0 ; x <= last ; x++){
                    tokenPtr = strtok (name[j], ".");
                    while (tokenPtr != NULL){
                        printf("tokenPtr: %s\n", tokenPtr) ;    // For checking                
                        strcpy( tokenPtr,tempsort[x] );            // assint tokenPtr to tempsort
                        printf("tempsort: %s\n", tempsort[x]) ;    // For checking                            
                        tokenPtr = strtok ( NULL, ".");            // get next token    
                    }    // end while
    //                    printf("%s",name[j]) ;    // For checking
                }
            }
        }
    }     // End function prototype

    Thanks in advance
    Attached Files Attached Files
    Last edited by Watervase Chew; 04-16-2013 at 08:34 AM. Reason: Attach the source code file

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