C + + Flexible array members

Source: Internet
Author: User

Flexible array member definition and declaration separation
#include <stdio.h>//只是告诉编译器,当编译到使用到这个函数的的代码时,虽然还没有找到函数定义的实体,但是也让它编译不出错误。externint fun(int a);externint x;int x;int main(){  //在这行调用fun函数,但是编译器只找到了声明,没有找到fun的定义,所以编译不出错误,编译器在后面找到了fun函数的定义实体,所以运行没有问题。  fun(10);  //在这行使用全局变量x,但是编译器只找到了声明,没有找到x的定义,所以编译不出错误,编译器在后面找到了全局变量x的定义实体,所以运行没有问题。  22;}int fun(int b){  printf("b = %d\n",b);  return22
There are pointers to strings in the struct

If there is a pointer to a string in the struct, the string will occur outside the struct and cannot be managed uniformly by the struct.

#include <stdio.h>struct Test{  int a;  long b;  char* c;};int main(){  //结构体里如果有指向字符串指针,就会发生字符串在结构体的外面,不能有结构体来统一管理。  char"asd";  struct Test t;  t.c = str;  printf("%s\n", t.c);}

Workaround:

#include <stdio.h>#include <malloc.h>#include <string.h>typedef structtest{intALongb;} Test;intMain () {Char* str ="I am out of struct Test";The //sizeof (Test) struct requires the number of bytes, strlen (str) is the number of bytes required by STR, and the last plus 1 is ' + '. In this way, it is equivalent to using only the pointer tp, you can control the structure of the members inside, you can also control the structure of the string outside the body. test* TP = (test*) malloc (sizeof(Test) + strlen (str) +1); Tp->a =Ten; Tp->b = One; strcpy ((Char*) (TP+1), str); printf"%s\ n", (Char*) (TP+1));  FREE (TP); }

The above code has a disadvantage, that is, to access which STR, you need to use the tp+1 is not easy to understand, improve the following.

#include <stdio.h>typedefstruct Test{    int a;  long b;  char pc[0];}Test;int main(){  int a;  long b;  long c;  //不管Test t放在哪行,都能正确访问t.pc  Test t;  long dd;  char"Hello c Hello c++!";  long ee;  //非常的神奇,虽然没有对t.pc赋值,但是打印出了正确的数据。  printf("%s\n",t.pc);//Hello c Hello c++!}

Why, while not assigning a value to T.PC, does it print the correct data?

The local members declared in the method are stored in the stack area, the compiler puts the array str below the test T, and the test's member PC is a 0-space array, that is, it does not occupy memory space, so the PC memory address is exactly the same as the memory address of Str. So even if you do not assign a value to T.PC, you can print out the Hello C hello c++! correctly.

Question, why no matter Test T and char str[] = "Hello c hello c++!"; The compiler can put STR below the test t where it is defined.

C + + Flexible array members

Related Article

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.