Thread: How make function with ability to vary member of interest in structure?

  1. #1
    Registered User
    Join Date
    Feb 2022
    Posts
    6

    How make function with ability to vary member of interest in structure?

    Hello all,

    I've read 180567 and 178521 on the forum and I feel no wiser regarding my idea of a function implementation.

    I have a link list of data that contains a pointer to another data type. Within that data type there are numerous members. I need help understanding how far I can use "redirection"

    I want to create a function that calculates the delta array & therefore I can later run that output on a histogram function.

    See following pseudo code on method of creating delta array.

    Code:
    Delta1 = (A->next -> B -> C.member_of_interest) - (A -> B -> C.member_of_interest);
    Alternatively one could say

    Code:
    Delta1 = (A2 -> B -> C.member_of_interest) - (A -> B -> C.member_of_interest);
    Therefore my idea of function is
    Code:
    d_type *delta_array my_function(**A_linklist, ?member_of_interest?)
    So i can get
    Code:
    Delta2 = (A2 -> B -> C.member_of_interest2) - (A -> B -> C.member_of_interest2);
    by running
    Code:
    d_type *x_array my_function(**A_linklist, ?member_of_interest2?);
    I intend to use the triple ref method so I can find delta array as I transition to link list null pointer.

    Regards,
    Wes

    N.B notice the bold 2.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,430
    How many 'member_of_interest' are there?

    Anytime you start putting numeric suffixes on symbols is a sign you should be thinking about an array.
    Then your 'member_of_interestx' parameter simply becomes an index into that array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2022
    Posts
    6
    Hey Salem,

    Imagine
    Code:
    typedef struct a_struct {
      struct a_struct *next;
      b_t *b;
    } a_t;
    
    typedef struct {
      c_t *c;
      int blah
    } c_t;
    
    typedef struct {
      int t_start;
      int t_center;
      int t_end;
    } c_t;
    Therefore
    Delta1 = ((A->next) -> B -> C.t_start) - (A -> B -> C.t_start);

    Basically I want to have a function whereby
    delta_t_start = generic_function(**A, ?pointer/offset to c.t_start?)
    delta_t_end =
    generic_function(**A, ?pointer/offset to c.t_end?)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,430
    Are all elements of c_t the same type?

    If they are, you can probably cook something up with offsetof - cppreference.com
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to pass a structure member to a function?
    By krishnampkkm in forum C Programming
    Replies: 4
    Last Post: 11-26-2020, 10:12 PM
  2. Replies: 3
    Last Post: 02-03-2020, 11:36 PM
  3. Replies: 10
    Last Post: 03-18-2014, 10:43 AM
  4. how to make function return value go to structure record
    By Tom Bombadil in forum C Programming
    Replies: 1
    Last Post: 05-27-2009, 03:38 PM
  5. member of a structure that is a stucture member
    By MK27 in forum C Programming
    Replies: 2
    Last Post: 11-29-2008, 10:33 AM

Tags for this Thread