Имя пользователя:
Пароль:  
Помощь | Регистрация | Забыли пароль?  

Показать сообщение отдельно

Старожил


Сообщения: 251
Благодарности: 3

Профиль | Отправить PM | Цитировать


Хотелось бы все-таки разобраться с моим кодом. Переделал программу так, чтобы в основной программе правильно определялись адреса:
Код: Выделить весь код
#include <Windows.h>
#include <iostream>
#include <string>
#include <tchar.h>

using namespace std;

float** outArray()
{
  float nrow = 3.0;
  float ncol = 5.0;
  float flArray[][5] = { { 1.1f, 1.2f, 1.3f, 1.4f, 1.5f },
                          { 2.1f, 2.2f, 2.3f, 2.4f, 2.5f },
                          { 3.1f, 3.2f, 3.3f, 3.4f, 3.5f } };
  float* out[3];
  out[0] = &flArray[0][0];
  out[1] = &nrow;
  out[2] = &ncol;
  cout << &flArray << endl;
  cout << out[0] << endl;
  cout << *out[0] << endl;
  cout << &flArray[0][0] << endl;
  cout << &flArray[0][1] << endl;
  cout << &flArray[0][1] - &flArray[0][0] << endl;
  cout <<  *(&flArray[0][0] + 1) << endl;
  cout << "---------------------" << endl;
  return out;
}

int _tmain(int argc, _TCHAR* argv[])
{
  setlocale(LC_ALL, "Russian");
  float** out2 = outArray();
  int nrow = (int)(*out2[1]);
  int ncol = (int)(*out2[2]);
  int delta = 1;
  float* fla = out2[0];
  cout << fla << endl;
  cout << fla + delta << endl;
  cout << *(fla + delta) << endl; // не совпадает с *(&flArray[0][0] + 1)
  for (int i = 0; i < nrow; i++)
  {
    for (int j = 0; j < ncol; j++)
    {
      cout << *(fla + i*ncol + j) << '\t';
    }
    cout << endl;
  }
  cin.get();
  return(EXIT_SUCCESS);
}
Однако несмотря на то, что адрес выражения fla + delta равен адресу &flArray[0][1], выводится неправильное значение второго элемента массива *(fla + delta). Что я делаю неправильно?

Отправлено: 12:13, 01-07-2015 | #3