- Nothing Phone (3) – tervezett kaotika
- One mobilszolgáltatások
- Google Pixel topik
- Yettel topik
- Samsung Galaxy S21 FE 5G - utóirat
- Redmi Note 14 Pro+ 5G - a tizenhármas átka
- Hivatalosnak tűnő képeken a Galaxy S25 FE
- Samsung Galaxy Watch5 Pro - kerek, de nem tekerek
- Samsung Galaxy Watch (Tizen és Wear OS) ingyenes számlapok, kupon kódok
- Milyen okostelefont vegyek?
Új hozzászólás Aktív témák
-
Jester01
veterán
Ha megoldható, akkor a tömböt add át, és a függvényen belül legyen a .adatx. Ez gondolom menne, tehát valamiért nem megvalósítható.
Alternatívaként csinálhatsz accessor függvényt, varázsolhatsz offsetof segítségével avagy ha esetleg véletlen c++ és nem c akkor member pointerrel.
#include <iostream>
using namespace std;
struct Foo
{
int x;
int y;
};
int sum0_x(Foo* array, int count)
{
int r = 0;
for(int i = 0; i < count; i += 1)
{
r += array[i].x;
}
return r;
}
int sum0_y(Foo* array, int count)
{
int r = 0;
for(int i = 0; i < count; i += 1)
{
r += array[i].y;
}
return r;
}
int sum1(Foo* array, int count, int (*accessor)(Foo*))
{
int r = 0;
for(int i = 0; i < count; i += 1)
{
r += accessor(array + i);
}
return r;
}
int sum2(Foo* array, int count, int member)
{
int r = 0;
for(int i = 0; i < count; i += 1)
{
r += *(int*)((char*)(array + i) + member);
}
return r;
}
int sum3(Foo* array, int count, int (Foo::*member))
{
int r = 0;
for(int i = 0; i < count; i += 1)
{
r += array[i].*member;
}
return r;
}
int accessor_x(Foo* foo)
{
return foo->x;
}
int accessor_y(Foo* foo)
{
return foo->y;
}
int main()
{
Foo foos[2];
foos[0].x = 1;
foos[0].y = 2;
foos[1].x = 3;
foos[1].y = 4;
cout << "sum0(x) = " << sum0_x(foos, 2) << endl;
cout << "sum0(y) = " << sum0_y(foos, 2) << endl;
cout << "sum1(x) = " << sum1(foos, 2, accessor_x) << endl;
cout << "sum1(y) = " << sum1(foos, 2, accessor_y) << endl;
cout << "sum2(x) = " << sum2(foos, 2, offsetof(Foo, x)) << endl;
cout << "sum2(y) = " << sum2(foos, 2, offsetof(Foo, y)) << endl;
cout << "sum3(x) = " << sum3(foos, 2, &Foo::x) << endl;
cout << "sum3(y) = " << sum3(foos, 2, &Foo::y) << endl;
return 0;
}
Új hozzászólás Aktív témák
● olvasd el a téma összefoglalót!
● ha kódot szúrsz be, használd a PROGRAMKÓD formázási funkciót!
- Autós topik
- PlayStation 5
- A fociról könnyedén, egy baráti társaságban
- Nothing Phone (3) – tervezett kaotika
- Milyen autót vegyek?
- Motorolaj, hajtóműolaj, hűtőfolyadék, adalékok és szűrők topikja
- Samsung Galaxy Felhasználók OFF topicja
- Óvodások homokozója
- Motoros topic
- exHWSW - Értünk mindenhez IS
- További aktív témák...
- LG UltraGear 27GR93U-B 27 4K UHD Gaming Monitor garanciával
- Kingston Fury Beast 32GB 6000MHz DDR5 CL30 RAM (KF560C30BBE-32)
- Eladó iPhone 13 Blue 128Gb független Akku 100%
- Új Dell 13 Inspiron 5310 FHD+ IPS i5-11300H 4.4Ghz 8GB 256GB Intel Iris XE Graphics Win11 Garancia
- Dell 16 Inspiron 5625 FHD+ IPS Ryzen5 5625U 4.3Ghz 16GB 512GB SSD Radeon RX Vega7 Win11 Garancia
- Telefon felvásárlás!! iPhone 16/iPhone 16 Plus/iPhone 16 Pro/iPhone 16 Pro Max
- Honor Pad X8 64GB, Wi-Fi, 1 Év Garanciával
- iKing.Hu -Xiaomi 14T Pro Titan Gray Használt, karcmentes állapotban 12 GB RAM / 512 GB tárhely
- AKCIÓ! Dell Latitude 5550 notebook - Intel Ultra 7 165U 16GB DDR5 RAM 1TB SSD Intel Graphics WIN11
- Telefon felvásárlás!! iPhone 15/iPhone 15 Plus/iPhone 15 Pro/iPhone 15 Pro Max
Állásajánlatok
Cég: FOTC
Város: Budapest