Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

C/C++ int[] vs int* (pointers vs. array notation). What is the difference?

I know that arrays in C are just pointers to sequentially stored data. But what differences imply the difference in notation [] and *. I mean in ALL possible usage context. For example:

char c[] = "test";

if you provide this instruction in a function body it will allocate the string on a stack while

char* c = "test";

will point to a data (readonly) segment.

Can you list all the differences between these two notations in ALL usage contexts to form a clear general view.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to the C99 standard:

An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type.

36) Array types are characterized by their element type and by the number of elements in the array. An array type is said to be derived from its element type, and if its element type is T, the array type is sometimes called array of T. The construction of an array type from an element type is called array type derivation.

A pointer type may be derived from a function type, an object type, or an incomplete type, called the referenced type. A pointer type describes an object whose value provides a reference to an entity of the referenced type. A pointer type derived from the referenced type T is sometimes referred to as a pointer to T. The construction of a pointer type from a referenced type is called pointer type derivation.

According to the standard declarations…

char s[] = "abc", t[3] = "abc";
char s[] = { 'a', 'b', 'c', '' }, t[] = { 'a', 'b', 'c' };

…are identical. The contents of the arrays are modifiable. On the other hand, the declaration…

const char *p = "abc";

…defines p with the type as pointer to constant char and initializes it to point to an object with type constant array of char (in C++) with length 4 whose elements are initialized with a character string literal. If an attempt is made to use p to modify the contents of the array, the behavior is undefined.

According to 6.3.2.1 Array subscripting dereferencing and array subscripting are identical:

The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))).

The differences of arrays vs. pointers are:

  • pointer has no information of the memory size behind it (there is no portable way to get it)
  • an array of incomplete type cannot be constructed
  • a pointer type may be derived from a an incomplete type
  • a pointer can define a recursive structure (this one is the consequence of the previous two)

These links may be useful to the subject:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...