generally created a function that creates an array

GLfloat Rainbow_color(bool a, int b) // радужный покрас { if (b == 0) return 0; GLfloat color[b]; // массивы нач с одного а пересчёт с нуля if(a == false) { // вводим одноцветно int i_t =0; for (int i =0; i < b; i++) { if(i_t == 0) { color[i] = 0.8f; i_t = 1; } else if(i_t == 1) { color[i] = 0.21f; i_t = 2; } else if(i_t == 2) { color[i] = 0.14f; i_t = 0; } } return color[b]; } else { return color_prohod(b); } } GLfloat color_prohod(int b) { GLfloat ranbow_color[b]; // разноцветный текст int t1 = b/60; int t2 = b%60; int t3 = t2/10; int t4 = t2%10; // перемен размера перемен остатка перремен остатка разм и размер от размера int prohod = (t1*6)+t3; .... return ranbow_color[b]; } 

I tried to use it in the draw call and decided to put it into an array for it.

 GLfloat cwet_chikl[360]; // попытка номер один cwet_chikl[360] = Rainbow_color(true, 360); // не сработало // попытка номер два cwet_chikl[360] = *Rainbow_color(true, 360); // ошибка // попытка номер три cwet_chikl[360] = &Rainbow_color(true, 360); // ошибка // попытка номер четыре std::memcpy(cwet_chikl[360], Rainbow_color(true,360), sizeof(GLfloat)*360); // пишет ошибка: cannot initialize a parameter of type 'void *' with an lvalue of type 'GLfloat' (aka 'float') // ещё вариант std::memcpy(cwet_chikl, &Rainbow_color(true,360), sizeof(GLfloat)*360); // ошибка: cannot take the address of an rvalue of type 'GLfloat' (aka 'float') 

In general, did not work: (Help !!!!

  • one
    You in Rainbow_color return one number. A bunch of work to get the array back. - AlexGlebe
  • Create an array NOT on the stack, but on the heap ( new or malloc ), then return a pointer to this array. (and not just one number) - AlexGlebe
  • @AlexGlebe mdaaa sadly: 3 can be “waved” through the “external” array, but as I heard it is not stylish and not according to canons, but I don’t know other methods (well, there through links) which way to dig ??? - timob256
  • can use std :: vector <GLfloat>? - Drawn Raccoon
  • So C or C ++? Why are there [C] and [C ++] tags on the question at the same time? - AnT 3:04 pm

1 answer 1

In general, I fixed it, through the link, it works: P

Place of function declaration

  void color_prohod1(GLfloat *cwet_chikl, int b); 

Place call function

 GLfloat cwet_chikl[360]; color_prohod1(cwet_chikl, 360); 

The function itself. Assigning the function of coloring the "drawing" line in the rainbow range of colors.

 void color_prohod1(GLfloat *cwet_chikl, int b) { int t1 = b/30; int t2 = b%30; int prohod = t1/7; qDebug() << "t1 = b/30: " << t1; qDebug() << "t2 = b%30: " << t2; qDebug() << "prohod = t1/7: " << prohod; int s =0; // элемент "позиция " массива прим: ranbow_color[124]; s ==124; int i = 0; int i_t1 = 0; for (int a_g =0; a_g < prohod+1; a_g++) { qDebug() << "2444!!!!!!!!!!!!!!!a_g:" << a_g; if (i == 0) { i = 1; GLfloat per = 0.0; for (int a = 0; a < 10 ; a++) { for (int a_i = 0; a_i < 3; a_i++) { if(a_i == 0) { per += 0.1f; cwet_chikl[s] = per; s++; } if(a_i == 1) { cwet_chikl[s] = 0.0f; s++; } if(a_i == 2) { cwet_chikl[s] = 0.0f; s++; } } } qDebug() << "tyt i:" << i << " cwet_chikl[" << s << "]" << cwet_chikl[s]; i_t1 = i_t1 +1; if(t1 <= i_t1) continue; } if(i == 1){ i = 2; GLfloat per = 0.0; for (int a = 0; a < 10 ; a++) { for (int a_i = 0; a_i < 3; a_i++) { if(a_i == 0) { cwet_chikl[s] = 0.0f; s++; } if(a_i == 1) { per += 0.1f; cwet_chikl[s] = per; s++; } if(a_i == 2) { cwet_chikl[s] = 0.0f; s++; } } } qDebug() << "tyt i:" << i << " cwet_chikl[" << s << "]" << cwet_chikl[s]; i_t1 = i_t1 +1; if(t1 <= i_t1) continue; } ... if (i == 6) { i = 0; GLfloat per = 0.0; for (int a = 0; a < 10 ; a++){ for (int a_i = 0; a_i < 3; a_i++){ if(a_i == 0){ per += 0.1f; cwet_chikl[s] = per; s++; } if(a_i == 1){ cwet_chikl[s] = 0.0f; s++; } if(a_i == 2){ cwet_chikl[s] = 0.0f; s++; } } } qDebug() << "tyt i:" << i << " cwet_chikl[" << s << "]" << cwet_chikl[s]; i_t1 = i_t1 +1; if(t1 <= i_t1) continue; } } // выводим одноцветно int i_t =0; for(int i1 = 0;i1 < t2;i1++) { if(i_t == 0) { cwet_chikl[s] = 0.8f; s++; i_t = 1; } else if(i_t == 1) { cwet_chikl[s] = 0.21f; s++; i_t = 2; } else if(i_t == 2) { cwet_chikl[s] = 0.14f; s++; i_t = 0; } } } 

enter image description here


The simplest example of working with pointers

 #include <stdio.h> #include <stdlib.h> #include <string.h> void addOne(float *buf, int N); int main() { float A[5] = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f}; float *B[5]; // заполним массивы printf("vvod A \n"); addOne(&A, 5); // по адресу printf(" vvod B \n"); addOne(B, 5); // по указателю printf("game ower"); system("pause>null"); return 0; } void addOne(float *Buf, int N) { // memset(Buf, 0, sizeof(Buf)); for (int n = 0; n< N; n++) { Buf[n] = Buf[n] + 1.0f; printf("%f, \n", Buf[n]); } printf("\n"); } 

enter image description here


I noticed that the question intersects with the standard simple question how to put an array into an array. In general, I use two methods. First: brute forcefully

 float S[5] = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f}; float C[5] = {0.0f, 0.0f, 0.0f,0.0f, 0.0f}; for (int i = 0; i < 5; i++){ C[i] = S[i]; printf("%f, \n", S[i]); } 

second (more often): memcpy function

 float S[5] = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f}; float C[5] = {0.0f, 0.0f, 0.0f,0.0f, 0.0f}; memcpy (C, S, sizeof(float)*5);