Print int pointer array error

int temp;

int k=1;
int j=4;
int l=6;
int m= 5;
int n=45;

int *sorting[]= {k,j,l,m,n};

void arraysort();

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
char state1[]= "GRRRGRRR";
char state2[]= "RRGRRRGR";
char state3[]= "GGRRRRRR";
char state4[]= "RRGGRRRR";
char state5[]= "RRRRGGRR";
char state6[]= "RRRRRRGG";
char state7[]= "RGRRRGRR";
char state8[]= "RRRGRRRG";

char *previous[] = {state1, state2, state3, state4, state5, state6, state7, state8 };

Serial.println(previous[7]);

   for(int j=0;j<=4;j++){
   for(int i=0;i<=3;i++){
   if(sorting[i]<sorting[i+1]){
      temp=sorting[i];
      sorting[i]=sorting[i+1];
      sorting[i]=temp;
      
    
}}}
  Serial.println(sorting);
}

I'm coding Arduino mega board and need to print the above sorting array to check it's sorted right or wrong. thank you

Hello
Insert a Serial.println() at POI to see what happens.

1 Like

You are not storing legit pointers in your array and likely getting a warning at compile time (enable all warnings in the preferences)

This would feel better:

int *sorting[]= {&k, &j, &l, &m, &n};

Of course if you have pointers you need to dereference to compare the values being pointed and not the memory addresses so that’s not OK

if(sorting[i]<sorting[i+1]){

(I did not read your code past that point)

1 Like

I'm getting an error sir. I cannot print any in the sorting array. can u help me sir. thanks a lot.

sir, what is the POI

POI = point of interrest, e.g. a counter or logic state

1 Like

Post code, not pictures of code.

Post error messages, not pictures of error messages.

1 Like

sir, i have posted the code. in error msg have too much lines. but i will post in this.

Arduino: 1.8.15 (Windows 10), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"





















C:\Users\PC\Desktop\design project\project\testing_again\testing_again.ino: In function 'void setup()':

testing_again:38:28: error: no matching function for call to 'println(int*&)'

   Serial.println(sorting[0]);

                            ^

In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Stream.h:26:0,

                 from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/HardwareSerial.h:29,

                 from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:233,

                 from sketch\testing_again.ino.cpp:1:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:80:12: note: candidate: size_t Print::println(char) <near match>

     size_t println(char);

            ^~~~~~~

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:80:12: note:   conversion of argument 1 would be ill-formed:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:81:12: note: candidate: size_t Print::println(unsigned char, int) <near match>

     size_t println(unsigned char, int = DEC);

            ^~~~~~~

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:81:12: note:   conversion of argument 1 would be ill-formed:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:82:12: note: candidate: size_t Print::println(int, int) <near match>

     size_t println(int, int = DEC);

            ^~~~~~~

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:82:12: note:   conversion of argument 1 would be ill-formed:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:83:12: note: candidate: size_t Print::println(unsigned int, int) <near match>

     size_t println(unsigned int, int = DEC);

            ^~~~~~~

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:83:12: note:   conversion of argument 1 would be ill-formed:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:84:12: note: candidate: size_t Print::println(long int, int) <near match>

     size_t println(long, int = DEC);

            ^~~~~~~

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:84:12: note:   conversion of argument 1 would be ill-formed:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:85:12: note: candidate: size_t Print::println(long unsigned int, int) <near match>

     size_t println(unsigned long, int = DEC);

            ^~~~~~~

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:85:12: note:   conversion of argument 1 would be ill-formed:

exit status 1

no matching function for call to 'println(int*&)'



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

You're trying to print a pointer.
The print function doesn't know how to do that.

You posted some code, but not the code that caused that particular error.

1 Like

I'm new sir. I don't know to do it. this code doesn't compile and shows an error in the serial print of sorting array.

yes sir, that is the problem. how can I print it.

int temp;

int k=1;
int j=4;
int l=6;
int m= 5;
int n=45;

int *sorting[]= {&k,&j,&l,&m,&n};

void arraysort();

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
char state1[]= "GRRRGRRR";
char state2[]= "RRGRRRGR";
char state3[]= "GGRRRRRR";
char state4[]= "RRGGRRRR";
char state5[]= "RRRRGGRR";
char state6[]= "RRRRRRGG";
char state7[]= "RGRRRGRR";
char state8[]= "RRRGRRRG";

char *previous[] = {state1, state2, state3, state4, state5, state6, state7, state8 };

Serial.println(previous[7]);

   for(int j=0;j<=4;j++){
   for(int i=0;i<=3;i++){
   if(sorting[i]<sorting[i+1]){
      temp=sorting[i];
      sorting[i]=sorting[i+1];
      sorting[i]=temp;
      
    
}}}
  Serial.println(sorting[0]);
}

