• No results found

Character Strings

In document C For WinCC (Page 168-176)

In C, a character string can be defined as a vector consisting of characters or as a pointer pointing to a character. In addition to the coded character, C adds a null character to the end of the character string. It serves as the end character of the character string. In the program code displayed below, the definition of string variables is shown using both types.

Below, the internal display of both string variables is shown. In the first case, exactly as much memory space has been reserved for the string variable as is needed for accepting the string indicated for the initialization. In the second case, as much memory space has been reserved as was specified during the definition of the vector.

S t r i n g 1 \0

S t r i n g 2 \0 ? ?

WinCC Configuration Manual 4-43 C79000-G8276-C157-01

4.4.1 Sample 1- Pointers

In this sample, the basic pointer operations are performed. The sample has been configured at the Button1 object displayed below at Event Mouse Mouse Action.

C-Action at Button1

In the first section, two variables of the int data type are defined and initialized.

Next, a pointer pointing to a variable of the int data type is defined and initialized with NULL.

Next, the address contained in the pointer is output via the printf() function. The content, to which the pointer is currently pointing, is not defined. Accessing the content of the pointer via the content operator * would cause a general access violation at this time.

Next, the address of the iValue1 variable is assigned to the pointer. Its address and content is again output via the printf() function.

Next, the address of the iValue2 variable is assigned to the pointer and the result output again. The output of this program is displayed in the next section.

Output in the Diagnostics Window

The sample described in this section generates the following output in the diagnostics window:

4-44 WinCC Configuration Manual C79000-G8276-C157-01

4.4.2 Sample 2 - Vectors

In this sample, the basic vector operations are performed. The sample has been configured at the Button2 object displayed below at Event Mouse Mouse Action.

C-Action at Button2

In the first section, a vector consisting of 5 variables of the int data type is defined. The vector is already initialized with number values while it is being defined.

Next, the iIndex counter variable of the int data type is defined.

The individual elements of the vector are output via the printf() function. The access to the individual elements is achieved in a for loop via the index operator [ ]. Dealing with loops is described in the next chapter Loops. This output is displayed in the next section.

Output in the Diagnostics Window

The sample described in this section generates the following output in the diagnostics window:

WinCC Configuration Manual 4-45 C79000-G8276-C157-01

4.4.3 Sample 3 - Pointers and Vectors

In this sample, the relationship between pointers and vectors is explained. The sample has been configured at the Button3 object displayed below at Event Mouse Mouse Action.

C-Action at Button3

In the first section, a vector consisting of 5 variables of the int data type is defined. The vector is already initialized with number values while it is being defined. In this case, the size specification can also be omitted during while defining the vector.

Next, the iIndex counter variable of the int data type is defined.

Next, a piElement pointer is defined for a variable of the int data type and initialized with NULL.

Next, the address of the first vector element is assigned to the piElement pointer. This address is output via the printf() function.

Next, the individual elements of the vector are accessed by the piElement pointer. The access is carried out in a for loop by advancing the pointer to the individual elements and the content operator *.

4-46 WinCC Configuration Manual C79000-G8276-C157-01

• Next, the individual elements of the vector are accessed again. This time, however, the name of the vector itself is used as the pointer. The output of the program is displayed in the next section.

Output in the Diagnostics Window

The sample described in this section generates the following output in the diagnostics window:

WinCC Configuration Manual 4-47 C79000-G8276-C157-01

4.4.4 Sample 4 - Strings

In this sample, working with string variables is explained. The sample has been configured at the Button4 object displayed below at Event Mouse Mouse Action.

C-Action at Button4

• In the first section, a character string (vector consisting of 13 characters) is defined. This length of the character string is one character more than the length of the assigned initialization string to make room for the closing null character.

Next, the i counter variable of the int data type is defined.

Next, the individual characters of the character string are output via the printf() function. The access to these characters is carried out in a for loop via the index operator [ ].

Next, the entire character string is output via the printf() function. The output of the program is displayed in the next section.

Output in the Diagnostics Window

The sample described in this section generates the following output in the diagnostics window:

4-48 WinCC Configuration Manual C79000-G8276-C157-01

4.4.5 Sample 5 - WinCC Text Tags

In this sample, the relation between string variables in C and WinCC text tags is explained.

The sample has been configured at the Button5 object displayed below at Event Mouse Mouse Action.

C-Action at Button5

• In the first section, a character string (pointer pointing to the first character) is defined.

This string is initialized with NULL.

Next, the content of a WinCC text tag is read in via the GetTagChar() function. The function reserves the memory space required for the character string as returns its starting address.

Next, the entire character string is output via the printf() function. In addition, the length of the character string is determined by the strlen() function and output together with the starting address of the character string. The output of the program is displayed in the next section.

Output in the Diagnostics Window

The sample described in this section generates the following output in the diagnostics window:

WinCC Configuration Manual 4-49 C79000-G8276-C157-01

4.5 Loops and Conditional Statements

In the WinCC project Project_C_Course, samples pertaining to the topic loops can be accessed by clicking on the navigation bar icon displayed below. The samples are configured in the cc_9_example_03.PDL picture.

Loops

Loops can be used to repeatedly perform a code section as long as a condition is satisfied.

In general, there are two types of loops: pre-check and post-check loops. The pre-check loops check before the loop body, if this loop is to be performed. The post-check loops check after the loop body, if this loop is to be performed. Therefore, post-check loops are performed at least once.

The following types of loops can be differentiated.

while

A sample of a while loop is displayed below. The loop is repeated as long as the condition is satisfied. In this sample, the loop is performed as long as the value of the i variable is less than 5.

do - while

A sample of a do-while loop is displayed below. The loop is performed at least once and then repeated as long as the condition is satisfied. In this sample, the loop is performed as long as the value of the i variable is less than 5.

4-50 WinCC Configuration Manual C79000-G8276-C157-01

for

A sample of a for loop is displayed below. The loop is repeated as long as the condition is satisfied. The initialization of the loop counter as well as the processing of the loop counter can be formulated within the loop.

In document C For WinCC (Page 168-176)