Thread: Please come in

  1. #1
    Registered User jacktibet's Avatar
    Join Date
    Mar 2003
    Posts
    18

    Exclamation Please come in

    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

  2. #2
    Registered User jacktibet's Avatar
    Join Date
    Mar 2003
    Posts
    18

    attached

    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].

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    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.

  4. #4
    Registered User jacktibet's Avatar
    Join Date
    Mar 2003
    Posts
    18

    Question Thanx.

    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))

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    For a one dimensional array (string) you can allocate memory in this way:

    Code:
    char *string;
    string = malloc (sizeof (char) * STRING_LEN);
    A two dimensional array can be seen as an array of strings. You need to allocate memory for each string.

    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);
    }
    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.

    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.

Popular pages Recent additions subscribe to a feed