Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

The difference between FIELD-SYMBOL and normal DATA TYPE

Former Member
0 Kudos
1,476

Dear experts,

Please see the example below, both are output the same result.


DATA: EXTERNAL_RECORD(4000),
      POSITION TYPE I,
      LENGTH TYPE N,
      ENTRY TYPE STRING.
EXTERNAL_RECORD = '0005Smith0007Edwards0005Young'.
DO.
  LENGTH = EXTERNAL_RECORD+POSITION(4).
  IF LENGTH = 0.
    EXIT.
  ENDIF.
  ADD 4 TO POSITION.
  MOVE EXTERNAL_RECORD+POSITION(LENGTH) TO ENTRY.
  WRITE ENTRY.
  ADD LENGTH TO POSITION.
  IF POSITION >= 4000.
    EXIT.
  ENDIF.
ENDDO.

--


OR It can be written as--


DATA: EXTERNAL_RECORD(4000),
      POSITION TYPE I,
      LENGTH TYPE N.
FIELD-SYMBOLS <ENTRY>.
EXTERNAL_RECORD = '0005Smith0007Edwards0005Young'.
DO.
  LENGTH = EXTERNAL_RECORD+POSITION(4).
  IF LENGTH = 0.
    EXIT.
  ENDIF.
  ADD 4 TO POSITION.
  ASSIGN EXTERNAL_RECORD+POSITION(LENGTH) TO <ENTRY>.
  WRITE <ENTRY>.
  ADD LENGTH TO POSITION.
  IF POSITION >= 4000.
    EXIT.
  ENDIF.
ENDDO.

Is there any special circumstances we need to use FIELD-SYMBOL?

Why is FIELD-SYMBOL is introduce in the first place?

Kindly advice with example.

Thanks in advance for those who can help me on this.

3 REPLIES 3

jj
Active Contributor
0 Kudos
325

When you read table entries using READ or in a LOOP, you can assign them to a field symbol using the addition

... ASSIGNING <fs>

The field symbol <fs> points directly to the assigned line in memory. Unlike work areas, in which the contents of the line are only available indirectly using the INTOaddition, field symbols allow you to read and change table entries directly.

.

Advantages of Field Symbols

When you read from an internal table, there are no overheads for copying the table line to the work area. When you change an internal table with the MODIFY statement, you must first fill a work area with values, and then assign them to the internal table. If you work with field symbols instead, you do not have this overhead. This can improve performance if you have large or complex internal tables. It also makes it easier to process nested internal tables.

jj
Active Contributor
0 Kudos
325

fieldsymbol has the same concept as pointer in c,

fieldsymbol don't point to a data type like char, num instead of that it points to the memory block.

You can use field symbols to make the program more dynamic. In this example the name of a table control is substituted by a field symbol. Thus you cal call the form with any internal table, using the name of the table control as a parameter.

Example

form insert_row

using p_tc_name.

field-symbols <tc> type cxtab_control. "Table control

assign (p_tc_name) to <tc>.

  • insert 100 lines in table control

<tc>-lines = 100.

Former Member
0 Kudos
325

HI,

You can use field symbols to make the program more dynamic. In this example the name of a table control is substituted by a field symbol. Thus you cal call the form with any internal table, using the name of the table control as a parameter.

Example

form insert_row

using p_tc_name.

field-symbols <tc> type cxtab_control. "Table control

assign (p_tc_name) to <tc>.

  • insert 100 lines in table control

<tc>-lines = 100.

-


Field symbols allow you to:

** Assign an alias to a data object(for example, a shortened

name for data objects structured through several hierarchies

- <fs>-f instead of rec1-rec2-rec3-f)

** Set the offset and length for a string variably at runtime

** Set a pointer to a data object that you determine at runtime (dynamic ASSIGN)

** Adopt or change the type of a field dynamically at runtime

** Access components of a structure

** (from Release 4.5A) Point to lines of an internal table

(process internal tables without a separate work area)

Field symbols in ABAP are similar to pointers in other programming

languages. However, pointers (as used in PASCAL or C) differ from ABAP

field symbols in their reference syntax.

The statement ASSIGN f to <fs> assigns the field f to field

symbol <fs>. The field symbol <fs> then "points" to the

contents of field f at runtime. This means that all changes to the

contents of f are visible in <fs> and vice versa. You declare

the field symbol <fs> using the statement FIELD-SYMBOLS: <fs>.

Reference syntax

Programming languages such as PASCAL and C use a dereferencing symbol

to indicate the difference between a reference and the object to which

it refers; so PASCAL would use p^ for a pointer instead of p, C would

use *p instead of p. ABAP does not have any such dereferencing symbol.

** In PASCAL or C, if you assign a pointer p1 to a pointer p2,

you force p1 to point to the object to which p2 refers (reference semantics).

** In ABAP, if you assign a field symbol <fs1> to a field

symbol <fs2>, <fs1> takes the value of the data object to

which <fs2> refers (value semantics).

** Field symbols in ABAP are always dereferenced, that is,

they always access the referenced data object. If you want to

change the reference yourself in ABAP, you can use the ASSIGN statement

to assign field symbol <fs1> to field symbol <fs2>.

Using field symbols

You declare field symbols using the FIELD-SYMBOLS statement.

They may be declared either with or without a specific type.

At runtime you assign a field to the field symbol using the ASSIGN

statement. All of the operations on the field symbol act on the field

assigned to it.

When you assign a field to an untyped field symbol, the field symbol

adopts the type of the field. If, on the other hand, you want to assign

a field to a typed field symbol, the type of the field and that of the

field symbol must be compatible.

A field symbol can point to any data object and from Release 4.5A,

they can also point to lines of internal tables.

The brackets (<>) are part of the syntax.

Use the expression <fs> IS ASSIGNED to find out whether the field

symbol <fs> is assigned to a field.

The statement UNASSIGN <fs> sets the field symbol <fs> so

that it points to nothing. The logical expression <fs>

IS ASSIGNED is then false. The corresponding negative expression

is IF NOT <fs> IS ASSIGNED.

An unassigned field symbol <fs> behaves as a constant with

type C(1) and initial value SPACE.

MOVE <fs>

TO dest Transfers the initial value SPACE to the variable dest

MOVE 'A' to <fs>

Not possible, since <fs> is a constant

(runtime error).

To lift a type restriction, use the CASTING addition in the

ASSIGN statement. The data object is then interpreted as though

it had the data type of the field symbol. You can also do this

with untyped field symbols using the CASTING TYPE <type> addition.

The danger with pointers is that they may point to invalid areas.

This danger is not so acute in ABAP, because the language does not

use address arithmetic (for example, in other languages, pointer p

might point to address 1024. After the statement p = p + 10, it would

point to the address 1034). However, the danger does still exist, and

memory protection violations lead to runtime errors.

A pointer in ABAP may not point beyond a segment boundary. ABAP does

not have one large address space, but rather a set of segments.

Each of the following has its own segment:

* All global data

* All local data

* Each table work area (TABLES)

* Each COMMON PART

You should only let field symbols move within an elementary field or

structure where ABAP allows you to assign both within the global data

and beyond a field boundary.

Rgds

Umakanth