Question

This is a discussion on Question within the C Programming forums, part of the General Programming Boards category; Hey guys, I just need some help thinking through a line of code: words = (char **)malloc(sizeof(char *)*TOTAL*TOTAL); I know ...

  1. #1
    PhysicistTurnedProgrammer Cell's Avatar
    Join Date
    Jan 2009
    Location
    New Jersey
    Posts
    72

    Question

    Hey guys, I just need some help thinking through a line of code:

    words = (char **)malloc(sizeof(char *)*TOTAL*TOTAL);
    I know that you'd need to cast (char*) onto malloc() to allocate memory for chars - but why is a 2-dimensional pointer necessary? The values (TOTAL) multiplying the malloc() value just creates a larger value - not a 2-dimensional one, right?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Cell View Post
    but why is a 2-dimensional pointer necessary?
    Is it?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    The variable name "words" would imply an array of pointers to words, in other words, a pointer to a char pointer, so the cast to char ** makes sense. Why it is allocated to an array of TOTAL*TOTAL elements, there is no way of knowing without more code.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    PhysicistTurnedProgrammer Cell's Avatar
    Join Date
    Jan 2009
    Location
    New Jersey
    Posts
    72
    Sorry, I should have included the variables and values in the code I posted.

    #define TOTAL (100 )

    char **words;

  5. #5
    PhysicistTurnedProgrammer Cell's Avatar
    Join Date
    Jan 2009
    Location
    New Jersey
    Posts
    72
    Quote Originally Posted by brewbuck View Post
    The variable name "words" would imply an array of pointers to words, in other words, a pointer to a char pointer, so the cast to char ** makes sense. Why it is allocated to an array of TOTAL*TOTAL elements, there is no way of knowing without more code.
    Would it make sense if 'words' was not an array? Because it looks like it's just a 2D pointer.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Cell View Post
    Would it make sense if 'words' was not an array?
    Wow, would it ever. It doesn't matter what 'words' is -- you are just assigning it a block of memory of a specific size. As long as it's a pointer.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    PhysicistTurnedProgrammer Cell's Avatar
    Join Date
    Jan 2009
    Location
    New Jersey
    Posts
    72
    Quote Originally Posted by MK27 View Post
    Wow, would it ever. It doesn't matter what 'words' is -- you are just assigning it a block of memory of a specific size. As long as it's a pointer.
    So assigning a block of memory to a 2D pointer differs from a 1D how? I'm not sure I see the benefit of 2D vs. 1D in memory assignment because there is no data structure being defined.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by Cell View Post
    Would it make sense if 'words' was not an array? Because it looks like it's just a 2D pointer.
    "words" is a pointer to a pointer to char not a 2D pointer.

  9. #9
    PhysicistTurnedProgrammer Cell's Avatar
    Join Date
    Jan 2009
    Location
    New Jersey
    Posts
    72
    Quote Originally Posted by itCbitC View Post
    "words" is a pointer to a pointer to char not a 2D pointer.
    Ohh, now I feel ridiculous! However, I guess I don't understand this pointer to a pointer idea.

    char **words;
    This statement defines 'words' to be a pointer to a pointer. But doesn't there have to be an initial location pointed to? Wouldn't a 'pointer to a pointer' include a pointer?

    Something like:

    char *ptr1;

    char *ptr2 = *ptr1; or char **ptr2 = *ptr1;
    Is ** some sort of shorthand for this?

    Though now I do understand:

    words = (char **)malloc(sizeof(char *)*TOTAL*TOTAL);
    Since 'words' is already a pointer to a pointer, malloc() needs to be casted to a pointer to a pointer.

    Is there a name for a pointer to a pointer? It's a mouthful.
    Last edited by Cell; 02-03-2009 at 08:44 PM.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by Cell View Post
    This statement defines 'words' to be a pointer to a pointer. But doesn't there have to be an initial location pointed to? Wouldn't a 'pointer to a pointer' include a pointer?
    That's why the return value from malloc() is cast appropriately.
    Quote Originally Posted by Cell View Post
    Since 'words' is already a pointer to a pointer, malloc() needs to be casted to a pointer to a pointer.
    Exactly!
    Quote Originally Posted by Cell View Post
    Is there a name for a pointer to a pointer? It's a mouthful.
    I suppose you could call it a double indirect pointer since it has two levels of indirection.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Cell View Post
    So assigning a block of memory to a 2D pointer differs from a 1D how? I'm not sure I see the benefit of 2D vs. 1D in memory assignment because there is no data structure being defined.
    You're right, it's the same thing. You don't have to cast malloc.

    Don't worry about pointers to pointers until you get pointers. Then you'll get pointers to pointers, too
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21