PnP
PnP

Reputation: 3185

About arrays and pointers

A brief question to do mainly with understanding how pointers work with arrays in this example:

char *lineptr[MAXLENGTH]

Now I understand this is the same as char **lineptr as an array in itself is a pointer.

My question is how it works in its different forms/ de-referenced states such as:

lineptr
*lineptr
**lineptr
*lineptr[]

In each of those states, whats happening, what does each state do/work as in code?

Any help is much appreciated!

Upvotes: 0

Views: 137

Answers (4)

John Bode
John Bode

Reputation: 123598

Except when it is the operand of the sizeof or unary & operator, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array of T" will be replaced with ("decay to") an expression of type "pointer to T", and the value of the expression will be the address of the first element.

The expression lineptr has type "MAXLENGTH-element array of char *". Under most circumstances, it will be replaced with an expression of type char **. Here's a handy table showing all the possibilities:

Expression      Type                  Decays to  Evaluates to
----------      ----                  ---------  ------------
  lineptr       char *[MAXLENGTH]     char **    address of first element in array
 &lineptr       char *(*)[MAXLENGTH]  n/a        address of array
 *lineptr       char *                n/a        value of first element
  lineptr[i]    char *                n/a        value of i'th element 
 &lineptr[i]    char **               n/a        address of i'th element 
 *lineptr[i]    char                  n/a        value of character pointed to by i'th element
**lineptr       char                  n/a        value of character pointed to by zero'th element

Note that lineptr and &lineptr evaluate to the same value (the address of an array is the same as the address of its first element), but their types are different (char ** vs. char *(*)[MAXLENGTH]).

Upvotes: 1

user406009
user406009

Reputation:

Ok, first things first.

Arrays are not pointers. They simply decompose to pointers when needed. Think of an array as a pointer that already has some data malloced/alloca'ed to it.


lineptr : This simply returns the array. Not much to say.

*lineptr : This is the same as accessing your array's first location. *lineptr = lineptr[0]. This just happens to return a char *

**lineptr: This is accessing the array's first location, an then dereferencing that location. **lineptr = *(lineptr[0]). Since your array holds char* this will return the char stored at the char * in slot 0 of the array.

*lineptr[i] : This dereferences the char* stored at i. So the char pointed to by lineptr[i] is returned.

Upvotes: 1

Dan
Dan

Reputation: 10786

lineptr is the /array/ itself.

*lineptr is the first element of the array, a char *

**lineptr is the char pointed to by the first element of the array

*lineptr[N] is the char pointed to by the Nth element of the array

Upvotes: 1

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272792

Now I understand this is the same as char **lineptr as an array in itself is a pointer.

No, an array is not the same as a pointer. See the C FAQ: http://c-faq.com/aryptr/index.html.

lineptr

This is the array itself. In most situations, it decays into a pointer to its first element (i.e. &lineptr[0]). So its type is either int *[MAXLENGTH] or int **.

*lineptr

This dereferences the pointer to the first element, so it's the value of the first element (i.e. it's the same as lineptr[0]). Its type is int *.

**lineptr

This dereferences the first elements (i.e. it's the same as *lineptr[0]). Its type is int.

*lineptr[]

I don't think this is valid syntax (in this context).

Upvotes: 5

Related Questions