Thread: char** in struct amd malloc()

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    char** in struct and malloc()

    Hi, I am trying to pass data to a .DLL I want to use. DLL accepts this structure for its data.

    Code:
    PTR 01FB317C    -> 01EEB208 -> ASCII "DATA0"
                       018947A0 ->          01F72f30 -> ASCII "DATA1"
                                     +8     01F74558 -> ASCII "DATA2"
                                     +12    01F742E8 -> ASCII "DATA3"
               			 +16    01F73FF8 -> ASCII "DATA4"
                                     +20    01FD3CF8 -> ASCII "DATA5"
              		         +24    01FD4EC8 -> ASCII "DATA6"
                			 +28    01F724D8 -> ASCII "DATA7"
                     		 +32             -> ASCII "DATA8"
                			 +36             -> ASCII "DATA9"
                			 +40             -> ASCII "DATA10"
    	    			 +44             -> ASCII "DATA11"
                			 +48             -> ASCII "DATA12"
    Pointers are from a call from real application.ASCII strings are of size 256.


    So i created this structure:


    Code:
    typedef struct
    	{
    	     char *s1;
                 char ** ss1;
    	} Rec1;

    Now I am trying to malloc before I put data on it


    Code:
          Rec1 *a;
    	
            a = (Rec1 *)malloc(sizeof(Rec1));
    	
    	a->s1 = (char*)malloc(16*sizeof(char));
    	
    	strcpy(a->s1, "DATA0"); <--- This works OK as seen on debugger
    	
    	a->ss1 = (char**) malloc(48);  <--- !CRASH!!! ; Trying to allocate space for pointers
    	
    	for( i=0; i < 12; i++)
    	a->ss1[i] = (char *)malloc(256);
    	
    	
    	strcpy(a->ss1[0], "DATA1");
    
            .......
    
    
            free(a);
    	for( i=0; i < 12; i++)
                 free( a->ss1[i] );
    	free( a->ss1 );
    Problem is the above malloc() crashes with C0000005

    Can you help me how is the correct way to do it?


    PS: On 1st declaration of structure it is char *........1; I keep editing the message and it keeps ignoring my edit always showing ......1; !!!! @#!
    PS2: I managed to do it
    Last edited by alexopth1512; 10-27-2010 at 03:56 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. casting struct to struct
    By klmdb in forum C Programming
    Replies: 6
    Last Post: 08-14-2010, 02:29 PM
  2. C problem - Create a Personal Library
    By Harliqueen in forum C Programming
    Replies: 33
    Last Post: 04-20-2010, 11:27 PM
  3. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 05:38 PM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

Tags for this Thread