首页 > 科技 > array and string

array and string

2007年8月24日 23点17分 发表评论 阅读评论

In many cases, you need to declare a set of variables that are of the same data type. Instead of declaring each variable separately, you can declare all hte variables collectively in the format fo an array. Each variable, as an element fo the array, can be accessed either through the array reference or through a pointer that references the array.

An array consists of consecutive memory locations.
datatype Array_Name[Array-Size];
The total bytes of the array:
sizeof(data-type)* Array-size;
Or sizeof(Array_Name);

A character string is actually a character array ended with a null character .

Displaying Arrays of characters:
In C, a character string is defined as character array whose last element is the null character ‘’. For instances:
printf(“%s”,c);
1. Tell the printf() where to find the first element to start with.
2. Use the string format specifier %s in the printf() function.
Note, c contains the address of the first element in the array. That is, the start address of the array.
3. How the printf() function knows where the end of the character array is?
The answer: the last element in array is a ‘’ character. This is null character that marks the end of character array.

The Null character():
The null character ‘’ is treated as one character in C language; it is a specical character that makes the end of string. Therebefore, when function like printf() act on a character string, they process one character after another untill they encounter the null character.

The null character ‘’, which is always evaluated as FALSE, can also be used for a logical test in a control-flow statement.

A series of characters enclosed in double quotes, is called a string constant.The C compiler can automatically add a null character at the end of a string constant to indicate the end of the string.

Initialize a character array str, with string constant:
char str[7] = “hello!”;
Here sizeof(str) = 6 + 1 = 7, ‘1’ is the position of null character.
char *ptr_str = “Hello world”;

Note, don’t specify the size of a character array as too small. Otherwise, it cannot string constant plus an extra null character. eg, the following declaration is considered illegal:
char str[4] = “test”;
That many C compilers will not issue a warning or an error that could eventually arise as result could be very difficult to debug. Therebefore, it’s your responsiblity to make sure you specify enough space for a string.

char *ptr_str;
ptr_str = “hello”;

These above two statments are equal to:
char *ptr_str = “hello”;

How long is a string, use strlen().
#include
size_t strlen(const char *s);
The return value from the function is the number fo bytes. Size_t is a data type defined in the string.h headfile. The size of data type depends on the paraticular computer systems.
strlen(array_name);
strlen(ptr_str);
Note, the result returned by strlen does not include null character.

Copying strings with strcpy()
If you want to copy a string from one array to another, you can copy each item of the first array to the corresponding element in the second array ot use strcpy().
strcpy() syntax:
# include
char *strcpy(char *dest, const char *size);
Here the content of the string src is copied to the array referenced by dest. The strcpy() function returns the value of the src if it is successful. eg:
char a[] = “hello”;
char *ptr_b = “world”;
strcpy(a, ptr_b)
Now, a[] is “world’.

The gets() function can be used to read characters from the standard input stream.
gets() syntax:
# include
char *gets(char *s);
Here the characters read from standard input stream are started in the character array identified by s. The gets() function stops reading, and appends a null character to the array, when a newline or end_of_file(EOF) is encounted. The function returns s if it concludes successfully. Otherwise, a null pointer is returned.

puts(), writes characters to the standard output stream.
#include
init puts(const char *s);
s refers to the character array that contains a string. If puts() is successful, it returns 0. Otherwise, a nonzero is returned. The puts() function appends a newline character to replace the null character at the end of the character array.

Scanf syntax:
#include
int scanf(const char *format,…);
If concludes successfully, it returns the number of data items real from the stdin.If an error occurs, the scanf() function returns EOF.

Note, using %s causes the scanf to read character until a space, a newline, a tab, a form feend as encounted. Characters read by the scanf are stored into an array referenced by the corresponding argument.

A null character is automatically appended to the array after the reading.
scanf(“%d %d”);
scanf(“%s”,str);
scanf(“%d %d”,&x,&y);

getc() stops reading: new line or EOF.
scanf() stops reading: space, newline, a tab, a vertical tab or a form feed.

