C/Structure/Struct Size


Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179

Deprecated: Creation of dynamic property PPDStack::$accum is deprecated in /var/www/cppe.ru/www/includes/parser/Preprocessor_DOM.php on line 803

Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179

Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/vendor/mediawiki/at-ease/src/Functions.php on line 48
Материал из C\C++ эксперт
Перейти к: навигация, поиск

Allocate memory for struct

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
 
struct address {
  char name[400];
  char street[400];
  char city[400];
  char state[30];
  char zip[100];
};
int main()
{
  struct address *p;
  if((p = malloc(sizeof(struct address)))==NULL) {
    printf("Allocation Error\n");
    exit(1);
  }
  return p;
}


Get the size of a struct

#include <stdio.h>
struct struct_type {
  int i;
  char ch;
  int *p;
  double d;
} s;
int main(void)
{
  printf("s_type is %d bytes long", sizeof(struct struct_type));
  return 0;
}