Thread: double pointer to 2-dim array?

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    16

    double pointer to 2-dim array?

    suppose I have a double pointer that stores words in a list:

    ex:
    char **wordlist;

    How do I store the data of **wordlist into a 2-dimensional array say List[define][define2] which is also a char?

  2. #2
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    strcpy( list[x], wordlist[y] )

    i think, check man page for strncpy to make it better

    edit: youll need to put a loop in there too
    "Assumptions are the mother of all **** ups!"

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You need to allocate lots of memory

    Do this once
    Code:
    /* allocate space for all the word pointers */
    wordlist = malloc ( numberOfWords * sizeof *wordlist );

    Do this for all the words you want to copy
    Code:
    wordlist[i] = malloc ( strlen(myWord)+1 );
    strcpy( wordlist[i], myWord );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Passing a pointer to two-dimension array in a function
    By E_I_S in forum C++ Programming
    Replies: 11
    Last Post: 06-19-2008, 09:57 AM
  3. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  4. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 12:04 PM
  5. "subscript requires array or pointer type"?
    By Nutka in forum C Programming
    Replies: 12
    Last Post: 12-06-2002, 05:51 PM