Here, &x,&y are memory location for the integer variables x,y.

In many cases, you need to declare a set of variables that are of the same data type. Instead of declaring each variable separately, you can declare all hte variables collectively in the format fo an array. Each variable, as an element fo the array, can be accessed either through the array reference or through a pointer that references the array.

An array consists of consecutive memory locations.
datatype Array_Name[Array-Size];
The total bytes of the array:
sizeof(data-type)* Array-size;
Or sizeof(Array_Name);

A character string is actually a character array ended with a null character .

Displaying Arrays of characters:
In C, a character string is defined as character array whose last element is the null character ‘’. For instances:
printf(“%s”,c);
1. Tell the printf() where to find the first element to start with.
2. Use the string format specifier %s in the printf() function.
Note, c contains the address of the first element in the array. That is, the start address of the array.
3. How the printf() function knows where the end of the character array is?
The answer: the last element in array is a ‘’ character. This is null character that marks the end of character array.

The Null character():
The null character ‘’ is treated as one character in C language; it is a specical character that makes the end of string. Therebefore, when function like printf() act on a character string, they process one character after another untill they encounter the null character.

The null character ‘’, which is always evaluated as FALSE, can also be used for a logical test in a control-flow statement.

A series of characters enclosed in double quotes, is called a string constant.The C compiler can automatically add a null character at the end of a string constant to indicate the end of the string.

Initialize a character array str, with string constant:
char str[7] = “hello!”;
Here sizeof(str) = 6 + 1 = 7, ‘1’ is the position of null character.
char *ptr_str = “Hello world”;

Note, don’t specify the size of a character array as too small. Otherwise, it cannot string constant plus an extra null character. eg, the following declaration is considered illegal:
char str[4] = “test”;
That many C compilers will not issue a warning or an error that could eventually arise as result could be very difficult to debug. Therebefore, it’s your responsiblity to make sure you specify enough space for a string.

char *ptr_str;
ptr_str = “hello”;

These above two statments are equal to:
char *ptr_str = “hello”;

How long is a string, use strlen().
#include
size_t strlen(const char *s);
The return value from the function is the number fo bytes. Size_t is a data type defined in the string.h headfile. The size of data type depends on the paraticular computer systems.
strlen(array_name);
strlen(ptr_str);
Note, the result returned by strlen does not include null character.

Copying strings with strcpy()
If you want to copy a string from one array to another, you can copy each item of the first array to the corresponding element in the second array ot use strcpy().
strcpy() syntax:
# include
char *strcpy(char *dest, const char *size);
Here the content of the string src is copied to the array referenced by dest. The strcpy() function returns the value of the src if it is successful. eg:
char a[] = “hello”;
char *ptr_b = “world”;
strcpy(a, ptr_b)
Now, a[] is “world’.

The gets() function can be used to read characters from the standard input stream.
gets() syntax:
# include
char *gets(char *s);
Here the characters read from standard input stream are started in the character array identified by s. The gets() function stops reading, and appends a null character to the array, when a newline or end_of_file(EOF) is encounted. The function returns s if it concludes successfully. Otherwise, a null pointer is returned.

puts(), writes characters to the standard output stream.
#include
init puts(const char *s);
s refers to the character array that contains a string. If puts() is successful, it returns 0. Otherwise, a nonzero is returned. The puts() function appends a newline character to replace the null character at the end of the character array.

Scanf syntax:
#include
int scanf(const char *format,…);
If concludes successfully, it returns the number of data items real from the stdin.If an error occurs, the scanf() function returns EOF.

Note, using %s causes the scanf to read character until a space, a newline, a tab, a form feend as encounted. Characters read by the scanf are stored into an array referenced by the corresponding argument.

A null character is automatically appended to the array after the reading.
scanf(“%d %d”);
scanf(“%s”,str);
scanf(“%d %d”,&x,&y);

getc() stops reading: new line or EOF.
scanf() stops reading: space, newline, a tab, a vertical tab or a form feed.

Here, &x,&y are memory location for the integer variables x,y.

