Initialization and assignment of character arrays in C-language struct arrays

Source: Internet
Author: User

1. First define the structure array:

typedef struct bleandtsprmtcmd{
Char terminal[3];
Char note[3];
Char rmtcmd[10];
Char cmdpropt[24];
};

Bleandtsprmtcmd is the structure name, can define other structural body variables through this structure name, struct bleandtsprmtcmd variable;

Or define structure array variables, struct bleandtsprmtcmd variable[];

2. Or define a global struct-body variable,

typedef struct {
Char terminal[3];
Char note[3];
Char rmtcmd[10];
Char cmdpropt[24];
}bleandtsprmtcmd;

Then define other structure variables according to the structure variables, bleandtsprmtcmd variable; or array variables bleandtsprmtcmd variable[];

3. Initialize the struct method:

Method One: To define the structure of the array variable when the direct initialization (assignment), because the structure of the body variable is a character array, also equivalent to a string pointer, which is the specificity of the character array.

struct Bleandtsprmtcmd myrmdcmdset[28] = {{"xx", "xx", "xx", "xx"}, {"XX", "xx", "xx", "XX"} ...}

Method Two: First define the structure array, and then assign the value.

struct Bleandtsprmtcmd myrmdcmdset[] = {NULL};

This assignment is divided into two types, which I have verified to be successful:

Assign a temporary variable first;

int index = 0;
Char node_temp[4] = "XX";
Char terminal_temp[4] = "XX";
Char rmtcmd_temp[11] = "XX";
Char cmdpropt_temp[24] = "XX";

The temporary variable is then assigned to the struct array variable, traversing:

First: strcpy, copy the temporary character array variable;

for (; index <; index + +)
{

strcpy (myrmdcmdset[index].terminal, terminal_temp);
strcpy (Myrmdcmdset[index].note, node_temp);
strcpy (Myrmdcmdset[index].rmtcmd, rmtcmd_temp);
strcpy (myrmdcmdset[index].cmdpropt, cmdpropt_temp);

}

Second: memcpy, similar to the first one, except that the function is not the same;

for (; index <; index + +)
{

memcpy (myrmdcmdset[index].terminal, terminal_temp, sizeof (terminal_temp));
memcpy (Myrmdcmdset[index].note, node_temp, sizeof (node_temp));
memcpy (Myrmdcmdset[index].rmtcmd, rmtcmd_temp, sizeof (rmtcmd_temp));
memcpy (myrmdcmdset[index].cmdpropt, cmdpropt_temp, sizeof (cmdpropt_temp));

}

This completes the assignment of the struct array variable.

Note that since the variables in the structure are all character arrays, they all point to the corresponding address of the struct, and the output will output a value that begins at the beginning of the address until the struct is finished.

To output the correct values, printf must intercept and align.

Output AS-isstring:
printf("%s", str);

2. OutputSpecifylengthOfstring, long without truncation, right justified when insufficient:
printf("%ns", str); --n forSpecifylengthThe 10 binary values

3. OutputSpecifylengthOfstring, long without truncation, left justified when insufficient:
printf("%-ns", str); --n forSpecifylengthThe 10 binary values

4. OutputSpecifylengthOfstring, long truncation, and right alignment when insufficient:
printf("%n.ms", str); --n for the finalstringOutputlength
--m for the From parameterstringSubstring taken in thelength

5. OutputSpecifylengthOfstring, long truncation, left justified when insufficient:
printf("%-n.ms", str); --n for the finalstringOutputlength
--m for the From parameterstringSubstring taken in thelength

Initialization and assignment of character arrays in C-language struct 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.