1 Attachment(s)
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 :redface: