Thread: Dynamic two dimensional Array of Characters

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    103

    Dynamic two dimensional Array of Characters

    Hi all

    I am trying to write the code for creating two dimension array of character and assign them value.

    The code is:

    Code:
    int _tmain(int argc, _TCHAR* argv[])
    {
    	   char **arrKey;
    	   int i,total_no=5; 
    
           arrKey =(char **)malloc(total_no * 100);
           memset(arrKey,0x40,sizeof(arrKey));
    
    	   for ( i = 0; i< 5; i++ )
    	   {
    		   memcpy(arrKey,"100",3);
    	   }
           for ( i = 0; i< 5; i++ )
    	   {
    		   printf("\n %s",arrKey);
    	   }
        getchar();  
    	return 0;
    }

    The output is:


    100@══════════════════════════════════════════════ ═════════════════════════════
    ══════════════════════════════════════════════════ ══════════════════════════════
    ══════════════════════════════════════════════════ ══════════════════════════════
    ══════════════════════════════════════════════════ ══════════════════════════════
    ══════════════════════════════════════════════════ ══════════════════════════════
    ══════════════════════════════════════════════════ ══════════════════════════════
    ═════════════════════²²²²½½½½½½½½
    100@══════════════════════════════════════════════ ═════════════════════════════
    ══════════════════════════════════════════════════ ══════════════════════════════
    ══════════════════════════════════════════════════ ══════════════════════════════
    ══════════════════════════════════════════════════ ══════════════════════════════
    ══════════════════════════════════════════════════ ══════════════════════════════
    ══════════════════════════════════════════════════ ══════════════════════════════
    ═════════════════════²²²²½½½½½½½½
    100@══════════════════════════════════════════════ ═════════════════════════════
    ══════════════════════════════════════════════════ ══════════════════════════════
    ══════════════════════════════════════════════════ ══════════════════════════════
    ══════════════════════════════════════════════════ ══════════════════════════════
    ══════════════════════════════════════════════════ ══════════════════════════════
    ══════════════════════════════════════════════════ ══════════════════════════════
    ═════════════════════²²²²½½½½½½½½
    100@══════════════════════════════════════════════ ═════════════════════════════
    ══════════════════════════════════════════════════ ══════════════════════════════
    ══════════════════════════════════════════════════ ══════════════════════════════
    ══════════════════════════════════════════════════ ══════════════════════════════
    ══════════════════════════════════════════════════ ══════════════════════════════
    ══════════════════════════════════════════════════ ══════════════════════════════
    ═════════════════════²²²²½½½½½½½½
    100@══════════════════════════════════════════════ ═════════════════════════════
    ══════════════════════════════════════════════════ ══════════════════════════════
    ══════════════════════════════════════════════════ ══════════════════════════════
    ══════════════════════════════════════════════════ ══════════════════════════════
    ══════════════════════════════════════════════════ ══════════════════════════════
    ══════════════════════════════════════════════════ ══════════════════════════════
    ═════════════════════²²²²½½½½½½½½
    I am getting the above result. I am doing memset to reset the dynamic array.
    Can anybody help me in this regard so that the output is proper.

    What I really want from above code is that, I want a array to be created at run time which look like this:

    a[][] = {"aaaaaa","bbbbbb","ccccccc","ddddddd","UPTO LOOPS RUN"};

    Please suggest me if any one has more efficient way to do that.

    Thanks

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    103
    Thanks Bayint,

    I have made changes as the link provide.

    Please have a look and let me know if any further changes are required to make it safe in all respect.

    Code:
    // 2_DIMENSION_ARRAY.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    #include <stdlib.h>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
         char **arrKey;
         int i,total_no=5; 	   
         arrKey =(char **)malloc(total_no * 100);
    
         if ( arrKey == NULL)
            return 0;
    
          memset(arrKey,0x40,sizeof(arrKey));
    
          for ( i = 0; i< 5; i++ )
         {
             arrKey[i] = (char *)malloc(100 * sizeof(char));
             if (arrKey == NULL)
    	return 0;
    	   }
           for ( i = 0; i< 5; i++ )
           {
               memcpy(arrKey[i],"10000000000000000",30);
           }
    	   
           for ( i = 0; i< 5; i++ )
    	   {
            printf("\n %s",arrKey[i]);
          }
        getchar();  
    	return 0;
    }
    I am getting correct output now.


    10000000000000000
    10000000000000000
    10000000000000000
    10000000000000000
    10000000000000000
    Thanks
    Last edited by nickman; 11-24-2010 at 11:17 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This looks wrong (sizeof only gives you the size of a pointer here) and unnecessary in the first place:
    Code:
    memset(arrKey,0x40,sizeof(arrKey));
    Remember to free what you malloc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    You should use strcpy instead memcpy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 03:56 AM
  2. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM