Summarized some common pointer error-prone issues (3) and pointer Common Problems

Source: Internet
Author: User

Summarized some common pointer error-prone issues (3) and pointer Common Problems

Pointer and string

  Difference between NULL and NUL: the former is used to represent a special pointer (void *) 0), while the NUL is a char (\ 0) and cannot be mixed.

Character constant: single quotation marks; string: Double quotation marks;

String declaration method: literal, character array, pointer.

String Literal pool:

      

String Initialization

Initialize the 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 method to initialize this string is to use the malloc and strcpy functions to allocate memory and copy the literal to the string.

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

Strcpy (header, "Meadia Player ");

 

Differences between sizeof and strlen:

Standard Input initialization string

Standard string operations

      

#include<stdio.h>#include<stdlib.h>#include"string.h"int main(){     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); } 

Transfer string

  

#include<stdio.h>#include<stdlib.h>#include"string.h"size_t stringLength(char* string){    size_t length = 0;    while(*(string++))    {        length++;    }    return length; } int main(){     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)); } 

Pointer for passing character constants

Passing parameters to an application

Returns a string.

When a function returns a string, it returns the address of the string. The focus is on returning valid addresses. You can return references to one of the following three objects: character volume, dynamically allocated memory, and local string variables.

Function pointer and string

Related Article

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.