linux-Flexible arrays

Source: Internet
Author: User

Flexible arrays: In a struct defined by C99, the last element allows an array of unknown size (or 0), which does not occupy any memory of the struct, which is called a flexible array, and must have at least one other member before the flexible array.

How to use:

#include <stdio.h>#include<string.h>#include<malloc.h>typedefstruct_flex_array{intLen; Charstr; Charbuff[];} Flex_array;intMainintargcChar**argv) {        Char*info ="This is a flex array!"; intInfo_size =strlen (info); printf ("info_size:%d,struct size:%d\n", Info_size,sizeof(Flex_array)); //info_size:21,struct Size:8Flex_array*p = (Flex_array *)malloc(sizeof(Flex_array) +strlen (info)); P->len =info_size; memcpy (P->buff,info,p->Len); printf ("p:%d,info:%s\n",sizeof(*p),p->buff); //P:8,info:this is a flex array!         Free(P); return 0;}

linux-Flexible arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.