C Language 12th round: Another talk about pointers

Source: Internet
Author: User

C Language 12th round: A second talk on pointers

"Learning goals"

1. Array of pointers

2. Array pointers

3. Function pointers

(1) Array of pointers

Array of pointers: first it is an array, the elements of the array are pointers, and the array takes up the number of bytes by the array itself

Decided. It is the abbreviation for "array of stored pointers"

Such as:

Defines a pointer to the array named PTR, which contains 10 data that points to type int

int *ptr[10]; Array of pointers, int* modifying the contents of an array

Program

#include <stdio.h> void print_error (int n); int main (void) {         inti;                 for (i= 0; i< 4; i++)         {                   print_error (i);         }                 Putchar (10); printf ("\ n");           return 0; } void Print_error (int i) {         //defines an array containing 4 data pointers to type int         char*error[]= {                           "Cann ' t openfile\n",                                                "read error \ n ",                                                " Write error\n ",                                                " Media failure\n "                        };                          printf ("%s", error[I]);                 

(2) Array pointers

Array pointer: First it is a pointer to an array. The 3 2-bit system will always be 4 bytes,

As to how many bytes it points to the array, do not know. It is the abbreviation for "pointers to arrays".

Format: Type (* identifier) []

int (*p) [5];

#include <stdio.h> void print (char (*PTR) [ten], int n)//array pointer as parameter {         inti;                 for (i= 0; i< N; i++)         {                   printf ("%s\n", * (ptr+ i));         }                 return;} int main (void) {         charname[3] []= {                                  "Zhangsan",                                                                            "Lisi",                                                                           "Wangwu"                                };         Print (name, 3);  Print           return 0;}

(3) Function pointers

Function pointer: is a pointer to a function.

Format: Type (* identifier) ();

Program

#include <stdio.h>//using pointer parameters//function: Two number exchange void swap (int *a, int *b); int main (void) {  int n=;  int m=;   The first pointer function, the return type void,f is the function pointer  void (*F) ();      F=swap; F points to swap. Note: The type must match    printf ("Pre-swap: n=%d m=%d\n", N, m);    if (n< m)//If n< m, swap   {        f (&n, &m);   }    printf ("After Exchange: n=%d m=%d\n", N, m);          return 0;   } void swap (int *a, int *b) {         inttemp;                 Temp=*a;         *a=*b;         *b=temp;           return;   }


Operation Result:

before Exchange : n=10 m=

after Exchange : n=20 m=


"Smile at the Fingertips" error is unavoidable, hope to get everyone's correction ^ ^

When reproduced, keep the original link http://codingit.howbbs.com and http://blog.csdn.net/mirrorsbeyourself

C Language 12th round: Another talk about pointers

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.