In many cases, you need to declare a set of variables that are of the same data type. Instead of declaring each variable separately, you can declare all hte variables collectively in the format fo an array. Each variable, as an element fo the array, can be accessed either through the array reference or through a pointer that references the array.

An array consists of consecutive memory locations.
datatype Array_Name[Array-Size];
The total bytes of the array:
sizeof(data-type)* Array-size;
Or sizeof(Array_Name);

A character string is actually a character array ended with a null character .

Displaying Arrays of characters:
In C, a character string is defined as character array whose last element is the null character ‘’. For instances:
printf(“%s”,c);
1. Tell the printf() where to find the first element to start with.
2. Use the string format specifier %s in the printf() function.
Note, c contains the address of the first element in the array. That is, the start address of the array.
3. How the printf() function knows where the end of the character array is?
The answer: the last element in array is a ‘’ character. This is null character that marks the end of character array.

The Null character():
The null character ‘’ is treated as one character in C language; it is a specical character that makes the end of string. Therebefore, when function like printf() act on a character string, they process one character after another untill they encounter the null character.

The null character ‘’, which is always evaluated as FALSE, can also be used for a logical test in a control-flow statement.

A series of characters enclosed in double quotes, is called a string constant.The C compiler can automatically add a null character at the end of a string constant to indicate the end of the string.

Initialize a character array str, with string constant:
char str[7] = “hello!”;
Here sizeof(str) = 6 + 1 = 7, ‘1’ is the position of null character.
char *ptr_str = “Hello world”;

Note, don’t specify the size of a character array as too small. Otherwise, it cannot string constant plus an extra null character. eg, the following declaration is considered illegal:
char str[4] = “test”;
That many C compilers will not issue a warning or an error that could eventually arise as result could be very difficult to debug. Therebefore, it’s your responsiblity to make sure you specify enough space for a string.

char *ptr_str;
ptr_str = “hello”;

These above two statments are equal to:
char *ptr_str = “hello”;

How long is a string, use strlen().
#include
size_t strlen(const char *s);
The return value from the function is the number fo bytes. Size_t is a data type defined in the string.h headfile. The size of data type depends on the paraticular computer systems.
strlen(array_name);
strlen(ptr_str);
Note, the result returned by strlen does not include null character.

Copying strings with strcpy()
If you want to copy a string from one array to another, you can copy each item of the first array to the corresponding element in the second array ot use strcpy().
strcpy() syntax:
# include
char *strcpy(char *dest, const char *size);
Here the content of the string src is copied to the array referenced by dest. The strcpy() function returns the value of the src if it is successful. eg:
char a[] = “hello”;
char *ptr_b = “world”;
strcpy(a, ptr_b)
Now, a[] is “world’.

The gets() function can be used to read characters from the standard input stream.
gets() syntax:
# include
char *gets(char *s);
Here the characters read from standard input stream are started in the character array identified by s. The gets() function stops reading, and appends a null character to the array, when a newline or end_of_file(EOF) is encounted. The function returns s if it concludes successfully. Otherwise, a null pointer is returned.

puts(), writes characters to the standard output stream.
#include
init puts(const char *s);
s refers to the character array that contains a string. If puts() is successful, it returns 0. Otherwise, a nonzero is returned. The puts() function appends a newline character to replace the null character at the end of the character array.

Scanf syntax:
#include
int scanf(const char *format,…);
If concludes successfully, it returns the number of data items real from the stdin.If an error occurs, the scanf() function returns EOF.

Note, using %s causes the scanf to read character until a space, a newline, a tab, a form feend as encounted. Characters read by the scanf are stored into an array referenced by the corresponding argument.

A null character is automatically appended to the array after the reading.
scanf(“%d %d”);
scanf(“%s”,str);
scanf(“%d %d”,&x,&y);

getc() stops reading: new line or EOF.
scanf() stops reading: space, newline, a tab, a vertical tab or a form feed.

Here, &x,&y are memory location for the integer variables x,y.

分类: 科技 标签:
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.
您必须在 登录 后才能发布评论.