Dark Horse Programmer-C language Pointer learning

Source: Internet
Author: User

The difficulty and essence of C language is the pointer, the ability to fully understand the pointer and use it skillfully is not an easy thing, there are a few points of knowledge more around.

1. The difference between an array pointer and an array of pointers.

An array pointer is a pointer to a pointer to an array, just like a int*,char* pointer, except that it is an array (the first element) with a size of 4 (under a 32-bit platform) that can be used to find the entire array.

1#include <stdio.h>2 intMainintargcChar*argv[])3 {4     inta[3][4] = {{0,1,2,4},5{5,6,7,8},6{9,Ten, One, A}};7     int(*p) [4];8p =A;9printf"%d", * * (P +1)) ;Ten     return 0; One}

where int (*p) [4] is a two-dimensional array pointer.

And the pointer array is an array, the elements of the array are pointers only, see the following code

1#include <stdio.h>2 intMainintargcChar*argv[])3 {4     inta[3][4] = {{0,1,2,4},5{5,6,7,8},6{9,Ten, One, A}};7      int*p[3];8       for(intI=0;i<3; i++){9P[i] =A[i];Ten  } Oneprintf"%d", *p[1]) ; A      return 0; -}

int *p[3] There are pointers.

2. The difference between pointer function and function pointer

A pointer function is a function whose return value is a pointer. In fact, there is no pointer function to say that only the return value is a function of a pointer or a function that is a pointer to a parameter.

A function pointer is a pointer to a specific function. It is very useful in programming, can significantly reduce the amount of code and optimize the code structure , here is a simple usage example.

1#include"stdio.h"2 3 #defineOK 14 5 intFUN1 (intnum)6 {7printf"The Fun1 value is%d\n", num);8     returnOK;9 } Ten  One intFun2 (intnum) A { -printf"The Fun2 value is%d\n", num); -     returnOK; the }  -  - intFUN3 (intnum) - { +printf"The Fun3 value is%d\n", num); -     returnOK; + }  A  at  - intMain () - {    -     inti,state; -     //Defining function Pointers -     int(*fun_p[3])(intnum);  in     //Initializing function Pointers -fun_p[0] =Fun1;  tofun_p[1] =Fun2; +fun_p[2] =Fun3; -      the      for(i=0;i<3; i++) *State =Fun_p[i] (i); $ GetChar ();Panax Notoginseng     return ; -}

Dark Horse Programmer-C language Pointer learning

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.