Pointer understanding-

Source: Internet
Author: User

These two weeks are busy with the company, so there is no time to read the C language. OK. In the past few days, I discovered the pointer in the C language, in the past, I thought it was just a 4-byte book. It was used to store the address of an object. At the same time, I can use this address to add or subtract ..... however, after this process, I found that many java C languages are actually different. Let's talk about some of my practical experience on this pointer, please give me more advice from cainiao .... in fact, it is as simple as copying characters from String object A to string object B. program # include <stdio. h>
# Include <stdlib. h>
# Include <string. h> char * copy_1 (const char *, char *);
Void copy_2 (const char *, char *); int main ()
{
Char * c = "hello google !! ";
Int len = strlen (c) + 1;
Char * t = (char *) malloc (len); if (NULL) // This switch is used to test different methods
{
T = copy_1 (c, t );
Printf ("copy_1: \ tbefor: % s \ tafter copy: % s \ n", c, t );
} Else {
Copy_2 (c, t );
Printf ("copy_2: \ tbefor: % s \ tafter copy: % s \ n", c, t );
}
Free (t); return 0;
} // Method for copying strings 1char * copy_1 (const char * in, char * out)
{
If (in = NULL | out = NULL)
{
Return;
} Char * t = out; //
While (* out ++ = * in ++ )! = '\ 0 ');
* (++ Out) = '\ 0 ';
Return t;
} // Method 2 for string copying void copy_2 (const char * in, char * out)
{
If (in = NULL | out = NULL)
{
Return;
} Int len = 0;
While (* out ++ = * in ++ )! = '\ 0 ')
{
Len ++;
}
* (++ Out) = '\ 0'; for (; len> 0; len --) \ B
{
Out --;
}
} First look at method _ 1. The purpose is very simple. Copy the character Object Pointer c to the object pointer t. I think this method is more in line with the java method and pass an object in, and then return another object, but do you know where \ A is in the copy_1 method, before copying the incoming object out, define another pointer pointing to this out object. At the same time, the returned pointer is also returned, but no out object involved in the replication process is returned, in this way, it seems that you want to do more, but you will understand it later. the objective of method _ 2 is to copy strings. However, this method clearly seems to be like a C language method that transfers a character object and then performs the copy operation in the method, OK, after the copy is complete, it should be able to return directly, because the character copy has been completed, but pay attention to the \ B of method 2 and perform a addition, subtraction, the pointer of the out object is returned to the length of the character, just like an array. The cursor is returned to the first place of the array, so that you can understand why the first method defines another object, and then proceed. in summary, it means that it is not only a four-byte item that saves the address of an object, but also saves the cursor displacement pointing to the object. you can try it out. If you don't want to use the \ B loop of method 2, see what the output result is .. you can say this .. definitely wrong... at that time, when I joined \ B, I was thinking about this pointer problem. It was just a whim .... I hope you will give more comments to your younger brother .... open source China

This article from the "starting life, chasing dreams, super .." blog, please be sure to keep this source http://kernaling.blog.51cto.com/654553/218138

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.