1

I'm trying to pass only a member of a struct to a function, I need the whole array of r[].b to be passed into an array in the function so that a can use that function with other members of the struct (hope it's clear).

struct:

struct rettangolo{
    int ei,ej,b,h,a;
}r[50];

function:

int trova_max(int r[],int k){
    int max,maxi,i;
    for(i=0;i<k+1;i++){
        if(r[i]>max){
            max=r[i];
            maxi=i;
        }
    }
    return maxi;
}

call:

max_b=trova_max(r.b,k);

EDIT: I changed the function as suggested but doesn't work. I don't want to use r[i].b because that would defeat the purpose of having a function

2
  • 3
    @Rabbid76: "the formal parameter of the function is a pointer to an array of type int". No it isn't. In the context of a function parameter's definition this int *r[] is 100% equivalent to int ** r. So here r is a pointer to pointer to int. No pointer to any array here. Commented Oct 14, 2018 at 9:54
  • Invalid call : max_b=trova_max(r.b,k); r is array of object. so you have provided an index to identify which object parameter would you like to pass. where you have passed single integer variable catch by the array. that's also invalid. it should better to read memory alignment of structure object and how to access them. Commented Oct 14, 2018 at 10:04

5 Answers 5

2

I think you are better off "overloading" the function trnova_max vs creating a function that returns array of pointers to the rettangolo members and passing that array to the trova_max.

In this case you are better of passing array of rettangolo and just using b in the loop.

int trova_max(struct rettangolo r[], int k) {
    int max, maxi, i;
    for (i = 0; i < k+1; i++){
        if (r[i].b > max) {
            max = r[i].b;
            maxi = i;
        }
    }
    return maxi;
}
Sign up to request clarification or add additional context in comments.

4 Comments

i could do that but I wanted to know if there was a way to pass r[].b so that i could use the function also for r[].a and r[].h
I can update the answer to match your requirement, but I think the design you are after isn't optimal since you always end up with one extra loop.
basically what i'd like to do is use the function trova_max() with r[].b, r[].h, r[].a so that i don't have to write the same code for the three of them
I understand, but there is nothing wrong with that. As noted, just create a function that loops over your target array and store pointers to the interested member of the struct and pass that array to the function. That, or have a factory struct holding references to all interested members in separated array.
2

You are doing it wrong. You are trying to pass a pointer to struct rettangolo and therefore you have to change your function declaration as per the requirement. Your function prototype should be

int trova_max(struct rettangolo r[],int k);

2 Comments

but there's no way to pass only r[].b as an integer array?
@orion; No. There is not way to achieve this.
1

You could pass an offset to the member you are interested in to the function:

int trova_max(struct rettangolo *r, size_t size, ptrdiff_t offset)
{
    int max = *(int*)((char*)&r[0] + offset);
    size_t maxi = 0;

    for (size_t i = 0; i < size; i++) {
        int v = *(int*)((char*)&r[i] + offset);
        if (v > max) {
            max = v;
            maxi = i;
        }
    }

    return maxi;
}

call:

trova_max(r, sizeof(r) / sizeof(*r), 0)); // ei
trova_max(r, sizeof(r) / sizeof(*r), &r[0].ej - &r[0].ei)); // ej
trova_max(r, sizeof(r) / sizeof(*r), &r[0].b  - &r[0].ei)); // b
trova_max(r, sizeof(r) / sizeof(*r), &r[0].h  - &r[0].ei)); // h
trova_max(r, sizeof(r) / sizeof(*r), &r[0].a  - &r[0].ei)); // a

Comments

1

Referring this comment:

You could use preprocessor magic to "automagically" create the code as often as you like without having to write it multiple times:

#include <limits.h>

struct rettangolo{
  int ei, ej, b, h, a;
};

#define TROVA_MAX(x) int trova_max_ ## x (struct rettangolo * r, int k) \
{ \
  int max = INT_MIN, maxi, i; \
  \
  for (i = 0; i < k; ++i) \
  { \
    if (r[i].x > max) \
    { \
      max = r[i].x; \
      maxi = i; \
    } \
  } \
  \
  return maxi; \
}

#define R_MAX (50)

TROVA_MAX(ei)
TROVA_MAX(ej)
TROVA_MAX(b)
TROVA_MAX(h)
TROVA_MAX(a)

int main(void)
{
  struct rettangolo r[R_MAX];

  int ei_max = trova_max_ei(r, R_MAX);    
  int ej_max = trova_max_ej(r, R_MAX);    
  int b_max = trova_max_b(r, R_MAX);    
  int h_max = trova_max_h(r, R_MAX);    
  int a_max = trova_max_a(r, R_MAX);    
}

:-)

Comments

-1

If the struct is a local variable(variable declared inside a function), then use

int trova_max(struct rettangolo r[], int k);


and call the function using

int main() {
    struct rettangolo r[50];
    int k = 10;
    trova_max(r, k)
    return 0;
}


else

#include <stdio.h>
struct rettangolo {
  int ei, ej, b, h, a;
} r[50];
int trova_max(int k) { // No need the struct array since it is a global variable
  int max, maxi, i;
  for (i = 0; i < k + 1; i++) {
    if (r[i].a > max) {// Assumed that you are comparing variable a of struct
      max = r[i].a;
      maxi = i;
    }
  }
  return maxi;
}

int main() {
  printf("%d\n", trova_max(2));
  return 0;
}

// To reuse the same function for comparing a, b and h

#include <stdio.h>
enum COMP { A_COMPARE, B_COMPARE, H_COMPARE };
struct rettangolo {
  int ei, ej, b, h, a;
} r[50];
int compare(struct rettangolo *elem, int max, enum COMP value_to_compare) {
  switch (value_to_compare) {
  case A_COMPARE:
    return elem->a > max;
  case B_COMPARE:
    return elem->b > max;
  case H_COMPARE:
    return elem->h > max;
  default:
    printf("Default statement executed\n");
  }
  return 0;
}
int trova_max(int k, enum COMP value_to_compare) { // No need the struct array since
                                              // it is a global variable
  int max, maxi, i;
  for (i = 0; i < k + 1; i++) {
    if (compare(&r[i], max,
                value_to_compare)) { // Assumed that you are comparing variable
                                     // a of struct
      max = r[i].a;
      maxi = i;
    }
  }
  return maxi;
}

int main() {
  printf("%d\n", trova_max(2, A_COMPARE));
  printf("%d\n", trova_max(2, B_COMPARE));
  printf("%d\n", trova_max(2, H_COMPARE));
  return 0;
}

2 Comments

this would work but I'd like to know if there is a way to pass only r[].b so that I can use that same function also passing r[].h and r[].a
@orion check this modified version

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.