void loop() {
  // put your main code here, to run repeatedly:

}

There's nothing to be gained by here by printing the value of a pointer, unless you print all of the pointers.
The actual values of pointers are pretty meaningless to us humans.

What do you want the print to tell you?

1 Like

sir, I don't want to print the pointer. I want the k,j,l,m,n value order in the sorting pointer array. I have doing bubble sorting and need to get the highest value variable to the sorting [0] position. in here I was defining the variable values as k=1, l=6. I want to add this knowledge to a project of mine. to actually using this, I need to sort the array with random variable locations inside it. so I need to print the k,j,l,m,n values through the sorting array. is there a way sir.

Just dereference the pointer then.

  Serial.println(*sorting[0]);
1 Like
int temp;

int k=1;
int j=4;
int l=6;
int m= 5;
int n=45;

int *sorting[]= {&k,&j,&l,&m,&n};

void arraysort();

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
char state1[]= "GRRRGRRR";
char state2[]= "RRGRRRGR";
char state3[]= "GGRRRRRR";
char state4[]= "RRGGRRRR";
char state5[]= "RRRRGGRR";
char state6[]= "RRRRRRGG";
char state7[]= "RGRRRGRR";
char state8[]= "RRRGRRRG";

char *previous[] = {state1, state2, state3, state4, state5, state6, state7, state8 };

Serial.println(previous[7]);

   for(int j=0;j<=4;j++){
   for(int i=0;i<=3;i++){
   if(*sorting[i]<*sorting[i+1]){
      temp=*sorting[i];
      *sorting[i]=*sorting[i+1];
      *sorting[i]=temp;
      
    
}}}
   for (int i = 0; i < 5; i++) {
    Serial.println(*sorting[i]);
    delay(500);
}
}

void loop() {
  // put your main code here, to run repeatedly:

}

sir, the error is fixed. but the sorting won't happen. it prints the array but didn't have any kind of sorting. can u help me, sir? thank you a lot.

Your sort code looks off - at the end of each iteration, you should be reducing the size you sort over by one element, because a bubble sort will always end each pass with at least one element being in the right place.

1 Like

yes of course. but it doesnt matter right. it will check all the array for 5 times. if the sorting was write it dont go to the inner loop. at least one variable need to change. but there isnt any kind of change. it just print the declared pattern only

get your bubble sort algorithm right...

int k = 1;
int j = 4;
int l = 6;
int m = 5;
int n = 45;

int *sorting[] = {&k, &j, &l, &m, &n};
const size_t sortingCount = sizeof sorting / sizeof sorting[0];

void setup() {
  Serial.begin(115200);
  Serial.println();

  for (size_t i = 0; i <= sortingCount - 1; i++) {
    for (size_t j = 0; j < sortingCount - i - 1; j++) {
      if (*sorting[j] > *sorting[j + 1]) {
        auto temp = *sorting[j];
        *sorting[j] = *sorting[j + 1];
        *sorting[j + 1] = temp;
      }
    }
  }

  for (size_t i = 0; i < 5; i++) {
    Serial.print(*sorting[i]);
    Serial.write(' ');
  }
  Serial.println();
}

void loop() {}
1 Like

thank you sir. can you explain a bit of the program. I'm a newbie in Arduino

What is it you don't understand? there is almost nothing Arduino related there (besides the use of Serial).

the rest is standard C++ and standard bubble sort algorithm

1 Like