Implement a growing string with one-dimensional array

xiaoxiao2021-03-06  41

It often sees a 1-dimensional array in the structure, and it is estimated that most people should see this definition. In fact, this is the use of structural implementation of a long string, which is a very interesting skill to use structure implementation. Variable length strings can be easily controlled by the structure of the string to facilitate the operation of the string data. Imagine that if you need a structure, a one is required to calculate each character, don't kill you, you say less, and it is an example, which is very simple. #pragma pach (push, 1) struct short_string {byte bystrlen; char sstring [1];

Struct struct_head {byte byrelation; bool bnotify;}; struct struct_head_save: struct_head {short_string partnername;

Struct struct_save_head {word wcount; byte bymsgtransmit;

#pragma pack (POP)

#define login_role_name_max_len 32 # Define short_string_len (n) ((n) 1) #define struct_head_save_len (n) (SIZEOF (Struct_Head) Short_String_len (n))

int main () {int nCount = 10; STRUCT_SAVE_HEAD * pSaveHead; int nSize = sizeof (STRUCT_SAVE_HEAD) (sizeof (STRUCT_HEAD_SAVE) LOGIN_ROLE_NAME_MAX_LEN) * nCount; pSaveHead = (STRUCT_SAVE_HEAD *) malloc (nSize);

Int noffset = sizeof (struct_save_head); psavehead-> wcount = 0;

Struct_head_save * prelation = (struct_head_save *) ((char *) psavehead) noffset; static char szname [] [12] = {"1", "22", "333", "4444", "5555", "666666", "7777777", "88888888", "99999999", "0000000000"}

For (int i = 0; i <10; i ) {pretent-> ByRelation = 1; pretent-> bnotify = 1; pretention-> partnername.BYSTRLEN = Strlen (szname [i]); memcpy (Predition-> Partnername. sString, szName [i], pRelation-> PartnerName.byStrLen); nOffset = STRUCT_HEAD_SAVE_LEN (pRelation-> PartnerName.byStrLen); pRelation = (STRUCT_HEAD_SAVE *) (((BYTE *) pSaveHead) nOffset); pSaveHead-> wCount }

STRUCT_SAVE_HEAD * head = (STRUCT_SAVE_HEAD *) pSaveHead; nOffset = sizeof (STRUCT_SAVE_HEAD); pRelation = (STRUCT_HEAD_SAVE *) (((char *) pSaveHead) nOffset); pRelation = (STRUCT_HEAD_SAVE *) & (pRelation-> PartnerName.sString [ pRelation-> PartnerName.byStrLen]); pRelation = (STRUCT_HEAD_SAVE *) & (pRelation-> PartnerName.sString [pRelation-> PartnerName.byStrLen]); pRelation = (STRUCT_HEAD_SAVE *) & (pRelation-> PartnerName.sString [pRelation- > Partnername.Bystrlen]);

转载请注明原文地址:https://www.9cbs.com/read-76875.html

New Post(0)