Array of pointer issue - Unix

This is a discussion on Array of pointer issue - Unix ; Hi All, I have an array of pointers like this char *(ptr)[2]; I use malloc to allocate space for each index.. so (ptr)[0] = malloc (2 * sizeof(char)); (ptr)[1] = malloc (10 * sizeof(char)); As you can see the space ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: Array of pointer issue

  1. Array of pointer issue

    Hi All,
    I have an array of pointers like this

    char *(ptr)[2];

    I use malloc to allocate space for each index.. so

    (ptr)[0] = malloc (2 * sizeof(char));
    (ptr)[1] = malloc (10 * sizeof(char));

    As you can see the space allocated for ptr[1] is 5 times the space for
    ptr[1]. My problem is with referencing these pointers. So for instance
    how can I assign the character 'a' to the first byte of ptr[0] and 'b'
    to the second byte it and then 'c', 'd', 'e', to first, second and
    maybe forth byte of ptr[1].

    Thanks


  2. Re: Array of pointer issue

    On Apr 2, 11:28 pm, "Jack" wrote:
    > Hi All,
    > I have an array of pointers like this
    >
    > char *(ptr)[2];
    >
    > I use malloc to allocate space for each index.. so
    >
    > (ptr)[0] = malloc (2 * sizeof(char));
    > (ptr)[1] = malloc (10 * sizeof(char));
    >
    > As you can see the space allocated for ptr[1] is 5 times the space for
    > ptr[1]. My problem is with referencing these pointers. So for instance
    > how can I assign the character 'a' to the first byte of ptr[0]
    > and 'b' to the second byte [of] it


    ptr[0][0] = 'a';
    ptr[0][1] = 'b';

    > and then 'c', 'd', 'e', to first, second and
    > maybe forth byte of ptr[1].


    ptr[1][0] = 'c';
    ptr[1][1] = 'd';
    ptr[1][2] = 'e';
    or
    memcpy(ptr[1], "cde", 3);

    >
    > Thanks




  3. Re: Array of pointer issue

    Jack wrote:
    > Hi All,
    > I have an array of pointers like this
    >
    > char *(ptr)[2];
    >
    > I use malloc to allocate space for each index.. so
    >
    > (ptr)[0] = malloc (2 * sizeof(char));
    > (ptr)[1] = malloc (10 * sizeof(char));
    >

    One extra point, sizeof(char) is by definition, 1.

    --
    Ian Collins.

  4. Re: Array of pointer issue

    On 3 Apr, 03:28, "Jack" wrote:

    > I have an array of pointers like this
    >
    > char *(ptr)[2];


    Just in case, parenthesis here are not strictly necessary.

    > I use malloc to allocate space for each index.. so
    >
    > (ptr)[0] = malloc (2 * sizeof(char));
    > (ptr)[1] = malloc (10 * sizeof(char));
    >
    > As you can see the space allocated for ptr[1] is 5 times the space for
    > ptr[1]. My problem is with referencing these pointers. So for instance
    > how can I assign the character 'a' to the first byte of ptr[0] and 'b'
    > to the second byte it and then 'c', 'd', 'e', to first, second and
    > maybe forth byte of ptr[1].


    One way to do the job is to allocate the whole array at once:

    char* array = malloc(2 + 10);
    ptr[0] = array;
    ptr[1] = array + 2;
    memcpy(array, "abcde", sizeof "abcde" - 1);



+ Reply to Thread