The pointer array of C language learning notes and its application

Source: Internet
Author: User
Tags function prototype

In C language, the most flexible but error-prone is the pointer. An array of pointers is a common application in C. An array of pointers means that all objects stored in this array are pointers. In addition to storing objects as pointers, that is, an address, other operations are exactly the same as normal arrays.

1#include <stdion.h>2 3 intMain () {4     inti;5     Char*name[] = {the BASIC","FORTRAN", "C + +","Pascal"," COBOL "};6      for(i =0; I < (sizeof(name)/sizeof(Char*)); i++)7printf"%s", Name[i]);8     return 0; 9}

  on A pointer array named name is defined in the code for the polygon. Its data type is char *, which means that all data stored in it is a char pointer, and [] After name indicates that this is an array and does not specify the size of the array. The array of pointers is initialized with the contents of {} as defined. Then, in the reference to the pointer array, each array element is a pointer to the string. The correspondence between them is as follows:

                  

The most common use of pointer arrays is in the main function to receive parameters passed by the command line in a UNIX system. Remember a freshman C language, when what pointer array, array pointers, is completely unknown. I remember that at the time, there was also the application of command-line arguments to the pointer array. However, in a Windows system, with the VC 6.0 teaching environment, no one can ask the teacher what is the command line. Because I do not know too much, I do not know where to ask. But to think about it, even if I told you what is the command line, I do not necessarily understand.

C main function prototype with command line arguments: int main (int argc, char *argv[]). The ARGC parameter records the number of arguments, and the argv parameter records all parameters, each of which is a pointer-addressed string, that is, each element in the pointer array points to the address of a parameter. So by accessing argv[1], you can get the directory name. In the application of command-line arguments, argv[0] points to the current command name, Argv[n] to the nth parameter passed in through the command line. For example: a simple implementation of the LS command in UNIX.

 

1#include"apue.h"2#include <dirent.h>3 4 intMainintargcChar*argv[])5 {6DIR *DP;7     structDirent *Dirp;8 9     if(ARGC! =2)TenErr_quit ("Usage:ls Directory_name"); One  A     if(DP = Opendir (argv[1])) ==NULL) -Err_sys ("can ' t open%s", argv[1]); -      while((DIRP = Readdir (DP))! =NULL) theprintf"%s\n", dirp->d_name); -      - Closedir (DP); -Exit0); +}

such applications, in Unix, do not know how much more. When I first learned C language, I was completely blindfolded. I do not know what is the specific use of it. After learning Unix, UNIX is the best practice for C. After all, C was invented as a tool to write Unix.

The pointer array of C language learning notes and its application

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.