I got some C code,
there is
typedef struct frame_struct
{
char **data; /* array of 1s and 0s */
short int hsize; /* horizontal size */
short int vsize; /* ver size*/
}frame_t;
please tell me what is char **data
I got some C code,
there is
typedef struct frame_struct
{
char **data; /* array of 1s and 0s */
short int hsize; /* horizontal size */
short int vsize; /* ver size*/
}frame_t;
please tell me what is char **data
can i look char **data as an array like data[][].
if i can, how to access any term in this array,
can i use *((*data)+1)to access data[0][1].
char **data;
Here data is a pointer to a pointer. You can use this to create for example 2D arrays or arrays of strings.
> can i look char **data as an array like data[][].
Not exactly. An array is fixed-size. To be able to use "char **data" for storing data, you must allocate memory for it first.
> can i use *((*data)+1)to access data[0][1].
When you have allocated memory for it, you can acces the memory in the same way as you would do it with arrays.
When you want to do address calculations. You need to calculate the exact position.
Thank you for replying. I am a newbie, please explain it in detail.
Thanx.
--------------------------------------------------------------------------------
>>be able to use "char **data" for storing data, you must allocate memory for it first.
How to allocate memory for it.
can i use
**data=(char*)calloc(1,sizeof(char))
For a one dimensional array (string) you can allocate memory in this way:
A two dimensional array can be seen as an array of strings. You need to allocate memory for each string.Code:char *string; string = malloc (sizeof (char) * STRING_LEN);
Don't forget to free() the memory when you don't need it anymore. For the two dimensional array you need to free each string apart.Code:// First allocate memory for the pointers char **string_array; string_array = malloc (sizeof (char *) * NR_OF_STRINGS); // Then allocate memory for each string for (index = 0; index < NR_OF_STRINGS; index++) { string_array [index] = malloc (sizeof (char) * STRING_LEN); }
Also it is usefull to perform a check if the memory was really allocated. If string_array[2] == NULL, then for string 2 there could no memory be allocated.