Struct assignment or memcpy?
Assign one struct to another in C
Byte-Reihenfolge: Big- oder Little-Endian
- Bei Big-Endian (wörtlich „Groß-Ender“)
wird das Byte mit den höchstwertigen Bits (d. h. die signifikantesten Stellen) zuerst gespeichert, das heißt an der kleinsten Speicheradresse. Allgemein bedeutet der Begriff, dass Daten mit dem größtwertigen Element zuerst genannt werden, wie etwa bei der deutschen Schreibweise der Uhrzeit: Stunde:Minute:Sekunde. - Bei Little-Endian (wörtlich „Klein-Ender“)
wird dagegen das Byte mit den niederstwertigen Bits (d. h. die am wenigsten signifikanten Stellen) an der kleinsten Speicheradresse gespeichert beziehungsweise das kleinstwertige Element zuerst genannt, wie bei der herkömmlichen deutschen Datumsschreibweise: Tag.Monat.Jahr.
|
|
#include <stdio.h>
#include <stdint.h>
typedef struct {
union {
uint16_t num;
/* little-endian */
struct {
uint8_t lo;
uint8_t hi;
};
struct {
uint16_t lsb : 1;
uint16_t lsb_1 : 1;
uint16_t lsb_2 : 1;
uint16_t lsb_3 : 1;
uint16_t raw : 8;
uint16_t msb_3 : 1;
uint16_t msb_2 : 1;
uint16_t msb_1 : 1;
uint16_t msb : 1;
};
};
} endian_t;
void
print_endian(endian_t endian)
{
printf("---------------------------\n");
printf(" 16: 0x%016lx 0x%04x\n", (uint64_t) &endian.num, endian.num);
printf("lo 8: 0x%016lx 0x%02x\n", (uint64_t) &endian.lo, endian.lo);
printf("hi 8: 0x%016lx 0x%02x\n", (uint64_t) &endian.hi, endian.hi);
printf("MSB: %u\n", endian.msb);
printf("LSB: %u\n", endian.lsb);
printf("Bin: %u%u%u%u %u%u%u%u\n", endian.msb, endian.msb_1, endian.msb_2, endian.msb_3,
endian.lsb_3, endian.lsb_2, endian.lsb_1, endian.lsb);
}
int main(int argc, char *argv[]) {
endian_t endian;
endian.num = 0x8000;
print_endian(endian);
endian.num = 0x4000;
print_endian(endian);
endian.num = 0x2000;
print_endian(endian);
endian.num = 0x1000;
print_endian(endian);
endian.num = 0x0008;
print_endian(endian);
endian.num = 0x0004;
print_endian(endian);
endian.num = 0x0002;
print_endian(endian);
endian.num = 0x0001;
print_endian(endian);
return 0;
}
$ ./endian --------------------------- 16: 0x00007fffd2223e80 0x8000 lo 8: 0x00007fffd2223e80 0x00 hi 8: 0x00007fffd2223e81 0x80 MSB: 1 LSB: 0 Bin: 1000 0000 --------------------------- 16: 0x00007fffd2223e80 0x4000 lo 8: 0x00007fffd2223e80 0x00 hi 8: 0x00007fffd2223e81 0x40 MSB: 0 LSB: 0 Bin: 0100 0000 [...]
Byte-Reihenfolge
Bitwertigkeit
UART Communication from Linux Userspace
Byteorder
Mit Bit-Feldern
struct {
#ifdef __ONE_ENDIANESS__
unsigned int b1:1;
unsigned int b2:8;
unsigned int b3:7;
unsigned int b4:8;
unsigned int b5:7;
unsigned int b6:1;
#define _STRUCT_FILLED
#endif /* __ONE_ENDIANESS__ */
#ifdef __OTHER_ENDIANESS__
unsigned int b6:1;
unsigned int b5:7;
unsigned int b4:8;
unsigned int b3:7;
unsigned int b2:8;
unsigned int b1:1;
#define _STRUCT_FILLED
#endif /* __OTHER_ENDIANESS__ */
};
#ifndef _STRUCT_FILLED
# error Endianess uncertain for struct
#else
# undef _STRUCT_FILLED
Converting Endianess on a bit field structure
bit ordering and endianess
GCC Attributes
C Function Declaration to Implementation
Search: ;
Replace with: \{\n \n\}\n
C Preprocessor
General
Pound / Hash
#define hash # #define f(x) x #define label(a) f(hash)a
C Designated Initializers
Static Struct Initialization
struct student {
int roll;
int class;
char name[50];
};
student mark = {
.name = "Mark",
.class = 10,
.roll = 1038
};
GCC: 6.26 Designated Initializers
GCC 4.4.1: 5.23 Designated Initializers
C: Structure Initialization (Advanced)
Static Array Initialization
#define ADDRESS1 0x01
#define ADDRESS2 0x02
#define ADDRESS3 0x03
#define ADDRESS4 0x04
const static uint32_t address_map[] = {
[0] = ADDRESS1,
[1] = ADDRESS2,
[2] = ADDRESS3,
[3] = ADDRESS4
};
#define REGISTER_MAP(name, reg) [name] = reg
#define NAME1 0x03
#define NAME2 0x01
#define NAME3 0x02
#define NAME4 0x04
#define ADDRESS1 0x01
#define ADDRESS2 0x02
#define ADDRESS3 0x03
#define ADDRESS4 0x04
const static uint32_t address_map[] = {
REGISTER_MAP(NAME1, ADDRESS1),
REGISTER_MAP(NAME2, ADDRESS2),
REGISTER_MAP(NAME3, ADDRESS3),
REGISTER_MAP(NAME4, ADDRESS4)
};
How to initialize an array in C
Initialization of arrays
c: initializing arrays
Fortran 95
GFortran
GFortran Getting Started
GFortran Usage
Fortran: Fortran 95: Unterprogramme
Fortran: Fortran 95: Datentypen
Fortran/Fortran examples (en)
RANDOM_NUMBER — Pseudo-random number
RAND — Real pseudo-random number
How can gfortran tell if I am compiling f90 or f95 code?
FAQ
Question
$ make [FC] src/main.f90 make: gfortran-4.7: Command not found Makefile.inc:37: recipe for target 'obj/main.o' failed make: *** [obj/main.o] Error 127
Answer
$ sudo apt-get install gfortran Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed: gfortran-5 libgfortran-5-dev Suggested packages: gfortran-multilib gfortran-doc gfortran-5-multilib gfortran-5-doc libgfortran3-dbg The following NEW packages will be installed: gfortran gfortran-5 libgfortran-5-dev [...] $ vi Makefile Change gfortran-4.7 to gfortran $ make [FC] src/main.f90 [FC] src/pi.f90 [LD] pi
Question
$ make [MKDIR] obj/hello/./ [FC] src/hello.f [...] Error: Non-numeric character in statement label at (1) src/hello.f:15:3: end 1 Error: Unclassifiable statement at (1) f951: Error: Unexpected end of file in ‘src/hello.f’ Makefile.inc:50: recipe for target 'obj/hello/hello.o' failed make: *** [obj/hello/hello.o] Error 1
Answer
Getting started with gfortran
Options controlling Fortran dialect
-ffree-form
-ffixed-form
Specify the layout used by the source file. The free form layout was introduced in Fortran 90. Fixed form was traditionally used in older Fortran programs. When neither option is specified, the source form is determined by the file extension.
hello_FFLAGS = -ffree-form
Berechnung der Zahl Pi nach der Monte-Carlo Methode
FreeBSD Upgrade Procedures
OpenSSL 1.0.1f on FreeBSD without SCTP
Question
cc -I.. -I../.. -I../modes -I../asn1 -I../evp -I../../include -fPIC -DOPENSSL_PIC -DZLIB_SHARED -DZLIB -DOPENSSL_THREADS -pthread -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -Wa,--noexecstack -DL_ENDIAN -DTERMIOS -O3 -DMD32_REG_T=int -Wall -O2 -pipe -fPIC -funroll-all-loops -march=opteron -fno-strict-aliasing -DOPENSSL_IA32_SSE2 -DOPENSSL_BN_ASM_MONT -DOPENSSL_BN_ASM_MONT5 -DOPENSSL_BN_ASM_GF2m -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM -DMD5_ASM -DAES_ASM -DVPAES_ASM -DBSAES_ASM -DWHIRLPOOL_ASM -DGHASH_ASM -c bss_dgram.c bss_dgram.c:74:26: netinet/sctp.h: No such file or directory [...] *** Error code 1 Stop in /usr/ports/security/openssl/work/openssl-1.0.1f/crypto/bio. *** Error code 1 Stop in /usr/ports/security/openssl/work/openssl-1.0.1f/crypto. *** Error code 1 Stop in /usr/ports/security/openssl/work/openssl-1.0.1f. *** Error code 1 Stop in /usr/ports/security/openssl.
Answer
# make config deselect SCTP # vi /etc/make.conf WITH_OPENSSL_PORT=yes

