The path of C language learning--on the pointer

Source: Internet
Author: User
Tags function prototype

Come in to learn C language encountered the pointer, really learned a period of time, do not have a clue, today feel some prospect, to do a record, also hope to be able to share the feeling of the people in trouble.

The learning pointers are also based on the definitions of variables, arrays, and functions.

such as: int a=10;

int a[]={1,2,3,4};

int max (int x,int y)

{

....

}

Learn the C language pointers I think the definition of pointer variables is needless to say.

Let's start with an example and then give you a start on the story.

#include <stdio.h>
int main ()
{
int A;
scanf ("%d", &a);
printf ("a=%d", a);
}

Haha, everyone is not too simple, haha, crossing also please Hugh to blame, is such a small example is learning pointers to the point of entry, scanf ("%d", &a), what is the meaning of this sentence, I think we must already know is to read from the keyboard input an integer, Then assign to a, yes, that is, but why &a, declare variable A, the system in space to open up an integer space, the specific number of bytes to be determined according to the compiler and system, &a, is the address of this address space, scanf ("%d", &a); In fact, the read-in integer is put into the address space, a is only a relatively friendly McCartney of the address space. The name of the pointer is also a friendly McCartney, except that this McCartney points to an address. Since &a represents the address of variable A, it must be assigned to the pointer variable. We have made a slight change to the example above.

#include <stdio.h>
int main ()
{
int A;
int * p=&a;
scanf ("%d", p);
printf ("a=%d", * p);
}

This code is the same as the above-mentioned example running results, p is the pointer variable, the address of A is assigned to P, that p represents the address space of a. Put the read in the whole number into p, or put into the &a, in fact, is the same place, but the name is different. Now, to think about it, the pointers are all in fact a workaround for this example.

int A; Open up is a separate address space, that array, function, string, and that opens up a series of contiguous address spaces. And those friendly names express the initial value of the address.

And then we'll go to chestnuts:

#include <stdio.h>
int main ()
{
int a[]={1,2,3,4,5};
int * P=A;
printf ("a[1]=%d\n", * (p+1));
}
We can try to see what will be the output, I can secretly tell you are ' 2 ', haha. Why is it? Above said a is actually an array of the first address, the first address assigned to the pointer variable p, then p+1 is the natural a[1] address, then why is +1? Haha, this is what you tell the compiler ah, the pointer variable declared as int, that natural 1 represents an int type number occupies an address space.

We slowly digest the practice, and then give you another chestnut, the pointer to multidimensional array, to tell the truth this really makes me Meng circle for a long time, multidimensional array, array name is also a starting address, but he is strange in his multidimensional.

Let's start with the last example.

#include <stdio.h>
int main ()
{
int a[][3]={1,2,3,4,5,6,7,8,9};
int * P=a[0];
Int (* p1) [3]=a;
printf ("a[1][1]=%d\n", * (p+4));
printf ("a[1][1]=%d\n", * (* (p1+1) +1));
}

This everyone may guess will output what ah, I again secretly told everyone output two 5, do not know you see this example is not ignorant, anyway I was ignorant, not anxious, a little bit of it out of the difference naturally also understand, the first two pointers to the assignment, in fact, here two assignment is the same, a,a[0], Represents the first address of this two-dimensional array, but these two pointers are different, so there is a certain difference in use, the pointer p is only an address, one-dimensional, the program in the storage of two-dimensional array is also in order to store, is also a one-dimensional data, so the pointer p represents the starting address of this array, then +1 Nature is the next step backwards, if I define the pointer p to int * p=a[1], the program should be what output, haha this I do not tell you, you can operate, look at, and then analyze why, here to remind you a little, a[0] represents the first address of line No. 0 , then A[1] said the nature is the first line of the first address, to this, we think about it. Then is the pointer P1 assignment, behind a [3], gee ... This is a terrific what does it want to do, haha, it does not want to do, it is that it is capable of large, want to share points, it tells the compiler that I can store three addresses, it is a two-dimensional concept, just as we define a two-dimensional array, * (p1+1) is the 1th row of the first address, and then +1 ( * (p1+1) +1), that is, in this one-dimensional array to iterate backwards, that is, the first row of the value of the first element (starting from 0). This place I feel not good understanding digestion, or do more practice slowly thinking to understand.

Give us a second chestnut:

#include <stdio.h>
int main ()
{
char * a[3]={"good study", "Day-day-up", "C Program"};
printf ("a[1]=%s\n", a[1]);
}

What does this output? This example can reflect the flexibility of the pointer array, it can store the length of the data, in fact, it seems to store the length of the data, but in fact it is stored in these short and long data of the first address, we see:

#include <stdio.h>
int main ()
{
Char str[]= "good good study";
printf ("str=%s\n", str);
}

Well, you know, this one knows what the pointer array is,
char * a[3]={"good study", "Day-day-up", "C Program"}, actually str1[]= "good good study", str2[]= "day-day-up", str3[]= "C Program", CH AR * A[3]={STR1,STR2,STR3}, a collection of these operations. The array of pointers is not as complicated as it looks.


Another example, haha, this example is very, I think this example is the most embodiment of the pointer type flexibility of the place.

#include <stdio.h>
int max (int x,int y)
{if (x>y) return x;
else return y;
}

int min (int x,int y)
{
if (x>y) return y;
else return x;
}

int fun (int x,int y,int (* p) (int a,int b))
{
int result;
result= (* p) (x, y);
printf ("result=%d\n", result);
}

int main ()
{
int max (int x,int y), int min (int x,int y), int fun (int x,int y,int (* p) (int a,int b));
int x,y,i;
scanf ("%d%d", &x,&y);
printf ("Choice The fun:\n1:max\n2:min\n");
scanf ("%d", &i);
if (i==1) fun (X,y,max);
else Fun (x,y,min);
return 0;
}

This example is long but very simple, we must be patient to understand, I would like to say that the function in the program is also a continuous address space, so you can also be represented by a pointer variable.



(Stdlib.h void * malloc (unsigned int size); void * CALLOC (unsigned n,unsigned size); void free (void * p);

Main function prototype int main (int argc,char * argv[]) This is what I used to do for the record, you don't have to pay attention.


In the next Beginner C language to a lot of things understand not deep, write blog one is their own record, another is also hope to help the study of the people in difficulty. We find that mistakes and errors can be directly practiced with QQ (2479630319).

This article is from the "I Want to learn" blog, please be sure to keep this source http://thxiaofan.blog.51cto.com/9469295/1722616

The path of C language learning--on the pointer

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.