Dynamic two dimensional Array of Characters

This is a discussion on Dynamic two dimensional Array of Characters within the C Programming forums, part of the General Programming Boards category; Hi all I am trying to write the code for creating two dimension array of character and assign them value. ...

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

    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
    81
    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
    22,159
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 07:18 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. 2D array in dynamic memory
    By VanJay011379 in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2003, 08:34 PM

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