Pointers

closed account (98qiz8AR)
I have passed an array called originalname to the reverseWithPointers function below using a pointer and I have returned output from this function to the main program using a pointer. I would like to use pointers in other areas of this function but I don't know how or where to start.

Can someone explain or show me where else in the function I can use a point?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
void reverseWithPointers(char *originalname, char *reversedname2)       
{
     int i,j;        //declared two variables i & j
     int k = -1;     //set k to -1
     char temps1[10];  //declare a temporary char array
     char temps2[10];  //declare a temporary char array
     //-------------------------------------------------------------------------------------------------
	 //This while loop reads up to the end of the first names...
	 while(originalname[k] != ' ')
     {
            k++;                                             //increment k
            temps1[k] = originalname[k];                     //firs temp array will equal the first names.
     } 
     k++;                                                  //increment k
     //---------------------------------------------------------------
     temps1[k]='\0';   //make temps1[k] = '\0'
     j=0;              //set j to zero..
     //This while loop reads in the last names...
     while(originalname[k] != ' '&& originalname[k] != '\0')
     {   
         reversedname2[j] = originalname[k];   //set copy the last names into the reversedname2[j] array
         k++;j++;                              //increment k and j
     } 
     reversedname2[j]=',' ;            //set reversedname2[j] to a comma
     j++;                              //increment j outside the loop
     reversedname2[j]=' ' ;            //set reversedname2[j] to a space
     k = -1;                           // set to k to -1 to read values starting before zero in the array.
     //This array willl read in the first names
	 while(originalname[k] != ' ')
     {
         k++, j++;                         //increment j and k
         reversedname2[j] = originalname[k];    //set the reversedname2[j] array to the originalname[k] array.
     } 
     reversedname2[j]='\0';            
     //cout << temps2 <<" "<< temps1 <<  endl;

}
¿why are you accessing out of bounds? (line 9 and 29)
¿what's the point of temps{1,2} ?

¿what's your issue?
closed account (98qiz8AR)
The purpose of the line 9 and line 29 is to grab the first name in each index of the array and store them in a temporary array called temps1 and temps2.

My issue is that I wanna use pointers in this array. I have already passed the originalname array as a parameter using a pointer. Now I would like to use pointers in other areas of this function?
Lines 9 and 29 result in undefined behavior. You are accessing out of the bounds of the originalname array. Don't. Do. This.

A correct way to write this function with indices:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void toLastNameFirst( const char* name, char* reformatted )
{
    unsigned i = 0 ;                        // advance i to last name.
    while ( name[i++] != ' ' )
        ;

    unsigned j=0;                           // copy the last name.
    while (name[i] != '\0' )
        reformatted[j++] = name[i++] ;

    reformatted[j++] = ',' ;                // append ", "
    reformatted[j++] = ' ' ;

    i = 0 ;                                 // append first name
    while ( name[i] != ' ' )
        reformatted[j++] = name[i++] ;

    reformatted[j] = '\0' ;
}


With pointers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void toLastNameFirst( const char* name, char* reformatted )
{
    const char* source = name ;             // advance source to last name.
    while ( *source++ != ' ' )
        ;

    while ( *source != '\0' )               // copy the last name.
        *reformatted++ = *source++ ;

    *reformatted++ = ',' ;                  // append ", "
    *reformatted++ = ' ' ;

    source = name ;                         // append first name
    while ( *source != ' ' )
        *reformatted++ = *source++ ;

    *reformatted = '\0' ;
}
closed account (98qiz8AR)
cool, Thanks....

The code without the pointers worked perfectly.

Thanks,
closed account (98qiz8AR)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void reverseWithPointers(char *originalname, char *reversedname2)
{
    char* temp_array = originalname ;   
    while ( *temp_array++ != ' ' );    
    while ( *temp_array != '\0' ){                
        *reversedname2++ = *temp_array++ ;}  
    *reversedname2++ = ',';               
    *reversedname2++ = ' ';                

    temp_array = originalname;                   
    while ( *temp_array != ' ' )        
        *reversedname2++ = *temp_array++; 

    *reversedname2 = '\0';               
}
Topic archived. No new replies allowed.