开发者

C Grid using pointers and Malloc

开发者 https://www.devze.com 2023-02-18 15:56 出处:网络
I am Trying to work through some class examples and have gotten stuck on the following: The array grid should

I am Trying to work through some class examples and have gotten stuck on the following:

The array grid should have length width with each entry representing a column of cells. Columns that have some occupied cells should be a malloc'ed character array of length height.

with the given header:

void grid(char **grid, int width, int height)

grid is defined in another file as:

char **grid;

As I have said I have gotten stuck on using malloc, I currently have:

int x;
*grid = malloc(width * sizeof(char));

        for(x = 0; x < width; x++){
            grid[x] = malloc(height * sizeof(char));
        }

Can any one take a look at give me some pointers on the correct way to accomplish "Columns that have some occupied cells should be a malloc'ed character array of length height.", As I dont understand how the line:

grid[x] = malloc(height * sizeof(char));

is equivalent to an开发者_如何学编程 array of char's

Thanks


char** grid; is a pointer to pointer.

grid = malloc( width* sizeof( char* ) ) ;  // Statement 1

for( int i=0; i<height; ++i )
{
    grid[i] = malloc( height ) ; // Statement 2
}

Understand char** -> char* -> char. So first need to reserve for holding addresses amounting to width. By Statement 1, it is achieved. Now each of these index should point to a memory location holding height characters and is achieved by Statement 2.


Diagramatic representation sounds more clear than description. Hope that helps !

C Grid using pointers and Malloc


In C, an array is a pointer to the first element of the array. So an array is just a memory block, with the array variable pointing onto the first element in this memory block.

malloc() reserves a new memory block of the specified size. To know the size of a type (i.e. number of bytes needed to store one variable of that type), one uses the sizeof operator. Hence a character needs sizeof(char) bytes, and hence height characters need height * sizeof(char).

So with the malloc() call you allocate a memory block to store all the elements of the array, and malloc() returns a pointer onto the first of them.

With C's definition for an array variable (a pointer onto the first element), you can assign the results of malloc(...) to your array variable.


Use this:

grid = malloc(width * sizeof(char *));

You want to allocate space for width pointers to char - And then you correctly allocate the individual pointers to height chars in the loop.

Using a typedef makes this more visible:

typedef char * charpointer;
charpointer * grid = malloc(width * sizeof(charpointer));


  1. First you allocate space for array of [width] char pointers. So instead of *grid = malloc(width * sizeof(char)); which allocates space for [width] chars, you should use *grid = malloc(width * sizeof(* char)); (the difference is that char is one byte, and char pointer is int (usually 32 bit, but architecture dependent)
  2. In your loop, each time you allocate (an array of) [hight] chars and store the pointer to it in one of the pointers that you allocated in (1). so grid[x], actually points to an allocated buffer of chars (which is your array)

hope I made it clear.

0

精彩评论

暂无评论...
验证码 换一张
取 消