C Language Basic Learning 09_ on the review of compound types

Source: Internet
Author: User
Tags shallow copy

=============================================================================
struct A
{
Char array[100]; Array doesn't know where it is? Just tell the C language compiler that there is a type structure. is a form of grammar, do not misunderstand.
int A;
};

struct B
{
Char *p = malloc (100); So you can't write like this!
NT A;
};

int main ()
{
struct a A; The array at this time is inside the stack.
A.A;

struct a *p = malloc (sizeof (struct a)); The array at this time is inside the heap. There is only one heap at this time.
p->a; You can use arrows----when there is only one heap.

return 0;
}
--------------------------------------
struct C//occupies 1 bytes, at least 1 bytes.
{
Char a:2;
Char B:4;
};

struct D
{
Char a:10; Bad syntax, char cannot exceed 8 bit.
Char B:4;
};

struct F//occupies 8 bytes.
{
Char a:2;
int b:4;
};
-----------------------------------------------------------------------------
struct ABC
{
int A;
int b;
};

struct ABC ABC1 = {1, 2};
struct ABC ABC2 = ABC1; A shallow copy is just a simple memory copy.
--------------------------------------
Char array[100] = "Hello";
Char array2[100];
Array2 = array; You cannot assign this value because the array name is a constant and cannot be left-valued.
--------------------------------------
struct STR
{
Char array[100];
};

struct STR str1 = {"Hello"};
struct STR str2;
STR2 = str1; At this point, the value of the array inside the STR2 is the same as the array value inside the str1. Because STR2 and str1 are a variable name, they can be used as lvalue values.
-----------------------------------------------------------------------------
the biggest disadvantage of arrays is that if you insert new data between the two members of an array, the cost is great. Because its memory is contiguous.
Instead of using the pointer variable in the heap, just add it and let the pointer do a point again. Benefits: not only can you make a structure with a special number of members, but also be free to delete and add elements.
As shown in the following:


-----------------------------------------------------------------------------

common functions of a consortium: make a mutable type. Because he can put many types, at the same time, there is only one type.
Similar to: There are many variables inJavaScript that do not have data types. C language can be implemented through a consortium.
(No data type is equivalent to a lot of data types)
Linux Example code:

1#include <stdio.h>2 3 Union A4 {5     intA;6     //double b;7     Charb;8 };9 Ten intMain () One { A union A; -A.A =0; -A.B = -; theprintf"%d\n", A.A);// - -  -     return 0; -}

-----------------------------------------------------------------------------

Note: Do not use a large structure as a function parameter because there must be a process for assigning a struct variable in the stack. is to assign the value of the argument to the parameter, which is actually a memory copy, at a very high cost.

-----------------------------------------------------------------------------

#define PAI 3.14
Double A = PAI;
--------------------------------------
When a constant is a finite range, enumeration of enums is generally used.
--------------------------------------
typedef struct STR S; So that s is similar to int, it becomes a specific data type.
S S1;
sizeof (S);
S *p = malloc (sizeof (S));
=============================================================================

C Language Basic Learning 09_ on the review of compound types

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.