structure

Calloc with structure with pointers in C

邮差的信 提交于 2019-11-26 22:01:23
问题 I know that calloc request memory to be used, writes 0 on all the bits and then returns a pointer to it. My question is: if I use calloc with a structure that contains pointers, will those pointers have the NULL value or do I have to set them to point to NULL? struct a{ char * name; void * p; }* A; So basically, will name and p point to NULL after I've used calloc with struct a? Thanks! 回答1: Somehow you've gotten a lot of incorrect answers. C does not require the representation of null

Assign string to element in structure in C

偶尔善良 提交于 2019-11-26 21:58:13
问题 I have this structure: typedef struct SM_DB { LIST_TYPE link; char name[SM_NAME_SIZE]; } SM_DB_TYPE; And I would like to assign a string to its 'name'. I am doing so like this: SM_DB_TYPE one; one.name = "Alpha"; However, after compiling I get an error: "error C2106: '=' : left operand must be l-value". I am hoping this is fairly obvious. Does anyone know what I am doing wrong? Thanks 回答1: Assuming SM_NAME_SIZE is large enough you could just use strcpy like so: strcpy(one.name, "Alpha"); Just

c structure array initializing

岁酱吖の 提交于 2019-11-26 21:06:23
I have structure struct ABC { int a; int b; } and array of it as struct ABC xyz[100]; I want to initialize it a = 10 and b = 20; for all array element. Which is better way ? While there is no particularly elegant way to initialize a big array like this in C, it is possible. You do not have to do it in runtime, as some answers falsely claim. And you don't want to do it in runtime, suppose the array is const ? The way I do it is by defining a number of macros: struct ABC { int a; int b; }; #define ABC_INIT_100 ABC_INIT_50 ABC_INIT_50 #define ABC_INIT_50 ABC_INIT_10 ABC_INIT_10 ABC_INIT_10 ABC

How pointers to function as struct member useful in C?

久未见 提交于 2019-11-26 20:48:22
问题 I am not new to C programming. But I don't understand what is usefulness to keep pointer to function as a structure member in C. e.g. // Fist Way: To keep pointer to function in struct struct newtype{ int a; char c; int (*f)(struct newtype*); } var; int fun(struct newtype* v){ return v->a; } // Second way: Simple struct newtype2{ int a; char c; } var2; int fun2(struct newtype2* v){ return v->a; } int main(){ // Fist: Require two steps var.f=fun; var.f(&var); //Second : simple to call fun2(

Where can I find information on the structure of the Delphi VMT?

狂风中的少年 提交于 2019-11-26 20:15:09
问题 The System.pas file contains a fair amount of information on hard-coded VMT offsets, but it doesn't seem to actually say much about the structure of the VMT itself. What I'd really like to know is, is there any way to find out the size of a VMT at runtime, or in other words, how many virtual methods exist for a given class? 回答1: What about the VMT structure are you wanting to know? You also do know that it is an internal implementation detail that is subject to change (and has changed over

Tree implementation in Java (root, parents and children)

半城伤御伤魂 提交于 2019-11-26 17:35:29
问题 I need to create a tree structure similar as the attached image in Java. I've found some questions related to this one but I haven't found a convincing and well explained response. The application business consists in food super categories (main courses, desserts and other). Each of these categories can have parent items or children items and so on. 回答1: import java.util.ArrayList; import java.util.List; public class Node<T> { private List<Node<T>> children = new ArrayList<Node<T>>(); private

Double alignment

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 17:24:21
问题 Following the discussion from this post, I have understood that the main reason for the alignment of structure members is performance (and some architectures restrictions). If we will investigate Microsoft (Visual C++), Borland/CodeGear (C++-Builder), Digital Mars (DMC) and GNU (GCC) when compiling for 32-bit x86: The alignment for int is 4 bytes and if int is not aligned, it can happen that 2 rows of memory banks will be read. My question is why not to make double to be 4 bytes aligned also?

Padding in structures in C

孤者浪人 提交于 2019-11-26 16:04:00
This is an interview question. Till now, I used to think such questions were purely compiler dependent and shouldn't worry me, but now, I am rather curious about it. Suppose you are given two structures as: struct A { int* a; char b; } and , struct B { char a; int* b; } So which one would you prefer and why? My answer went like this (though I was somewhat shooting in the dark) that the first structure should be preferred since the compiler allocates space for a structure in some multiples of the word size (which is the size of the pointer - 4 bytes on 32 bit machines and 8 bytes on 64 bit ones

Casting a byte array to a managed structure

亡梦爱人 提交于 2019-11-26 15:55:42
问题 Update: Answers to this question helped me code the open sourced project AlicanC's Modern Warfare 2 Tool on GitHub. You can see how I am reading these packets in MW2Packets.cs and the extensions I've coded to read big endian data in Extensions.cs. I am capturing UDP packets of Call of Duty: Modern Warfare 2 using Pcap.Net in my C# application. I receive a byte[] from the library. I tried to parse it like a string, but that didn't work well. The byte[] I have has a generic packet header, then

java.util.zip - Recreating directory structure

隐身守侯 提交于 2019-11-26 15:13:37
While trying to zip an archive using the java.util.zip I ran into a lot of problems most of which I solved. Now that I finally get some output I struggle with getting the "right" output. I have an extracted ODT file (directory would be more fitting a description) to which I did some modifications. Now I want to compress that directory as to recreate the ODT file structure. Zipping the directory and renaming it to end with .odt works fine so there should be no problem. The main problem is that I lose the internal structure of the directory. Everything becomes "flat" and I do not seem to find a