Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #define offsetof(structure, field) (size_t)(&((structure*)0)->field)
- struct person_struct
- {
- float height;
- int age;
- float weight;
- };
- typedef struct person_struct person;
- int sumar(const void* arreglo, size_t tamanio_elemento, size_t offset_campo, size_t cant_elementos);
- int main (void)
- {
- int i;
- int result;
- person ppl_list[10];
- for (i=0; i<10; i++)
- {
- ppl_list[i].height = 1.4 + i/10.;
- ppl_list[i].age = 15 + i;
- ppl_list[i].weight = 50. + 2.* i;
- }
- result = sumar(&ppl_list[0],sizeof(person), offsetof(person, age), sizeof(ppl_list)/ sizeof(ppl_list[0]));
- printf("El resultado es: %i\n", result);
- return EXIT_SUCCESS;
- }
- int sumar(const void* arreglo, size_t tamanio_elemento, size_t offset_campo, size_t cant_elementos)
- {
- int result, i, current_value;
- char * pointer_to_element;
- result = 0;
- pointer_to_element = (char*) arreglo;
- pointer_to_element += offset_campo;
- for (i=0; i<cant_elementos; ++i)
- {
- current_value = *(int*) pointer_to_element;
- result += current_value;
- pointer_to_element += tamanio_elemento;
- }
- return result;
- }
Add Comment
Please, Sign In to add comment