Pointer programming art (2)

Source: Internet
Author: User

Pointer programming art (2) pointer and string


The pointer variable size is 4 bytes. Let's look at an example:

#include<stdio.h>int main(){    double i = 10, j = 100;    double * p = &i;    double * q = &j;    printf("%p\n",p);    printf("%p\n",q);    return 0;}



It is found that the address distance between two data types is 8, which is exactly the size of double data (the address gap is the size of the Data Type pointed to by the pointer variable), but it is not the size of the pointer type variable, in fact, we can see that this address is composed of 8-bit hexadecimal data and exactly needs 4 bytes. That is, the size of the pointer variable.


Go to the topic, pointer and string:

    char * p = "Apple iPod";
P points to the address of the first character 'a', that is, the first address of the string.

A

P

P

L

E

 

I

P

O

D

\ 0

Note: There is an empty character at the end of the string as the end of the string. If it is missing, an error will occur.

Traverse the characters in the string:

# Include <stdio. h> # include <conio. h> int main () {char * P = "Apple iPod"; printf ("string:"); For (INT I = 0; * P! = '\ 0'; I ++) {printf ("% C", * P); P ++;} printf ("\ n "); printf ("% s", P); // directly output the string return 0 ;}

The following example shows a terminator missing:

#include<stdio.h>#include<stdlib.h>int main(){    char * str1 = "Apple iPod";    char str2[10] = "Apple iPod";    printf("%s",str1);    printf("%s",str2);    return 0;}
Some compilers will recognize the str2 error during compilation, but some cannot. The output result is that many characters are added to the string, which is similar to "hot ".

Common string library functions

Generally speaking of string operations, such as string length, string copy, string concatenate, and string compare ).

The following describes how to use the library function and write your own code to simulate the library function.


Calculate the length of a string: Strlen (STR)

# Include <stdio. h> # include <stdlib. h> # include <string. h> int stringlength (char * P); int main () {/* use library function */char * STR = "Apple iPod"; printf ("length: % d \ n ", strlen (STR);/* write it by yourself */INT length = 0; length = stringlength (STR); printf (" length: % d \ n ", length); Return 0;} int stringlength (char * P) {int S = 0; while (* P! = '\ 0') // until the end is found {s ++; P ++;} return s ;}

Copy string: Strcpy (str1, str2); Role: copy the characters in str2 to str1.

#include<stdio.h>#include<stdlib.h>#include<string.h>void stringcopy(char *p, char *q);int main(){    char *str1 = "apple";    char str2[10] = "";    strcpy(str2,str1);    printf("%s\n",str2);    stringcopy(str2,str1);    printf("%s\n",str2);    return 0;}void stringcopy(char *p, char *q){    while((*p=*q)!='\0')    {        p++;        q++;    }}

If
char str2[10] = "";
Changing to char * str2 will cause an error. The quote str2 does not point to any memory space. The solution is to allocate memory to it.

t = (char *)malloc(10 * sizeof(char));

String connection: Strcat (str1, str2) is used to connect the str2 string to the end of str1.

#include<stdio.h>#include<stdlib.h>#include<string.h>void stringcat(char *p, char *q);int main(){    char * str1 = "apple";    char * str2 = "orange";    char str3[50] = "i want to bay a";    strcat(str3,str1);    printf("%s\n",str3);    stringcat(str3,str2);    printf("%s\n",str3);    return 0;}void stringcat(char *p, char *q){    while(*p!='\0')    {        p++;    }    while((*p=*q)!='\0')    {        p++;        q++;    }}
Never do this:
Strcat (str1, str2); // The reason is that no memory space is allocated.

String comparison: Strcmp (str1, str2) is used to determine whether the two strings are equal.

# Include <stdio. h> # include <stdlib. h> # include <string. h> int stringcompare (char * P, char * q); int main () {char * str1 = "abczx"; char * str2 = "abczc "; int value = strcmp (str1, str2); If (value = 0) {printf ("str1 and str2 are equal \ n");} else if (value> 0) {printf ("str1 greater than str2 \ n");} else {printf ("str1 less than str2 \ n");} value = stringcompare (str1, str2 ); if (value = 0) {printf ("str1 and str2 are equal \ n");} else if (value> 0) {printf ("str1 greater than str2 \ n");} else {printf ("str1 less than str2 \ n");} return 0;} int stringcompare (char * P, char * q) {for (; * P = * q; P ++, Q ++) if (* P = '\ 0') return 0; return * P-* q ;}


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.