logo
down
shadow

Error when initializing struct members in struct definition


Error when initializing struct members in struct definition

By : Harshit Goel
Date : September 23 2020, 09:00 PM
this one helps. You're not allowed to have inline initialization of structure members in C.
If you want to initialize members then you need to do it after you create instances of the structures.
code :


Share : facebook icon twitter icon
Initializing a struct's members

Initializing a struct's members


By : Manong Drayber
Date : March 29 2020, 07:55 AM
I hope this helps .
I can't understand why root->right is not initialized to NULL.
C How can I acces struct members of a struct that is part of a struct array in another struct?

C How can I acces struct members of a struct that is part of a struct array in another struct?


By : Ghostman
Date : March 29 2020, 07:55 AM
around this issue there're issues with allocation. when you send pointer to pointer you basicly gives the address of that pointer means if you sent:
code :
  newTeam(&(temp->ranking), &n);
  void newTeam(team **obj, int *n)
  {
    //blahblah
    (*obj) = (team*) malloc(sizeof(team)* (*n));
int main(){
 sportSeason *ptr = (sportSeason*)malloc(sizeof(sportSeason));
 ptr->totalMatches = ptr->remainingMatches = ptr->remainingMatches = 0;
 ptr->calendar = (matchday*)malloc(4*sizeof(matchday));
 ptr->calendar[0].date = (char*)malloc(sizeof(char) * 4);
 strcpy_s(ptr->calendar[0].date,4,"hi");
 printf("%s", ptr->calendar[0].date);
 //...
  int main(void)
   {
      char buffer[MAX_BUFFER_SIZE];
      int teams;
      sportSeason *footbalSeason = NULL;

      //Memory allocation
      footbalSeason = (sportSeason*)malloc(sizeof(sportSeason));

      printf("Welcome to a new season: \n");
      printf("Please enter the following information before registering the teams: \n");
      printf("How many teams are participating: \n");
      fgets(buffer, MAX_BUFFER_SIZE, stdin);
      teams = atoi(buffer);

      initSeason(footbalSeason, teams);
       sportSeason *temp = obj; //bit unnessery to have temp but w/e

       temp->totalMatches = (((n + 1) * n) / 2);
       temp->matchesPlayed = 0;
       temp->remainingMatches = 0;

       printf("Register teams: \n");

       /*Passing the adres of the pointer to the first element of the rank
      array (team *)<-INCORRECT you pass the address to the pointer it's basicly nothing more - you allocate it */
      newTeam(&(temp->ranking), &n);
      printf("%d\n",temp->ranking[0].playedGames); //will print it just fine

   }

   void newTeam(team **obj, int *n)
   {
      *obj = (team*)malloc(sizeof(team)*(*n));
     obj[0]->playedGames = 0; //for eg.

   }
Initializing and using struct members inside another struct

Initializing and using struct members inside another struct


By : chickenit
Date : March 29 2020, 07:55 AM
this will help Minimum necessary changes
You can't mix a typedef with initializers — at least, not in C. You also can't have structure types where the size varies depending on the data in the structure — at least, not without using one (and only one) flexible array member (FAM), but your code attempts to use two variable arrays (so they can't be FAMs).
code :
typedef struct ytxModule{
    uint8_t nEncoders;
    uint8_t nDigital;
    uint8_t nAnalog;
    uint8_t nLedsPerControl;
} ytxModule;  // Add name for typedef

typedef struct{
    ytxModule components;
    uint8_t encPins[4][2];
    uint8_t encSwitchPins[4]; 
} ytxE41Module;

ytxE41Module module = {
    .components = {4, 0, 0, 16},
    .encPins = {
      {1, 0},  
      {4, 3},   
      {14, 15},  
      {11, 12}   
    },
    .encSwitchPins = { 2, 5, 13, 10 },
}; 
typedef struct ytxModule{
    uint8_t nEncoders;
    uint8_t nDigital;
    uint8_t nAnalog;
    uint8_t nLedsPerControl;
} ytxModule;

typedef struct
{
    ytxModule components;
    struct
    {
        uint8_t encPins[2];
        uint8_t encSwitchPins;
    } pins[]; 
} ytxE41Module;
// Data used for initializing structure
int n_pins = 4;
uint8_t encPins[][2] = {
    {  1,  0 },  
    {  4,  3 },   
    { 14, 15 },  
    { 11, 12 },   
};
uint8_t switchPins[] = { 2, 5, 13, 10 };

// Allocate and check allocation
ytxE41Module *mp = malloc(sizeof(*mp) + n_pins * sizeof(mp->pins[0]));
if (mp == NULL)
    return NULL;

// Assign values to allocated structure
mp->components = (ytxModule){ n_pins, 0, 0, 16};
for (int i = 0; i < n_pins; i++)
{
    for (int j = 0; j < 2; j++)
        mp->pins[i].encPins[j] = encPins[i][j];
    mp->pins[i].encSwitchPins = switchPins[i];
}
Initializing or assigning struct members of variable array directly, with reference to named struct members, in C?

Initializing or assigning struct members of variable array directly, with reference to named struct members, in C?


By : user3549239
Date : March 29 2020, 07:55 AM
hope this fix your issue If you were to for example create an array of int you could initialize it like this:
code :
int arr[3] = { 3, 4, 5 };
struct textureParams textures[texturesCount] = {
    {
        .textureFormat = textureFormat,
        .textureAccess = textureAccess,
        .srcrect = {.x = 0, .y = 0, .w = 50, .h = 50},
        .dstrect = {.x = 0, .y = 500, .w = 50, .h = 50},
        .r = 255,
        .g = 0,
        .b = 0,
        .a = 0,
    },
    {
        .textureFormat = textureFormat,
        .textureAccess = textureAccess,
        .srcrect = {.x = 0, .y = 0, .w = 20, .h = 20},
        .dstrect = {.x = 10, .y = 100, .w = 20, .h = 20},
        .r = 0,
        .g = 255,
        .b = 0,
        .a = 0,
    }
};
Initializing struct data members of a class

Initializing struct data members of a class


By : a58a
Date : March 29 2020, 07:55 AM
Hope that helps First, you don't need to do typedef like this in C++. Second, create a default constructor for your structure:
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