I wrote a program to output numbers in bytes and addresses of storage of these bytes, but the teacher says that the addresses of the fields are stored in complex data types are not stored sequentially, but my version of the program is not true. Actually the question is how to get the address of the structure field? Code attached:
#include <iostream> #include <stdlib.h> #include <stdio.h> #include <locale.h> using namespace std; typedef unsigned char *byte_ptr; void print_int(byte_ptr start, int size) { cout << "Вывод числа типа int "; for (int i = 0; i < size; i++) printf("\n|%p|%2x|", &start[i], start[i]); } void print_float(byte_ptr start, int size) { cout << "Вывод числа типа float "; for (int i = 0; i < size; i++) printf("\n|%p|%2x|", &start[i], start[i]); } void print_double(byte_ptr start, int size) { cout << "Вывод числа типа double "; for (int i = 0; i < size; i++) printf("\n|%p|%2x|", &start[i], start[i]); } int main() { setlocale(0, ""); struct Numbers { int x; float y; double z; } Byte; Byte.x = 15; Byte.y = 15; Byte.z = 15; print_int((byte_ptr) &Byte.x, sizeof(Byte.x)); cout << endl; print_float((byte_ptr) &Byte.y, sizeof(Byte.y)); cout << endl; print_double((byte_ptr) &Byte.z, sizeof(Byte.z)); cout << endl; system("PAUSE"); return 0; } 
floatanddoublein the structure. - AnT