About pointers in C

I started reading several articles on pointers to C, and I have one example that I don't understand.

An example from here: http://en.wikibooks.org/wiki/C_Programming/Pointers_and_arrays

Here he is:

Let's look at a slightly different problem. We want to have a two-dimensional array, but we do not need to have all the rows of the same length. We are declaring an array of pointers. The second line below declares A as an array of pointers. Each pointer points to a float. Here is a sample code:

float linearA[30]; float *A[6]; A[0] = linearA; /* 5 - 0 = 5 elements in row */ A[1] = linearA + 5; /* 11 - 5 = 6 elements in row */ A[2] = linearA + 11; /* 15 - 11 = 4 elements in row */ A[3] = linearA + 15; /* 21 - 15 = 6 elements */ A[4] = linearA + 21; /* 25 - 21 = 4 elements */ A[5] = linearA + 25; /* 30 - 25 = 5 elements */ A[3][2] = 3.66; /* assigns 3.66 to linearA[17]; */ A[3][-3] = 1.44; /* refers to linearA[12]; negative indices are sometimes useful. But avoid using them as much as possible. */ 

My question is: why A[0] is a pointer to only five elements, and not to ALL from linearA , since the name of the array is a pointer to its first member.

And A[1] = linearA + 5; - 6 elements per line - for the same reason? Shouldn't A[1] be a pointer to the 6th member of linearA ?

Can someone explain where my mistake is?

+6
source share
5 answers

With a few exceptions, in C, the name of the array is converted to a pointer to the first element of the array. linearA is an array of 30 of float and in expression:

 A[0] = linearA; 

it is converted to a pointer to a float .

A is an array of 6 pointers to float . Element A has a pointer to a float . Therefore, A[0] is a pointer to a float , not a pointer to an array.

And A[i][j] in C is equivalent to *(A[i] + j) , so A[i][j] is a float (dereferencing a pointer to a float gives a float ).

+3
source

A[0] is a pointer to the 1st element of linearA . Since linearA is a continuous array, this pointer actually allows you to access any of the elements of linearA 30 by adding the appropriate offset. However, in this code snippet, you emulate a 2D array, pointing to different offsets in the linearA array. The result is a 2D-like addressing of the array: A[n] brings you to the location (ie offset in linearA ) of your nth line and A[n][m] brings you to the mth element inside that line.

+1
source

This is because this line sets an array of 6 pointers to float :

 float *A[6]; 

And this line sets the first of these pointers to the first element of 30.

 A[0] = linearA; 

Therefore, each element of A points to a subsection of the original array. You must assign them, although they indicate random addresses first.

The first is the start address ( &linearA[0] ), and the next five are the following. They are available as A[0][0] to A[0][5] . Due to how arrays match pointers, you can continue to grow until you exceed the 30s.

But you can assign A[n] any part of the array that you like. While this is part of the original array, it will point to this member and the next 5 (or any number).

For example, by pointing A[1] to &linearA[6] , you are effectively setting up a two-dimensional array (it will resemble one, but it will not behave like one).

+1
source

My question is: why A [0] is a pointer to only five elements, and not to ALL of linearA, since the name of the array is a pointer to its first member.

you set A[0] to point to linearA , which is the first float in the array, A[0] is a pointer, and therefore knows nothing about the fact that it points to part of the address. Thus, A[0] not a pointer to only five elements, it indicates where the array begins, and has no idea where the array ends.

And A [1] = linearA + 5; 6 elements per row - for the same reason? Shouldn't A [1] be a pointer to the 6th term of linear A?

yes A[1] points to the sixth element, but as said before its starting address.

+1
source

The above example shows a somewhat esoteric method called Iliffe vector , which is one of the possible ways to implement jagged arrays in C. An uneven array is a matrix in which each row has a different length.

Since arrays are one-dimensional in C, you create a single linearA array containing all elements, which is interpreted as a sequence of strings, each of which has a different size. The pointer array A contains pointers to the first element of each row, which allows you to access elements using row and column indexes.

The code displays several interesting features of C pointers and arrays:

 linearA + 5 

Pointer arithmetic: adding an integer to a pointer (or array) gives a pointer indicating n elements after the original pointer.

 A[3][2] = 3.66; 

This good syntax allows you to think of this structure as a two-dimensional matrix.

In addition, and this is probably the highlight of the example, pointers and arrays are interchangeable. Here A[3] is a pointer to a float, since A is defined as an array of pointers to a float; adding [2] gives us an element of 2 places after the pointer indicated by the pointer of the original. This is similar to the arithmetic of the pointer above, only in this case the pointer is dereferenced. In fact, access to the array is defined in terms of pointers, so X[5] equivalent to *(X+5) .

 A[3][-3] 

This shows that nothing prevents you from accessing an item outside of this line. In this case, you get access to the element 3 of the place before that pointed to by A[3] . This is something that is rarely required, and it only works in this case, because you built the matrix to have adjacent elements. Typically, accessing elements outside the allocated array range will cause your program to crash.

Finally, to answer your question:

And A[1] = linearA + 5; - 6 elements per line - for the same reason? Shouldn't A[1] be a pointer to the 6th member of linearA ?

Because pointers and arrays are interchangeable, A[1] is a pointer to the sixth element in linearA and an array starting from the sixth element in linearA . There is nothing in this language, saying that the last of the 6 elements is long, you must implement this logic in your code.

+1
source

All Articles