Brace notation

Brace notation

In several Programming languages, such as Perl, Brace Notation is a faster way to extract bytes from a string variable.

Pseudocode Example

An example of brace notation using pseudocode which would extract the 82'nd character from the string would be

a_byte=a_string{82}

The equivalent of this using a hypothetical function 'MID' would be

a_byte=MID(a_string,82,1)

Brace Notation in C

In C, strings are normally represented as a character array rather than an actual string data type. The fact a string is really an array of characters means that referring to a string would mean referring to the first element in an array. Hence in C, the following is a legitimate example of brace notation:

#include
#include
#include

int main(int argc, char* argv [] ) { char* a_string = "Test"; printf("%c",a_string [0] ); // Would print "T" printf("%c",a_string [1] ); // Would print "e" printf("%c",a_string [2] ); // Would print "s" printf("%c",a_string [3] ); // Would print "t" printf("%c",a_string [4] ); // Would print the 'null' character (ASCII 0) for end of string return(0);}

Note that each of a_string [n] would have a 'char' data type while a_string itself would return a pointer to the first element in the a_string character array.

Drawbacks

While this notation is much faster, it is also more dangerous because it does not perform any checks on string length, and therefore its return cannot be accurately predicted.

In particular, using brace notation without making sure you are within the limits of your string could have really nasty consequences for your program since if you read a character that's past the string terminator, you would be reading memory that's not allocated to that string. This memory could be another string, a pointer, or something completely different such as unallocated space. Writing to this space in memory would cause unpredictable results, and will probably end in a SIGSEGV (segmentation fault).


Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • Brace — may refer to:Medical* Brace (orthopaedic), a device used to restrict or assist body movement * Back brace, a device limiting motion of the spine ** Milwaukee brace, a kind of back brace used in the treatment of spinal curvatures * Cervical collar …   Wikipedia

  • Knuth's up-arrow notation — In mathematics, Knuth s up arrow notation is a method of notation of very large integers introduced by Donald Knuth in 1976. It is closely related to the Ackermann function. The idea is based on iterated exponentiation in much the same way that… …   Wikipedia

  • Descriptive notation — is a notation for recording chess games, and at one time was the most popular notation in English speaking and Spanish speaking countries (Brace 1977:79–80), (Sunnucks 1970:325). It was used in Europe until it was superseded by abbreviated… …   Wikipedia

  • Americanist phonetic notation — Infobox Writing system name=Americanist phonetic notation type=Alphabet languages=Reserved for phonetic transcription of any language time=1880s to the present fam1=Latin alphabet Americanist phonetic notation (variously called [North] American… …   Wikipedia

  • Common Music Notation — Example CMN rendering CMN (or Common Music Notation) is open source musical notation software. It is written in Common Lisp and runs on a variety of operating systems and Common Lisp implementations. CMN provides a package of functions to… …   Wikipedia

  • Object literal — In computer science, a literal is a notation for representing a fixed value in source code, eg string literal. In contrast to this, variables or constants are symbols that can take on one of a class of fixed values, the constant being constrained …   Wikipedia

  • Literal (computer programming) — In computer science, a literal is a notation for representing a fixed value in source code. Almost all programming languages have notations for atomic values such as integers, floating point numbers, strings, and booleans; some also have… …   Wikipedia

  • ColdFusion Markup Language — CFML redirects here. For the radio station, see CFML FM. Cold Fusion Markup Language (CFML) Paradigm(s) imperative, object oriented Appeared in 1995 Designed by Jeremy Allaire Developer Adobe System …   Wikipedia

  • Bracket — 〈 redirects here. It is not to be confused with く, a Japanese kana. This article is about bracketing punctuation marks. For other uses, see Bracket (disambiguation). Due to technical restrictions, titles like :) redirect here. For typographical… …   Wikipedia

  • List of musical symbols — Modern musical symbols are the marks and symbols that are widely used in musical scores of all styles and instruments today. This is intended to be a comprehensive guide to the various symbols encountered in modern musical notation. Contents 1… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”