Differences and relations pointer arrays and array pointers - references and pointers (two)

table of Contents

 

Pointer arrays and array pointer differences and relations

Pointer array

Array pointer


Pointer arrays and array pointer differences and relations

  • Pointer Array: an array, and the array pointer is stored in each element
  • Array pointer: a pointer and points to an array

First example:

An array of pointers 10:

int *a[10];

10 has a pointer pointing to a numeric array of integers:

int(*a)[10];

Pointer array

#include <stdio.h>
#include <iostream>

int main()
{
	int a = 1;
	int b = 2;
	int c = 3;
	int *x[3];
	x[0] = &a;
	x[1] = &b;
	x[2] = &c;
	for (int i = 0; i <3; i++)
	{
		printf("指针:%d, 值:%d\n", x[i], *x[i]);
	}
	system("pause");
	return 0;
}

Run as follows:

Array pointer

I had been thinking what an array of pointers that scenario is it? Big brother because it feels custom C ++ rules, specifically the definition of a pointer for the array, I do not know dim, after a deduction for example, to understand, it seems array pointer and array of pointers even used together really useful, for example, put some strings in an array, then each string is an array of elements, how fast can I extract a string array in any of it? And how quickly extract to any one of the characters in the string it? Which is an array pointer practical scenario:

#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
	const char *str[4] = {"Welcome","to","Fortemedia","Nanjing"}; //定义了一个指针数组,但里面没有存放指针变量,而是字符串。
	const char **p = str;//定义一个指向指针数组的数组指针
	printf("指针指向数组的首字符:%c\n",**p);
	printf("指针指向当前字符的在ascaⅡ码中的下一个:%c\n", **p + 1);
	printf("指针指向第二个字符串的首字符:%c\n", **(p + 1));
	printf("指针指向第二个字符串:%s\n", *(p+1));

	system("pause");
	return 0;
}

As very close to "** p", "** p + 1", "** (p + 1)", "* (p + 1)" represent a different meaning, a different pointers and change two pointer is switched to point to the next character string or the next one.

 

Published 271 original articles · won praise 8 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_17846375/article/details/104947829