Summarizes some common problems with pointers error-prone (iii)

Source: Internet
Author: User

Pointers and strings

  Null and NUL difference: The former is used to represent a special pointer ((void*) 0), and Nul is a char (), not mixed.

Character constants: single quotes; strings: double quotes;

How a string is declared: literal, character array, pointer.

string literal pool:

      

String initialization

Initialize char array: char header[]= "Media Player";

    

strcpy function Initialization Array

Char header[13];

strcpy (header, "Meadia Player");

2. Initialize the char pointer

char *header; a common way to initialize this string is to use the malloc and strcpy functions to allocate memory and copy the literal to a string.

Char *header= (char*) malloc (strlen ("Media Player") +1);

strcpy (header, "Meadia Player");

The difference between sizeof and strlen:

Standard input initialization string

Standard string manipulation

      

#include <stdio.h>#include<stdlib.h>#include"string.h"intMain () {Char* error="ERROR:"; Char* errormessage="Not enough memory"; Char* Buffer= (Char*)malloc(strlen (Error) +strlen (errormessage) +1);     strcpy (Buffer,error);    strcat (buffer, errormessage); printf ("%s", error); printf ("%s\n", errormessage); } 

Passing strings

  

#include <stdio.h>#include<stdlib.h>#include"string.h"size_t Stringlength (Char*string) {size_t length=0;  while(*(string++) ) {length++; }    returnlength;} intMain () {Char* error="ERROR:"; Char* errormessage="Not enough memory"; Char* Buffer= (Char*)malloc(strlen (Error) +strlen (errormessage) +1);     strcpy (Buffer,error);    strcat (buffer, errormessage); printf ("%s\n", buffer); printf ("%s\n", error); printf ("%s\n", errormessage); printf ("%d\n", buffer); printf ("%d\n", Stringlength (buffer)); } 

Passing a pointer to a character constant

Passing parameters to an application

return string

When a function returns a string, it returns the actual address of the string. The point is how to return a valid address that can return a reference to one of the following three objects: the character amount/dynamically allocated memory/local string variable.

function pointers and strings

Summarizes some common problems with pointers error-prone (iii)

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.