Thread: How to malloc (char **)

  1. #1

    Post How to malloc (char **)

    Hi all,

    I am having some trouble with using malloc() and a (char **). Here is a look at my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct Words {
    	char **wordList;
    };
    
    int main() {
    	int l;
    	int listCount = 4;
    	char words[5][25] = {"Season", "Fan", "Car", "Fruit"};
    	Words *filter;
    
    	filter = (Words*) malloc ( sizeof(Words) );
    
    	// Write bad words to char array
    	for (l = 0; l < listCount; l++) {
    		filter->wordList[l] = (char *) malloc (strlen(words[l]) + 1);
    
    		strcpy( filter->wordList[l], words[l] );
    	}
    
    	printf("%s\n", filter->wordList[0]);
    
    	for (l = 0; l < listCount; l++) {
    		free((char *)filter->wordList[l]);
    	}
    	free((Words*)filter);
    
    	return 0;
    }
    I'm trying to stay away from using an array of any sort like a char *[], I was just hoping there was a way to allocate memory to a (char **). This is also C, so yes, I would have used new and delete in a split second if it were C++.


    Thank you for your time,
    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Something like this?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct Words
    {
       char **wordList;
    };
    
    int main(void)
    {
       int i, listCount = 4;
       char words[5][25] = {"Season", "Fan", "Car", "Fruit"};
       struct Words filter;
    
       filter.wordList = malloc(listCount * sizeof *filter.wordList);
       if ( filter.wordList )
       {
          for ( i = 0; i < listCount; i++ )
          {
             filter.wordList[i] = malloc(strlen(words[i]) + 1);
             if ( filter.wordList[i] )
             {
                strcpy( filter.wordList[i], words[i] );
                printf("%s\n", filter.wordList[i]);
             }
          }
          for ( i = 0; i < listCount; i++ )
          {
             free(filter.wordList[i]);
          }
          free(filter.wordList);
       }
       return 0;
    }
    
    /* my output
    Season
    Fan
    Car
    Fruit
    */
    >This is also C

    Not really.
    Last edited by Dave_Sinkula; 05-06-2004 at 06:20 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3

    Smile

    Thanks,

    That's exactly what I was looking for

    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM