logo
down
shadow

Function to look though any struct in C


Function to look though any struct in C

By : Saikumar Reddy
Date : September 23 2020, 08:00 AM
I hope this helps you . What you are asking for is effectively impossible in C.
First of all, struct types contain no metadata about their contents. At runtime, a struct object is just a sludge of bytes. There's no way to determine at runtime the number or types of members in a struct object.
code :


Share : facebook icon twitter icon
Copying variables of one inner struct to other inner struct using a function that takes address of one inner struct as a

Copying variables of one inner struct to other inner struct using a function that takes address of one inner struct as a


By : manonmoon
Date : March 29 2020, 07:55 AM
I hope this helps you . I have a complex structure which looks like this.
code :
#include <stdio.h>
#include <stddef.h>

struct a
{
  struct b
  {
    int b_inner_int;
    char b_inner_char;
  }x;

  struct c
  {
    int c_inner_int;
    char c_inner_char;
  }y;

}z;

void copy_val(struct c *addr){
    size_t offset_c = offsetof(struct a, y);
    size_t offset_b = offsetof(struct a, x);
    struct b *bp = (struct b*)((char*)addr - offset_c + offset_b);
    bp->b_inner_int = addr->c_inner_int;
    bp->b_inner_char = addr->c_inner_char;
}

int main(void){
    z.y.c_inner_int = 1;
    z.y.c_inner_char = '1';
    copy_val(&z.y);
    printf("%d, %c\n", z.x.b_inner_int, z.x.b_inner_char);
    return 0;
}
Why passing struct to function with literal struct parameter from current package differs from the same for function fro

Why passing struct to function with literal struct parameter from current package differs from the same for function fro


By : user3486046
Date : March 29 2020, 07:55 AM
it helps some times This works perferctly: , You need to export your struct field:
code :
type Struct struct {
    Field string
}
func Fn(arg struct{Field string}) {
    fmt.Println(arg)
}
Define a struct that contains a function pointer to a function that takes the struct as an argument

Define a struct that contains a function pointer to a function that takes the struct as an argument


By : KWP
Date : March 29 2020, 07:55 AM
wish help you to fix your issue I believe this code is fine in principle. Just you are misusing the C namespaces. This is why typedefs are kind of misleading and IMO should be used very sparingly. It greatly confuses namespace understanding.
In your example you have declared a function pointer such as void (*func)(s_foo);. There is no such thing as an s_foo in your program for the function to take as an argument, only a struct s_foo these are different things in different namespaces.
code :
struct foo {
  void (*func)(struct foo);
  /* A few other fields */
};

void function_name(struct foo f){
    /* Use f.func and other fields. */
}
IAR Compiler: using struct with function pointers, map file shows all the function declared in struct irrespective of fu

IAR Compiler: using struct with function pointers, map file shows all the function declared in struct irrespective of fu


By : Brandon
Date : March 29 2020, 07:55 AM
it helps some times If you fill up the structure with function pointers, the functions have been "used" so far as the linker is concerned.
Think about it in terms of what the linker is supposed to replace R_ADC_ScanConfigure with. It can't choose to set g_adc_on_adc.scanCfg to NULL, can it? It doesn't know that the object code doesn't actually call g_adc_on_adc.scanCfg.
code :
const adc_api_t g_adc_on_adc =
{
    .open                   = R_ADC_Open,
    .scanCfg                = NULL,
    .infoGet                = R_ADC_InfoGet,
    .scanStart              = R_ADC_ScanStart,
    .scanStop               = R_ADC_ScanStop,
    .scanStatusGet          = R_ADC_CheckScanDone,
    .sampleStateCountSet    = R_ADC_SetSampleStateCount,
    .read                   = R_ADC_Read,
    .close                  = R_ADC_Close,
    .versionGet             = R_ADC_VersionGet
};
Function which takes an int pointer * as an argument and returns a struct. Storing data in struct struct* and printing r

Function which takes an int pointer * as an argument and returns a struct. Storing data in struct struct* and printing r


By : Weronika Pijagin
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Your program is having several typo's, like at several places you have:
code :
struct payload
          ^ 
if(arr[i]%2==1)
    sum = sum + arr[i];
}
if(arr[i]%2==1) {
    sum = sum + arr[i];
}
int num,
printf("OddSum : %d\n", calcVal->oddSum);
Related Posts Related Posts :
  • Avoid Overflow when Calculating π by Evaluating a Series Using 16-bit Arithmetic?
  • How to handle data or char -1 when reading from file, since EOF is also -1
  • Compare every 1kb of contents of two files instead of character by character
  • Understanding functions in stm8s disassembly?
  • Recursive and no-recursive get different answer
  • Can anyone explain the following output of the code?
  • How to resync time from NTP server in esp-idf?
  • Output for the following C code is confusing me
  • Determine if window is visible with High CPU
  • Writing a file by taking input from user only saves value for "x" only
  • How should I fill an array of ints from a file using command line args? The size of the file and the no. of elements may
  • Segmentation Fault While Reading File in C
  • Fill an array at index n with m times data without bit-fields
  • Why don't the values I type in the terminal match the values I've already stored in the array?
  • MPLABX XC16 unable to resolve built-in identifier __builtin_dmaoffset
  • How call and compile function from elf to my binary?
  • Modifying non-const char array that is referred to by const char array
  • Does anyone see any mistakes here? I am trying to get a message via a pipe, from my parent to child
  • Memory Leak - Singly linked list in C
  • Trouble with translation of typedef between C to D
  • Why does SIGINT stop sleep more than one time?
  • How to pause a timer created with CreateTimerQueueTimer
  • How can I read data from file ? I can't fix it
  • Scanf in visual studio not accepting multiple cases of characters
  • How can I determine if a char* is a windows line ending?
  • I am trying to store value in long long int type but giving wrong return
  • How to manage different kinds of data in a linked list?
  • uint8_t not rollover to 0 after reaching 255 not working properly
  • Why do these two execvp produce different results?
  • Pass uintmax_t or size_t to custom printf conversion specifier
  • Why does free() leaves stuff in memory?
  • Why pointers can't be used to index arrays?
  • memory allocation eror in C
  • C custom datatypes mapped to C datatypes grouped under a single struct
  • pipe() data is not transferred to child process
  • Getting a core dump from a simple C program
  • Fatal error on makefile, need to understand the problem
  • How can I add a delay of 90 minutes when a port has gone from 0 to 1?
  • To use strcpy or not
  • the usage of strtok() in c shows warnings and returns segmentation fault(core dumped)
  • Trouble allocating array of structs
  • Only first char of user input used in array
  • Why does "int x = 5; printf("%d %d %d", x==5, x=10, x==5);" in C print "0 10 0"?
  • How to scan specific string format in C?
  • sscanf skipping the final value when reading multiple values from a line
  • How can I access full memory space in FreeDOS with C application
  • Semantics of sem_getvalue() in POSIX
  • What does the [x,y] symbol mean in a multidimensional array access?
  • compilation error: cast from pointer to integer of different size
  • why am i getting compilation error "error: conflicting types for ‘ptr’ " for the following code? static int va
  • Why does getw return -1 when trying to read a character?
  • Why C program in whch two functions call each other recursively gives segmentation fault on linux?
  • ssize_t is undefined
  • Passing a generic argument in a C function
  • Restoring stdout after redirecting it via freopen()
  • Tuples "cannot be marshalled in a foreign call" when trying to export a Haskell function to C
  • Dereferencing double pointer to pass to recursive function
  • Why is the output different in these two scenarios
  • what will happen if we dont use free() for allocated memory
  • counting words from user input
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk