Modifing pointers in a function

I have a function to order some constant char arrays.
I did this to order an array of numbers and it worked fine, but with this i can not find the way to make it work.
I have tried a lot of combinations to pass the elements and so on, but this is the only one that i can compile without errors so far.

The code works fine, even inside the order() function it compares the strings and changes the values (as you can see in the cout) but this changes are not mantained in the passing array. (Changes inside the sort function do apply to the x vector).
For what i know, i should pass the directions of the two elements i want to compare in order() and change what it's writen in those directions, but if i do it explicitly (as i know) i can't compile it.

Thanks for the time and help!

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
 #include <iostream>
#include <cstring>
#include <string.h>
#include <math.h>
#include <stdlib.h>  
#include <stdio.h>     
#include <stdlib.h>    
#include <time.h> 
using namespace std ;

//This program orders numbers passing references

//function to order.The inputs are the adresses where those values are stored.
//Directly changing it's value.
int order(const char C[], const char B[]){
  int i = strcmp(C,B);
  cout << "C is: "<< C << " B is: " << B <<" i is: " << i <<endl;
  if(i<0){
   
      
     const char *d;
//change the order if the IF is fullfilled
	d=C;
	cout <<"D is:  " << d << endl;
       	C=B;
       	B=d;
	cout << "C is: "<< C << " B is: " << B << " d is" << d << endl;	
      }
  return 0;
  }



//function to sort the array. Pointer A pointing to where x variable is stored
const char * sort(const char * A[]){
  int i,j,d;
for (i=0; i<10; i++) { 
    for (j=0; j<10-i; j++) { 
      //condition for not taking into account A[10] which is undefined
      if(j==9){
	continue;
      }
      //call order sending two integers
      order(A[j+1],A[j]);
      cout <<"After order, A[" << j <<"] is: " << A[j] << endl;
    } 
    } 

 

 return A[9];

 }




//Main function
int main(){
  int i;
  const char* x[] = { "blue", "and", "mint", "sand", "ten", "hold", "danke", "figure", "apple", "gain" };

  
 
  //call the sort function
  x[10]=sort(x);

 //print the array to see it is in order
 for(i=0;i<10;i++){
   cout << x[i]<<endl;
 }

 //delete [] x;
 return 0;
  }
In reality the signature of the order is:
int order( const char * C, const char * B );
Lets alias the type and rewrite:
1
2
using T = const char *;
int order( T C, T B );

The C and B are by value arguments. Copies.

Lets look at std::swap. It does the same operation as order (optionally does):
http://www.cplusplus.com/reference/algorithm/swap/

What was the signature similar to?
void swap ( T& C, T& B ); By reference arguments.

That is the answer. If the order() takes references to pointers, then it can change where those pointers of the caller point to.
It worked fine using the int order( const char * &C, const char * &B).

This was passing the references, but now i would like to pass a pointer. Can i do it with this const char?

Edit:

I managed calling

 
order(&A[j+1],&A[j])


and declarating the function order as:

 
int order ( const char **C, const char **B)

Last edited on
You can do that, but is it somehow better?

When you call order ant take addresses, it is like writing:
1
2
3
const char * * a = &A[j+1];
const char * * b = &A[j];
order( a, b );

except that the a and b are unnamed temporary objects.
Then the function call creates by value copies:
1
2
const char * * C = a;
const char * * B = b;


Furthermore, it is now legal to write: order( nullptr, nullptr );
The reference version does not like that.


A pointer type argument can be optional. The function can be called without it (by giving value null and will behave differently).
A reference argument is compulsory.
Topic archived. No new replies allowed.