pointer of array

Discussion in 'C Programming' started by NA Gary, Jun 28, 2003.

  1. NA Gary

    NA Gary Guest

    How to "malloc" a pointer of fixed length char array such as char
    *(vertex[3]) ?

    I encounter syntax errors for these cases

    vertex = (**char)malloc(sizeof(char)*3) ;
    vertex = (**char)malloc(sizeof(*char)) ;
     
    NA Gary, Jun 28, 2003
    #1
    1. Advertising

  2. "NA Gary" <> wrote (28 Jun 2003) in news:bdjbd1
    $dtu$ / comp.lang.c:

    > How to "malloc" a pointer of fixed length char array such as char
    > *(vertex[3]) ?
    >
    > I encounter syntax errors for these cases
    >
    > vertex = (**char)malloc(sizeof(char)*3) ;
    > vertex = (**char)malloc(sizeof(*char)) ;


    #include <stdlib.h>
    #include <stdio.h>
    #define NPTRS 3
    #define SSIZE 256

    int main(void)
    {
    char *(vertex[NPTRS]);
    size_t i, j, n = sizeof vertex / sizeof *vertex;
    printf("'vertex' is an array of %lu pointers to char\n"
    "On this implementation, sizeof(char *) is %lu\n"
    " sizeof vertex is %lu\n"
    " and sizeof *vertex is %lu\n"
    "Since vertex is an array, it makes no sense to\n"
    "try to allocate it. You allocate space for the\n"
    "%lu pointers to point to.\n\n"
    "For example,\n"
    " vertex = malloc(%lu * sizeof *vertex);\n"
    "makes no sense.\n\n",
    (unsigned long) n,
    (unsigned long) sizeof(char *),
    (unsigned long) sizeof vertex,
    (unsigned long) sizeof *vertex,
    (unsigned long) n,
    (unsigned long) n);
    printf
    ("Attempting to allocate %d bytes for each of the %lu pointers
    \n",
    SSIZE, (unsigned long) n);
    for (i = 0; i < n; i++) {
    if ((vertex = malloc(SSIZE)))
    printf("malloc for vertex[%lu] succeeded.\n",
    (unsigned long) i);
    else {
    printf("malloc for vertex[%lu] failed.\n"
    "quiting ...", (unsigned long) i);
    for (j = 0; j < i; j++)
    free(vertex);
    exit(EXIT_FAILURE);

    }
    }
    for (i = 0; i < n; i++)
    free(vertex);
    return 0;
    }


    'vertex' is an array of 3 pointers to char
    On this implementation, sizeof(char *) is 4
    sizeof vertex is 12
    and sizeof *vertex is 4
    Since vertex is an array, it makes no sense to
    try to allocate it. You allocate space for the
    3 pointers to point to.

    For example,
    vertex = malloc(3 * sizeof *vertex);
    makes no sense.

    Attempting to allocate 256 bytes for each of the 3 pointers
    malloc for vertex[0] succeeded.
    malloc for vertex[1] succeeded.
    malloc for vertex[2] succeeded.


    --
    Martin Ambuhl
    Returning soon to the
    Fourth Largest City in America
     
    Martin Ambuhl, Jun 28, 2003
    #2
    1. Advertising

  3. In 'comp.lang.c', "NA Gary" <> wrote:

    > How to "malloc" a pointer of fixed length char array such as char
    > *(vertex[3]) ?


    > I encounter syntax errors for these cases
    >
    > vertex = (**char)malloc(sizeof(char)*3) ;
    > vertex = (**char)malloc(sizeof(*char)) ;


    a usual:

    #inclide <stdlib.h>
    ....
    char *vertex = malloc (3);
    or
    char *vertex = malloc (sizeof *vertex * 3);

    --
    -ed- [remove YOURBRA before answering me]
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    C-library: http://www.dinkumware.com/htm_cl/index.html
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
     
    Emmanuel Delahaye, Jun 28, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sangeetha

    Array of pointer Vs Pointer to Array

    sangeetha, Oct 8, 2004, in forum: C Programming
    Replies:
    9
    Views:
    357
    Tim Rentsch
    Oct 9, 2004
  2. erfan

    Array of pointer and pointer of array

    erfan, Jan 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    682
    Martin Ambuhl
    Jan 28, 2008
  3. RSL
    Replies:
    14
    Views:
    3,339
  4. Hansen
    Replies:
    3
    Views:
    1,133
    rep_movsd
    Apr 24, 2010
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    471
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page