struct

incomplete type 'struct' error in C

耗尽温柔 提交于 2020-01-04 06:50:59
问题 I have this issue and can't see where the error is so I'm hoping someone can help address it. The error I get from compiling the source is: client.c:15:54: error: invalid application of ‘sizeof’ to incomplete type ‘struct client’ I have the struct definition inside a header file - client.h: #ifndef PW_CLIENT #define PW_CLIENT #include <event2/listener.h> #include <event2/bufferevent.h> #include <event2/buffer.h> #include <arpa/inet.h> #include <stdlib.h> #include <stdio.h> #include <errno.h>

Do all C compilers allow functions to return structures?

≡放荡痞女 提交于 2020-01-04 06:24:22
问题 I am working on a program in C and using the SDCC compiler for a 8051 architecture device. I am trying to write a function called GetName that will read 8 characters from Flash Memory and return the character array in some form. I know that it is not possible to return an array in C so I am trying to do it using a struct like this: //********************FLASH.h file******************************* MyStruct GetName(int i); //Function prototype #define NAME_SIZE 8 typedef struct { char Name[NAME

Can a structure tag be used before its scope?

非 Y 不嫁゛ 提交于 2020-01-04 06:18:43
问题 In the following example: #include <stdio.h> #include <stdlib.h> struct B b; struct B{int a;}; struct B; int main() { b.a=3; printf("%d\n", b.a); return 0; } The file scope of structure tag B starts from its first definition/declaration struct B{int a;}; . How can struct B b; refer to B before B 's scope without error? Is struct B; a structure type declaration, or a structure type definition? Why does it not conflict with the other definition struct B{int a;}; in the same file scope? 回答1:

Warning: assignment makes pointer from integer without a cast

随声附和 提交于 2020-01-04 05:59:22
问题 I'm doing cast from a pointer then it keeps me runing this warning (assignment makes pointer from integer without a cast). here's the code: #include<stdio.h> #include<stdbool.h> typedef int TipoChave; typedef struct TipoRegistro { TipoChave Chave; /*outros componentes*/ } TipoRegistro; typedef struct TipoPagina* TipoApontador; typedef struct TipoPagina { int registros; TipoRegistro *r; TipoApontador *p; } TipoPagina; TipoApontador NovaSubArvore(int ordem){ TipoApontador A; A=malloc(sizeof

Is it possible to obtain a Swift type from a string?

冷暖自知 提交于 2020-01-04 05:51:09
问题 I wonder if it's possible to obtain a Swift type dynamically. For example, say we have the following nested structs: struct Constants { struct BlockA { static let kFirstConstantA = "firstConstantA" static let kSecondConstantA = "secondConstantA" } struct BlockB { static let kFirstConstantB = "firstConstantB" static let kSecondConstantB = "secondConstantB" } struct BlockC { static let kFirstConstantC = "firstConstantBC" static let kSecondConstantC = "secondConstantC" } } It's possible to get

Cannot access struct value in std::map [duplicate]

坚强是说给别人听的谎言 提交于 2020-01-04 05:43:07
问题 This question already has answers here : c++ “no matching function for call to” error with structure (2 answers) Closed 3 years ago . DISCLAIMER: I crafted the MCVE to demonstrate an issue I had with a much larger project that has nothing to do with the weather. I chose the MCVE's names and program goal only to demonstrate the problem in an easy-to-understand way. I know this design would suck for a real-life forecast program. I am using a std::map to store structs, using std::string as a key

Casting integer constant to a pointer type

≡放荡痞女 提交于 2020-01-04 05:03:13
问题 Is it UB to cast an arbitrary integer constant to a pointer to a object/function type (to use in unit tests for example)? struct helper; //opqaue, creation the structure is complicated struct my_struct{ struct helper *h_ptr; char another_member; }; static inline struct my_struct *create_my_struct(struct helper *h_ptr, char am){ struct my_struct *m_ptr = malloc(sizeof(*m_ptr)); m_ptr->h_ptr = h_ptr; m_ptr->another_member = am; return m_ptr; } I want to write unit test for it as follows:

Using fread to read the contents of a file into a structure

时光总嘲笑我的痴心妄想 提交于 2020-01-04 04:19:11
问题 In the "Advanced Programming in the Unix Environment" book there's a part (ch 8.14, page 251) in which the author shows us the definition of the "acct" struct (used to store accounting records info). He then shows a program in which he reads the accounting data from a file into the struct (the key part of which is): fread (&acdata, sizeof(acdata), 1, fp) The trouble I'm having is that I've heard that C compilers will sometimes rearrange the elements of a struct in memory in order to better

initializing structs using user-input information

ε祈祈猫儿з 提交于 2020-01-04 04:05:21
问题 I am trying to make a program that works with poker (texas holdem) starting hands; each hand has a value from 1 to 169, and i want to be able to input each card and whether they are suited or not, and have those values correspond to a series of structs. Here is the code so far, i cant seem to get it to work (im a beginning programmer). oh and im using visual studio 2005 by the way #include "stdafx.h" #include <iostream> int main() { using namespace std; struct FirstCard { struct SecondCard {

Why does this struct definition add extra one byte of memory usage?

岁酱吖の 提交于 2020-01-04 03:18:13
问题 #include <stdio.h> typedef struct { short x,y; char type; } Tile; int main(int argc, const char *argv[]) { printf("%d\n",sizeof(short)); printf("%d\n",sizeof(char)); printf("%d\n",sizeof(Tile)); return 0; } The output is: 2 1 6 I expected sizeof(Tile) to be 5, instead of 6. Is this a well-defined behaviour that structs add one extra byte of memory usage, or is it implementation dependant? 回答1: It's because of padding (kind of like rounding). for example: struct example1 { char a; int b; char