Embedded Linux C language (v)--pointers and strings

Source: Internet
Author: User

Embedded Linux C Language (v)--pointers and strings First, Introduction to Strings1. String Declaration

There are three ways to declare a string: literal, character array, character pointer.

String literals are sequence of characters enclosed in double quotation marks, commonly used for initialization, in string literal pools, and character literals are characters enclosed in single quotation marks.

A string literal pool is an area of memory allocated by a program to hold the sequence of characters that make up a string. When a string literal is used more than once, the string literal pool usually holds only one copy, generally the string literal is allocated in read-only memory and is immutable, but when the compiler's options for the literal pool are closed, the string literal may generate multiple copies, each with its own address.

The string literal in the GCC compiler can be changed in order to declare a string pointer variable as a constant can be decorated with a const .

A string is a sequence of characters ending with an ASCII character NUL. Strings are usually stored in an array or in memory allocated from the heap. Not all character arrays are strings, and character arrays may not have NUL characters.

nul

1 4 sizeof ( char = 1 sizeof ' a '

A character array is an array, and the value of each element can be changed. The string pointer points to a constant string, which is stored in the program's static data area and cannot be changed once defined. This is the most important difference.

2. String Initialization

The method used to initialize the string depends on whether the variable is a bit character array or a character pointer, and the memory allocated for the string is either an array or a piece of memory that the pointer points to.

Initialize character array:

char buffer[] = " Hello World " ;// the length of the string is one, and the literal requires a number of bytes

Char buffer[12];

strcpy (buffer, "helloWorld");

Initialize the character pointer:

char *buffer = (char *) malloc (strlen ("helloWorld") + 1);

strcpy (buffer, "helloWorld");

Second, Passing Strings

Parameters are often declared as character pointers in functions.

1. passing a simple string

To pass string literals directly:

Char buffer[12];

strcpy (buffer, "helloWorld");

Passing character arrays:

char src[] = " Hello World " ;// the length of the string is one, and the literal requires a number of bytes

Char dest[12];

strcpy (dest, SRC);

To prevent the incoming string from being modified, the passed formal parameter can be declared as const.

const char src[] = " Hello World " ;// the length of the string is one, and the literal requires a number of bytes

Char dest[12];

strcpy (dest, SRC);//char *strcpy (char *dest, const char *SRC);

2. passing strings that need to be initialized

The function returns a string that requires the initialization of the function, so the buffer needs to be passed to the function.

A, the address and length of the buffer must be passed

B. The caller is responsible for releasing the buffer

C, the function usually returns a pointer to the buffer

Char Pfun (char *buffer, int size)

{

xxxxx

return buffer;

}

3. passing parameters to an application

int main (int argc, char **argv)

{}

Through the application's entry function, main can pass parameters like an application

Third, return String

When a function returns a string, the address returned is actually a string, and the returned address must be valid.

1. returns the address of the literal number

Char *returnstring (int code)

{

xxx

return " Hello World " ;

}

2. returns the address of the dynamically allocated memory

Dynamically allocates the memory of the string from the heap within the function, returning the address.

char *blanks (int num)

{

Char *buffer = (char *) malloc (num + 1);

strcpy (buffer, "helloWorld");

xxx

Retrurn buffer;

}

After the use is complete, the function caller must free memory, otherwise it will cause a memory leak.

3. returns the address of a local string

It is undesirable to return the address of a local string, where the memory of the local string is overwritten by other stack frames.

Char *blanks (void)

{

Char buffer[] = "Hello World";

return buffer;

}


This article from "Endless life, Struggle not only" blog, please be sure to keep this source http://9291927.blog.51cto.com/9281927/1790159

Embedded Linux C language (v)--pointers and strings

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.