Thread: declaring array of strings dynamically!!!!

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    4

    declaring array of strings dynamically!!!!

    somebody out there would like to help me???

    My BIG problem:
    -----------------------------
    is there a way to assign an array of strings dynamically in C ?(e.g. malloc())

    I tried to declare a char pointer: char **str;

    and..

    try to assign:

    *str = (char *)(malloc (20));

    is this a proper assignment or allocating an array of strings in C?
    I compiled it successfully, but crashed my system as I runned it, what's the problem with my code??? can somebody help me???


    help plz.


    thanks a lot,
    REYAL

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    4
    thanks a lot Salem,

    I tried your code, it compile successfully, but still crushed by system.

    By the way, this are my module implementations in allocating memory for every file. I didn't include the graphics implementation and the main module.


    ----------module for display the file in an inputted directory---------

    fname is define outside this modules (char *fname), it is a holder for the current directory.


    Code:
    #include <dir.h>
    #include <stdlib.h>
    #include <graphics.h>
    
    
    struct ffblk ffblk;
    char **files;
    
    void countMem_to_Alloc(){
    	int done,counter=0,i;
    
    	done = findfirst(fname,&ffblk,0);
    	while(!done){
    		done = findnext(&ffblk);
    		counter++;
    	}
    
    		files = malloc(counter * sizeof(char));
    		for(i=0;i<counter;i++) files[i] = malloc(50*sizeof(char));
    }
    
    void saveFiles(int* nDex){
    	int i,done;
    
    	countMem_to_Alloc();
    
    	done = findfirst(fname,&ffblk,0);
    	while(!done){
    		done = findnext(&ffblk);
    		strcpy(files[*nDex++],ffblk.ff_name);
    	}
    }
    
    void displayDir(){
    	int i,index=0,
    		chHeight = textheight("H"),
    		curX = 10,curY = 66;
    
    	saveFiles(&index);
    
    	setcolor(14);
    	settextstyle(2,0,0);
    	for(i=0;i<index;i++){
    		  if(i==42){curX = 272;curY = 66;}
    
    			outtextxy(curX,curY,files[i]);
    			curY += chHeight;
    	}
    
    	for(i=0;i<index;i++)free(files[i]);
    	free(files);
    }

    thanks a lot man for your quick reply, this is actually our project program to be passed tomorrow.

    hope you'll help with this.


    thanks,
    REYAL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. How To Declare and Dynamically Size a Global 2D Array?
    By groberts1980 in forum C Programming
    Replies: 26
    Last Post: 11-15-2006, 09:07 AM
  4. File I/O problem for dynamically allocated struct array
    By veecee in forum C++ Programming
    Replies: 2
    Last Post: 05-05-2006, 09:28 PM
  5. Array of strings in C
    By szill in forum C Programming
    Replies: 10
    Last Post: 02-22-2005, 05:03 PM