How to dynamically allocate a 2d array and why? - c

For my assignment one of the things I have to do in my program is dynamically allocate a 2D array.
I can't figure out how to do it or why.
This is what I have now.
size = atoi(argv[1]);
int Pond[size][size];
int i, j;
for(i = 0; i < size; i ++){
for(j = 0; j < size; j++){
Pond[i][j]=0;
}
}
I found a answer to do it this way but I can't figure out how to access each column or row.
int **Pond;
Pond = (int**)malloc(size * sizeof(int*));
for (i = 0; i < size; i++) {
Pond[i] = (int *) malloc(size * sizeof(int));

Correct use of C's powerful array type syntax looks like this:
int (*Pond)[size] = malloc(size * sizeof(*Pond));
That's it for the allocation. After that, the elements in Pond can simply be accessed like this:
for(int y = 0; y < size; y++) {
for(int x = 0; x < size; x++) {
Pond[y][x] = 0;
}
}
And of course the deallocation:
free(Pond);
It doesn't get simpler.

The [] operator works on pointers pretty much the same way it works on array expressions, so you'd use Pond[i][j] to access a particular element regardless of how you allocate (unless you allocated it as a 1D array as dom0 shows).
If the number of rows and columns are known at compile time, this is the easiest:
#define M ... // rows
#define N ... // columns
int (*Pond)[N] = malloc ( sizeof *Pond * M);
...
Pond[i][j] = x;
...
free( Pond );
The type of the variable Pond is "pointer to N-element array of int". The type of the expression *Pond is "N-element array of int". So sizeof *Pond gives us the number of bytes in N-element array of int; we multiply this by M to get the total number of bytes required for the array.
Since a[i] is evaluated as *(a + i), the [] operator works the same way on a pointer expression as it does on an array expression1; that is, you could use Pond[i][j] here as you would for a regular 2D array.
If the number of rows and columns are not known until run time, but you're using a C99 compiler or a C2011 compiler that supports variable length arrays, it's pretty much the same:
size_t n, m;
/**
* get values of n and m
*/
int (*Pond)[n] = malloc( sizeof *Pond * m );
...
Pond[i][j] = x;
...
free( Pond );
Same deal as above, it's just that m and n aren't known until runtime. In both of these cases, the dynamically-allocated arrays are contiguous (all rows are adjacent in memory).
+---+
Pond: | | ---+
+---+ |
... |
+---+ |
Pond[0][0]: | | <--+
+---+
Pond[0][1]: | |
+---+
...
+---+
Pond[0][n-1]: | |
+---+
Pond[1][0]: | |
+---+
Pond[1][1]: | |
+---+
...
+---+
Pond[m-1][0]: | |
+---+
Pond[m-1][1]: | |
+---+
...
+---+
Pond[m-1][n-1]: | |
+---+
If the number of rows and columns aren't known until runtime, and you're using a compiler that does not support VLAs, you have two choices depending on whether you want the arrays to be contiguous or not.
If the array doesn't have to be contiguous, you can use a two-step allocation approach:
size_t n, m;
/**
* get values for n and m
*/
int **Pond = malloc( sizeof *Pond * m ); // allocate m rows of pointer to int
if ( Pond )
{
for ( size_t i = 0; i < m; i++ )
{
Pond[i] = malloc( sizeof *Pond[i] * n ); // allocate n columns of int for each row
}
}
In this case, Pond is a pointer to pointer to int; it will wind up pointing to what is effectively a 1D array of pointers to int; each of those pointers will point to a 1D array of int, sort of like the following:
+---+
Pond: | | ---+
+---+ |
|
+------+
|
V
+---+
Pond[0]: | | ------------+
+---+ |
Pond[1]: | | ---------+ |
+---+ | |
Pond[2]: | | | |
+---+ | |
... | |
+---+ | |
Pond[m-1]: | | | |
+---+ | |
| |
+---+ | |
Pond[0][0]: | | <--------|--+
+---+ |
Pond[0][1]: | | |
+---+ |
... |
+---+ |
Pond[0][n-1]: | | |
+---+ |
|
+---+ |
Pond[1][0]: | | <--------+
+---+
Pond[1][1]: | |
+---+
...
+---+
Pond[1][n-1]: | |
+---+
But because Pond[i][j] is evaluated as *(*(Pond + i) + j), it all still works.
Note that since this was a two-step allocation process, deallocation also requires two steps:
for ( size_t i = 0; i < m; i++ )
free( Pond[i] );
free( Pond );
If the array does need to be contiguous, then you'll have to allocate the memory as a 1D array and compute your indexes manually:
int *Pond = malloc( sizeof *Pond * m * n );
...
Pond[ i * m + j ] = x;
...
free( Pond );
1. Under most circumstances, an array expression will be converted or "decay" to a pointer expression, so the subscript is actually working on a pointer expression either way.

Related

Trying to convert every string to an integer and storing each element into int**

I have an object of type int** which I want to fill with integers that were originally strings, in this case fill it with the integer 1234 that was originally a string "1234".
int** num_list = malloc(10*sizeof(int*));
for(int i=0; i<10; i++) {
num_list[i] = atoi("1234");
}
free(num_list);
The error / warning I get is assignment makes pointer to integer without a cast [-Wint -conversion]. I'm still new to C language and after failing with various attempts of making this work correctly I came to ask here, why is this happening and how can this work without issues? I cannot change the type of the list, it needs to stay as int** because all my work depends on this type and I cannot change it to something else.
You could do this:
int** num_list_1 = malloc(10*sizeof(int*));
for(int i=0; i<10; i++) {
num_list_1[i] = malloc(sizeof int);
num_list_1[i][0] = atoi("1234");
}
for(int i=0; i<10; i++) {
free(num_list_1[i]);
}
free(num_list_1);
Or you could do this:
int** num_list_2 = malloc(10*sizeof(int*));
num_list_2[0] = malloc(10*sizeof(int));
for(int i=1; i<10; i++) {
num_list_2[i] = NULL;
}
for(int i=0; i<10; i++) {
num_list_2[0][i] = atoi("1234");
}
Notice that in the first example the key assignment is
num_list_1[i][0] = atoi("1234");
while in the second example it is
num_list_2[0][i] = atoi("1234");
Another way to write the first (not the second) example would be
*num_list_1[i] = atoi("1234");
The first example looks like this in memory:
+---+
num_list_1: | * |
+-|-+
|
v
+---+ +------+
| *----> | 1234 |
+---+ +------+ +------+
| *-----------------> | 1234 |
+---+ +------+ +------+
| *----> | 1234 |
+---+ +------+
.
.
.
+---+ +------+
| *----> | 1234 |
+---+ +------+
The second example looks like this:
+---+
num_list_2: | * |
+-|-+
|
v
+---+ +------+------+------+ +------+
| *----> | 1234 | 1234 | 1234 | ... | 1234 |
+---+ +------+------+------+ +------+
| 0 |
+---+
| 0 |
+---+
.
.
.
+---+
| 0 |
+---+
Notice that in each case, num_list_1 and num_list_2 point to an array of pointers, and each of those second-level pointers actually points to an int (or to several ints in the case of num_list_2).
If you could use int * (as in SlLoWre's answer) it would look like this:
+---+ +------+------+------+ +------+
num_list: | *----> | 1234 | 1234 | 1234 | ... | 1234 |
+---+ +------+------+------+ +------+
But, again, if you want this picture, num_list has to be an int *, not an int **.
The requirement to use int ** seems strange. The only reason I can imagine that you would use an int ** would be if you are using a function to construct the array, and the function is returning the pointer by reference. That would look like this:
void allocate_array(int **num_list_p)
{
*num_list_p = malloc(10*sizeof(int*));
for(int i=0; i<10; i++) {
(*num_list_p)[i] = atoi("1234");
}
}
int main()
{
int *num_list;
allocate_array(&num_list);
for(int i = 0; i < 10; i++) {
printf("%d: %d\n", i, num_list[i]);
}
}
Also, one last thing: You probably knew this, but in real code, we would always check the return value of malloc to make sure it wasn't NULL.
You dont need to have int** (pointer to a pointer) use just int*
int* num_list = malloc(10*sizeof(int));
for(int i=0; i<10; i++) {
num_list[i] = atoi("1234");
}
free(num_list);

How one malloc call for all rows work for 2D array?

I know that we can achieve dynamic multi-dimensional arrays using pointers and there are many ways to do it, with single pointers and double pointers as well. But while exploring on this topic, came across this piece of code in which I am not able to understand the head and tail. Can anyone please explain me how the below piece of code works?
Also please explain,
1) Why it is necessary to allocate r*sizeof(int*) to arr when we are anyways going to allocate memory to arr[i] as r*c*sizeof(int).
2) Why it is necessary to do arr[i] = *arr+c*i.
Since I am very new to this dynamic memory allocation and so much eager to dig little deeper, arised these questions. Sorry if it is basic but still I have no idea about it.
Thanks,
#include<stdio.h>
#include<stdlib.h>
int main()
{
int r=3, c=4;
int **arr;
int count = 0,i,j;
arr = (int **)malloc(sizeof(int *) * r);
arr[0] = (int *)malloc(sizeof(int) * c * r);
for(i = 0; i < r; i++)
arr[i] = (*arr + c * i);
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
arr[i][j] = ++count; // OR *(*(arr+i)+j) = ++count
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
{
printf("%d, %p, %p\n", arr[i][j], &arr[i][j], arr[i]);
}
return 0;
}
Output:
1, 21100, 21100
2, 21104, 21100
3, 21108, 21100
4, 2110c, 21100
5, 21110, 21110
6, 21114, 21110
7, 21118, 21110
8, 2111c, 21110
9, 21120, 21120
10, 21124, 21120
11, 21128, 21120
12, 2112c, 21120
Instead of allocating for each of the r pointers allocated in arr, only the first one is used to allocate memory for an rxc array. And rest of the pointers points to chunk of this memory.
Benefit can be that it is possible to use single memset to initialize the array. Freeing is much more easier (just free the first pointer's allocated memory).
arr[i] = (*arr + c * i); this is basically initializing the pointer arr[i] with the relevant section to which it should point to.
And from the start of the allocated memory where will that be?
There arr[0],arr[1]..arr[i-1] pointers which points to their rows which contains c elements each. So c elements each for i pointers - i*c elements all together is being addressed already. So the next one to be pointed by arr[i] will (*arr+c*i).
After OP edited the question:
OP asked why we need to do arr = (int **)malloc(sizeof(int *) * r) ?
I guess this picture explains a lot than words.
arr --> [0] [1] [2] [3] .....[r-2] [r-1]
| | | | | |
V | | | | |
[0] <-+ | | | |
[1] | | | |
[2] | | | |
[3] | | | |
[4] | | | |
. | | | |
| | | |
[c-1] | | | |
[c] <----+ | | |
[c+1] | | |
[c+2] | | |
. | | |
. | | |
. | | |
[2c] <----------+ | |
[2c+1] | |
[2c+2] | |
. | |
. | |
. | |
[(r-2)*c] <------------------+ |
[(r-2)*c+1] |
. |
. |
[(r-2)*c+(c-1)] |
[(r-1)*c] <----------------------+
[(r-1)*c+1]
[(r-1)*c+2]
[(r-1)*c+3]
[(r-1)*c+(c-1)]~[rc-1]
Those first row explains arr = malloc(sizeof(int *) * r);
You can see all allocated memory in the soingle column. Because that's what you did arr[0] = (int *)malloc(sizeof(int) * c * r);
And then the links explain arr[i] = (*arr + c * i);.
Check the thing, the links point to (*arr) also in pic [0]
and (*arr+c) in pic [c] and (*arr+2c) in pic [2c].
We need them because it is basically letting us get to the beginning of the correct address of the starting of each of the r rows.
The address are being calculated as offset from the beginning of the *arr.
If you didn't assign the addresses to arr[i] then you couldn't access the array like this arr[row][col] then you had to do arr[0][row*c+col] (You can see that image also says that thing).
This piece of code allocates two memory areas
arr able to contain r pointers to int
arr[0] area, for r * c int
(*arr + c * i) is same as &arr[0][c*i]
then each r (row) is assigned a pointer to a location in arr[0] spaced by c (column) int (to store c int)
for(i = 0; i < r; i++)
arr[i] = (*arr + c * i);
graphically (with r == 3, and c == 4), P pointer, I integer
arr: 0:P 1:P 2:P
| | |
v v v
arr[0]: IIIIIIIIIIII
then it just treats the array as a arr[row][col]
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
arr[i][j] = ++count;
In C, arrays are stored in "row major" order in memory. That means in a two dimensional array elements in the same row are adjacent to I've another in memory, and so elements in the same column are spaced apart by the number of columns (i.e the row width).
*arr gives you back a pointer to an int because it is dereferencing a pointer to a pointer to an int. Specifically it is the pointer pointed to by the name arr. That is the base address of the array.
arr[i] is also a pointer to an int. Now remember what I said about row major. Where should i-th row pointer point? The answer is, at the base of the array (*arr) plus i times the number of columns (c). The loop that confuses you is merely making the others point at the right places so that implied pointer arithmetic works when it comes time to use arr[r][c] notation.
Voila.

How to allocate memory using a pointer to pointer in C

I am trying to understand code snippets in
free a double pointer
and
Why use double pointer? or Why use pointers to pointers?
I want to understand the difference between the following. Both snippets are from the above urls
int** pt;
pt = (int*) malloc(sizeof(int)*10);
and
*pt = (int*) malloc(sizeof(int)*10);
Could you elaborate with some examples and drawing
First of all, the code snippet is bad for several reasons - the first is casting the result of malloc to the wrong type, and is using the wrong type to compute the amount of memory. Fixing the cast and type issues, we have:
int **pt;
pt = malloc( sizeof *pt * 10 ); // allocate space for 10 int *
*pt = malloc( sizeof **pt * 10 ); // allocate space for 10 int
After executing the first line, you have the following:
int ** int *
+---+ +---+
pt: | | --------------->| | pt[0]
+---+ +---+
| | pt[1]
+---+
| | pt[2]
+---+
...
+---+
| | pt[9]
+---+
You've set aside space for 10 int * objects, and pt points to the first of them.
The next line
*pt = malloc( sizeof **pt * 10 ); // allocate space for 10 int
allocates space for 10 int objects, and sets pt[0] to point to them:
int ** int * int
+---+ +---+ +---+
pt: | | --------------->| | pt[0] -------->| | pt[0][0]
+---+ +---+ +---+
| | pt[1] | | pt[0][1]
+---+ +---+
| | pt[2] | | pt[0][2]
+---+ +---+
... ...
+---+ +---+
| | pt[9] | | pt[0][9]
+---+ +---+
This illustrates one way of allocating a "jagged" array; you can still index it as pt[i][j], but unlike a true 2D array the rows are not adjacent in memory, and each row may be a different length. You'd normally write that as
pt = malloc( sizeof *pt * ROWS );
if ( pt )
{
for ( size_t r = 0; r < ROWS; r++ )
{
pt[r] = malloc( sizeof *pt[r] * COLS );
}
}
When that's all done, you have something like this:
int ** int * int
+---+ +---+ +---+---+ +---+
pt: | | ---------> | | pt[0] --------> | | | ... | | pt[0][0] - pt[0][COLS-1]
+---+ +---+ +---+---+ +---+
| | pt[1] ------+
+---+ | +---+---+ +---+
| | pt[2] ---+ +-> | | | ... | | pt[1][0] - pt[1][COLS-1]
+---+ | +---+---+ +---+
... |
| +---+---+ +---+
+----> | | | ... | | pt[2][0] - pt[2][COLS-1]
+---+---+ +---+
The following is wrong, compiler should complain about types:
int** pt;
pt = (int*) malloc(sizeof(int)*10);
This is also wrong for another reason (here pt does not actually point to anything usable):
int** pt;
*pt = (int*) malloc(sizeof(int)*10);
A pointer to T is a variable of type T * that may contain address of some memory that may contains elements of type T:
+------+
| | pointer to T
+------+
|
v
+-------------+-------------+-------------+
| | | | elements of type T
+-------------+-------------+-------------+
For example, in C, to obtain what is drawn, you can write:
int *pi;
pi = malloc(sizeof(int)*3);
If you have a pointer to pointer to T then then diagram could be something like:
+------+
| | pointer to pointer to T
+------+
|
v
+------+------+------+
| | | | pointers to T
+------+------+------+
| | | +-------------+-------------+-------------+
| | +---->| | | | elements of type T
| | +-------------+-------------+-------------+
| | +-------------+-------------+
| +---->| | | elements of type T
| +-------------+-------------+
|
v
+-------------+-------------+-------------+-------------+
| | | | | elements of type T
+-------------+-------------+-------------+-------------+
and the code could be:
int **ppi;
ppi = malloc(sizeof(int *)*3);
ppi[0] = malloc(sizeof(int)*3);
ppi[1] = malloc(sizeof(int)*2);
ppi[2] = malloc(sizeof(int)*4);
Of course, malloc could fail and return value should be tested against failure.
Using the casting you helped the compiler to find an error in this code snippet
int** pt;
pt = (int*) malloc(sizeof(int)*10);
For example the error message can look like
error: assignment from incompatible pointer type [-Werror=incompatible-pointer-types]
pt = (int*) malloc(sizeof(int)*10);
^
Without the casting the compiler could accept this evidently invalid code because the return type of the function malloc is void * and a pointer of the type void * may be assigned to a pointer to object of any other type.
That is in the right side of the assignment the evaluated expression has the type int * while in the left side of the assignment there is an object of the type int ** and there is no implicit conversion from the type int * to the type int **.
This code snippet
int** pt;
*pt = (int*) malloc(sizeof(int)*10);
is invalid by another reason. The pointer pt is not initialized by a valid address of an object. It has either indeterminate value if the pointer has automatic storage duration or NULL if the pointer has static storage duration. In any case its dereferencing results in undefined behavior.
So it would be correctly to write
int* pt;
^^^^^^^
pt = (int*) malloc(sizeof(int)*10);
However this construction
int** pt;
//...
*pt = (int*) malloc(sizeof(int)*10);
can be made valid in some context.
Let's assume that you declared a pointer
int *pt;
and want to initialize it in a function. In this case you have to pass the pointer to the function by reference. Otherwise the function will deal with a copy of the pointer and in this case the original pointer will not be assigned in the function.
So the corresponding code snippet can look as it is shown in the demonstrative program
#include <stdlib.h>
#include <stdio.h>
size_t f( int **pt )
{
const size_t N = 10;
*pt = (int*) malloc( sizeof( int ) * N );
if ( *pt )
{
int value = 0;
for ( size_t i = 0; i < N; i++ ) ( *pt )[i] = value++;
}
return *pt == NULL ? 0 : N;
}
int main( void )
{
int *pt;
size_t n = f( &pt );
if ( n )
{
for ( size_t i = 0; i < n; i++ ) printf( "%d ", pt[i] );
putchar( '\n' );
}
free( pt );
}
The program output is
0 1 2 3 4 5 6 7 8 9

Using mmap to allocate double 2D array

I am attempting to use mmap to create/access twi different (N+2)*(N+2) 2D array of doubles, so that multiple threads can look at their own portion of it and change it, applying the changes so that all others can see. Here is what I have:
int main(int argc, char *argv[]) {
int N = atoi(argv[1]);
int numProcs = atoi(argv[2]);
int ARRAY_SIZE = (N+2)*(N+2)*sizeof(double);
double **grid = (double **) mmap(NULL, ARRAY_SIZE, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
if (grid == MAP_FAILED) {
printf("Error mmapping grid\n");
}
double **newGrid = (double **) mmap(NULL, ARRAY_SIZE, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
if (newGrid == MAP_FAILED) {
printf("Error mmapping grid\n");
}
When I run it, or try to access anything in it, I get a segmentation fault. I tried to allocate memory as well, with:
for(i = 0; i < N+2; i++) {
for(j = 0; j < N+2; j++) {
grid[i][j] = malloc(sizeof(double));
}
}
for(i = 0; i < N+2; i++) {
for(j = 0; j < N+2; j++) {
newGrid[i][j] = malloc(sizeof(double));
}
}
But I am met with: error: assigning to 'double' from incompatible type 'void *'
newGrid[i][j] = malloc(sizeof(double));
I believe I am missing something here with how mmap works, could anyone point me in the right direction?
An array, or an array of arrays, is a contiguous area o memory. A "2d array" using pointers to pointers" is not contiguous.
Lets look at some "images" for comparison:
A proper array of arrays looks something like this in memory:
+--------------+--------------+-----+----------------+--------------+-----+------------------+
| matrix[0][0] | matrix[0][1] | ... | matrix[0][N-1] | matrix[1][0] | ... | matrix[M-1][N-1] |
+--------------+--------------+-----+----------------+--------------+-----+------------------+
On the other hand, a matrix using pointer to pointer looks something like this:
+-----------+-----------+-----------+-----+
| matrix[0] | matrix[1] | matrix[2] | ... |
+-----------+-----------+-----------+-----+
| | |
| | V
| | +--------------+--------------+-----+
| | | matrix[2][0] | matrix[2][1] | ... |
| | +--------------+--------------+-----+
| |
| V
| +--------------+--------------+-----+
| | matrix[1][0] | matrix[1][1] | ... |
| +--------------+--------------+-----+
|
V
+--------------+--------------+-----+
| matrix[0][0] | matrix[0][1] | ... |
+--------------+--------------+-----+
As you can see they memory layout is quite different, which is why you can't use one as the other.
If N is a compile-time constant or if your compiler supports variable length arrays (VLAs), then you could simply do:
double (*grid)[N+2] = (double (*)[N+2]) mmap(NULL, ARRAY_SIZE, ...
grid[4][5] = 2.0; // setting an element
If N is not constant and your compiler doesn't support VLAs, then you need to manually offset:
double *grid = (double *) mmap(NULL, ARRAY_SIZE, ...
grid[4 * (N + 2) + 5] = 2.0; // setting the same element using manual offsets

Rotate multi-dimension pointer

If I have a multidimension pointer representation of a grid like so
char **p;
int w; // width (i.e. number of columns)
int h; // height (i.e. number of rows)
How do I go about creating a copy that is rotated by 90 degrees clockwise for NxM grid?
I've tried mallocing the height as the new width, and width as new height then transposing the values. Then I was going to finish by reversing the values of the row but I haven't managed to do this.
Actual transposition is moderately painful: you have to move every element from "where it is now" to "where it should be in the transposition". If you really do have a pointer p pointing to the first of M pointers, and each of those M pointers points to the first of N chars (used as if it's an array of size M of arrays of size N of chars):
+---+ +---+---+---+---+
p ---> | * | ----> | a | b | c | d |
+---+ +---+---+---+---+
| * | --
+---+ \ +---+---+---+---+
| * | -----------> | i | j | k | l |
+---+ \ +---+---+---+---+
\
\ +---+---+---+---+
--> | e | f | g | h |
+---+---+---+---+
then you need a new pointer (which I will call q) pointing to the first of N pointers, each of which points to the first of M chars (note: this is a different transposition than you asked for):
+---+ +---+---+---+
q ---> | * | -----> | a | e | i |
+---+ +---+---+---+
| * | --
+---+ \
| * |etc \ +---+---+---+
+---+ ---> | b | f | j |
| * |etc +---+---+---+
+---+
However, if you can live with relatively annoying subscript-writing and any cache miss effects on your runtime, you can simply access p[i][j] as p[j][i] or p[N-1-j][i], etc., to "pretend" that things are transposed. This might be easiest with some macros:
#define ORIENTATION_A(p, M, N, i, j) ((p)[i][j])
#define ORIENTATION_B(p, M, N, i, j) ((p)[(N)-1-(j)][i])
/* etc */
(note: none of the above is tested).
When using type char **, since the fixed-size solution is already posted I thought I would chime in with a dynamic, \0 terminated solution that works with various-sized arrays. If it is possible to terminate the arrays h and w can be omitted. This function can figure out the h and w. Of course it may be changed to support h and w, but the powers that be would rather I get back to work funding their empire rather than providing free help.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
/* rotate_array
w h
**p _______ **q ___
|A B C D|\0 ===> |E A|\0
h |E F G H|\0 ==> |F B|\0 w
NULL----- |G C|\0
|H D|\0
NULL-
*/
char **rotate_array(char **p) {
int w,h,hh;
char **q;
for (w=0;p[0][w];w++);
for (hh=0;p[hh];hh++);
if (!(q = malloc(w * sizeof q))) {
perror ("malloc");
exit (1);
} fprintf (stderr,"made it\n");
for (w=0;p[0][w];w++) {
if (!(q[w] = malloc(hh))) {
perror ("malloc");
exit (1);
} for (h=0;h<hh;h++) {
q[w][hh-h-1] = p[h][w];
} q[w][h]='\0';
} q[w]=NULL;
return q;
} void free_array(char **p) {
int h;
for (h=0;p[h];h++) {
free (p[h]);
} free (p);
}
// main
int main (int argc, char **argv) {
int h;
char *p[3]={"ABCD","EFGH",NULL};
char **q;
for (h=0;p[h];h++) {
printf ("%s\n",p[h]);
} printf ("\n");
q = rotate_array (p);
for (h=0;q[h];h++) {
printf ("%s\n",q[h]);
} free_array (q);
return 0;
}

Resources