C Language Grammar Note – Advanced usage pointer array pointer pointer to a two-dimensional array pointer structure pointer list | It house. com

Source: Internet
Author: User
Tags define null strcmp

Original: C language Grammar note – Advanced usage pointer array pointer pointer to two-dimensional array pointer structure pointer list | It house. com

C Language Grammar Note – Advanced usage pointer array pointer pointer to a two-dimensional array pointer structure pointer list | It house. com

C language Grammar Note – Advanced usage pointer array pointer pointer to two-dimensional array pointer structure pointer list
This article was posted 315 days ago by arthinking ⁄itzhai.com Original article ⁄c language ⁄ comments 3⁄ be onlookers 1,775 views+
 

Array of pointers: In an array, if its elements are all data of pointer type, then this array is called an array of pointers.

Definition: type name * array name [array length];

Char *suit[3] = {"First", "second", "Third"}; pointer to pointer:

If one variable holds the address of another pointer variable, the pointer variable is called a pointer variable that points to the pointer data, called a multilevel pointer, or a pointer to a pointer.

Definition: Type identifier * * pointer variable name;

Accessing another variable with a pointer variable is "indirect access", where the address of a target variable is stored in a pointer variable, which is "single-level addressing".

For array suit, because the array name itself represents the address, you can create a level two pointer directly:

char **p;p = suit;
#include <stdio.h>void main () {int a[5] = {1,3,5,7,9};int *num[5],i;int **p;for (i=0;i<5;i++) {num[i] = &a[i] ;} p = num;for (i=0;i<5;i++) {printf ("%d", **p);p + +;} printf ("\ n");}
Pointer to a two-dimensional array:



Address of the two-dimensional array:

A=a[0][0]=a[0] a+1=a[1] a[0]+1=a[0][1]

A is a row pointer, *a is a column pointer, **a represents the value of a[0][0], *a represents the address of a[0]. A[1]+2 equivalent to * (a+1) +2

Before the line pointer is preceded by a * to convert to a column pointer, if a and a+1 are row pointers, then *a and * (a+1) are column pointers.



Pointer variable to array element

#include <stdio.h>void main () {int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12}int *p;for (p = a[0]; p<a[0]+12; p++) {if ((p-a[0])%4 = = 0) printf ("\ n");p rintf ("%4d", *p);}}
Pointer variable to a one-dimensional array consisting of M elements

This pointer makes p+1 not point to a[0][1], but instead points to the increment of a[1],p in the length of an array, which is called a row pointer.

Data type (* pointer variable name) [N];

int a[4][3], (*P) [3];

Functions that return pointers
function type * function name ([formal parameter type declaration table]) {function Body}
Pointers to functions

A general definition of a pointer to a function:

Data type (* pointer variable name) (parameter type list)

Call Mode:

(* pointer variable name) (actual parameter list)

Int (*functionpointer) (int a); Functionpointer = func;   Func is the function name (*functionpointer) (100);
Main function with parameters
void Main (int argc, char *argv[]) {function Body}

ARGC indicates the number of command-line arguments, argv represents the parameter array

Pointers to struct bodies
struct Student *p;struct Student stu;p = &stu;//Three ways to get child elements: stu.name; (*p) .name;p->name;  Methods of pointers
Pointer to an array of struct bodies

A pointer to a struct array is actually similar to a pointer to a two-dimensional array previously defined, and can be understood as a row pointer to an array of two-bit addresses.

Dynamic memory allocation:

void *malloc (unsigned int size);

Newptr = malloc (sizeof (struct node));

void free (void *p)

Linked list structure:
#include <stdio.h> #define NULL 0#define LEN sizeof (struct student)/* Defines the length of the node */#define node struct studentstruct stu Dent{char no[5];float score;struct student *next;}; struct student *create (void); void printlist (struct student *head); Node * INSERT (node *head, node *new, int i); Node * dellist (node *head,char no[]); void main () {struct student *a;struct student test1={"ABC", 1.0,NULL};STRUCT Student * Test2;a = Create ();p rintf ("Insert new node\n"), test2 = &test1;a = insert (a,test2,2);p rintlist (a);p rintf ("delete Node\n "), a = Dellist (A," 2 ");p rintlist (a); Getch ();} /* Create a single-linked list with a head node that returns the head pointer of the single-linked list */struct student *create (void) {struct student *head = NULL, *new1, *tail;int count = 0;for (;;)  {New1 = (struct student *) malloc (LEN); /* Apply for a new node space */printf ("Input the number of student no.%d (5bytes):", Count + 1), scanf ("%5s", new1->no); if (strcmp (new1-   >no, "*") = = 0)/* Here does not need to add the address symbol, because no represents the first address of the array */{free (NEW1);  /* Release the last requested node space */break; /* End For statement */}printf ("Input The score of the student no.%d:", Count +1); scanf ("%f", &new1->score); count++;/* inserts a new node into the end of the list and sets a new tail pointer */if (count = = 1) {head = New1;  /* is the first node, the head pointer */} ELSETAIL-&GT;NEXT = New1;    /* is not the first node, insert the new node into the end of the list */tail = New1; /* Set new tail node */}/* the pointer field for the new node is empty */new1->next = Null;return (head);} /* Output list */void printlist (struct student *head) {struct Student *p;p = head;if (head = = NULL) {printf ("List is empty!!! \ n ");} else {while (p!=null) {printf ("%5s%4.1f\n", p->no,p->score);p = P->next;}}} /* Insert List node */node * INSERT (node *head, node *new, int i) {node *pointer;/* inserts a new node into the list */if (head = = NULL) {head = new; New->nex t = NULL;} else {if (i = = 0) {new, next = Head;head = new;} else {pointer = head;/* finds the first node of the single-linked list (pointer points to it) */for (;p ointer! = NULL & amp;& i > 1; pointer = pointer->next,i--); if (pointer = = NULL) printf ("Out of the Range,can ' t insert new node!\n"); else {/* General Poin ter points to the first node */new next = Pointer->next;pointer->next = new;}} return (head);}    /* Delete List */node * dellist (node *head,char no[]) {node *front; /*front means to delete the knotPoint of the previous node */node *cursor; /*cursor represents the node */if (head = NULL) that is currently being deleted {/* empty list */printf ("\nlist is empty\n"), return (head); if (strcmp (head->no,no = = 0)) {/* the node to be deleted is the header node */front = Head;head = Head->next;free (front);} else {/* Non header node */front = Head;cursor = head->next;/* moves through the loop to the location of the node to be deleted */while (cursor! = NULL && strcmp (cursor->no,no)! = 0) {front = C Ursor;cursor = cursor->next;} if (cursor! = NULL) {/* Find the node that needs to be deleted for deletion */front->next = Cursor->next;free (front);} else {printf ("%5s have not been fou nd! ", *no);}} return (head);}
In addition to the article has special instructions, are it house original articles, reproduced please link the form to indicate the source.

This article link: http://www.itzhai.com/ C-language-syntax-notes-advanced-usage-of-two-dimensional-array-of-pointers-to-a-pointer-list-pointer-array-pointer-struc ture.html keywords: c language, pointers, linked list

C Language Grammar Note – Advanced usage pointer array pointer pointer to a two-dimensional array pointer structure pointer list | It house. com

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.