Thread: array of pointers of strings?

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    6

    Question array of pointers of strings?

    hi all

    i'm trying to do array of pointers, each pointer is a string.

    can any one help me please, this is my code:
    PHP Code:
    int main (void)
    {
        
    int imax=0ext=0;
        
    char *strch;

        
    str =(char *)malloc(10 sizeof(char));
        
    char **=(char **)malloc(10 sizeof(char));
        
        if(
    str == NULL)
            
    printf("\nError...\n");
        else
        {
            
    i=0;
            do
            {
                
    s[i] =(char *)malloc(10 sizeof(char));
                
    scanf("%c", &ch);
                while (
    ch != '\n')
                {
                    *
    str ch;            
                    
    str++;
                    
    scanf("%c", &ch);
                }
                
    strcpy(s[i], str);
                
    i++;

            }while (*(
    s[i]) != "Stop"); //the error is here.....!!!
            
    max i;    

            
    //printf("\nname is = %s\n", str);
            //puts(str);

            
    for(int j=0j<maxj++)
            {
                
    puts(s[j]);
            }

        }
        return 
    0;

    whats wrong with my code???!!!
    Last edited by thefalcon79; 01-12-2011 at 06:10 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    You can't compare C strings like this. Use strcmp, in string.h, instead.
    Devoted my life to programming...

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    whats wrong with my code???!!!
    1. You are casting malloc.
    2. You are calling malloc incorrectly at one point (char **s).
    3. You are not checking the return value of malloc.
    4. You are not freeing the memory allocated by malloc.
    5. Since you do not check for it, your code can potentially loop past i = 9 at which point you'll be lucky to not crash.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    When allocating for s you need to malloc room for a number of char pointers... not chars.
    You seem to be ruining the pointer str... incrementing it, then using it to copy to s[i]. By that time it's past the end of the string. Also, for next time, it's way out to lunch.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    69
    @thefalcon79:
    There does not seem to be anything right about your code.

    Here is how you allocate an array of character string pointers:
    Code:
        char **str;   // declare and array of (array-of-char_pointers)
    
        str = malloc(5 * sizeof(char *));      // allocate 5 (array-of-char_pointers)
        str[0] = malloc(10 * sizeof(char));  // allocate array[0] of 10 chars
        str[1] = malloc(10 * sizeof(char));  // allocate array[1] of 10 chars
        // etc...
    
        strcpy(str[0], "hello ");
        strcpy(str[1], "world!");

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    22
    Quote Originally Posted by thefalcon79 View Post
    hi all

    i'm trying to do array of pointers, each pointer is a string.

    can any one help me please, this is my code:
    Code:
    int main (void)
    {
    	int i, max=0, ext=0;
    	char *str, ch;
    
    	str =(char *)malloc(10 * sizeof(char));
    	char **s =(char **)malloc(10 * sizeof(char));
    	
    	if(str == NULL)
    		printf("\nError...\n");
    	else
    	{
    		i=0;
    		do
    		{
    			s[i] =(char *)malloc(10 * sizeof(char));
    			scanf("%c", &ch);
    			while (ch != '\n')
    			{
    				*str = ch;			
    				str++;
    				scanf("%c", &ch);
    			}
    			strcpy(s[i], str);
    			i++;
    
    		}while (*(s[i]) != "Stop"); //the error is here.....!!!
    		max = i;	
    
    		//printf("\nname is = %s\n", str);
    		//puts(str);
    
    		for(int j=0; j<max; j++)
    		{
    			puts(s[j]);
    		}
    
    	}
    	return 0;
    }
    whats wrong with my code???!!!
    To declare array of pointers use calloc function...

    Code:
    char **s=(char**)calloc(size of array,sizeof(char*))
    Now doing
    Code:
    s[i] =(char *)malloc(10 * sizeof(char));
    will be valid...

    Hope this helps

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    calloc isn't getting you anything more than malloc is doing already (as previously demonstrated).

    In particular, using calloc to allocate an array of pointers does NOT guarantee that you have an array of pointers initialised to NULL.
    Question 7.31

    Additionally, casting the result of malloc/calloc in a C program is a bad idea.
    Cprogramming.com FAQ > Casting malloc
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of strings problems...
    By chubigans in forum C Programming
    Replies: 4
    Last Post: 08-27-2009, 03:13 PM
  2. Creating an array of pointers to int[]
    By OkashiiKen in forum C Programming
    Replies: 3
    Last Post: 09-29-2006, 06:48 PM
  3. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  4. Concatenating strings (dynamic array using pointers)
    By Tankndozer in forum C Programming
    Replies: 8
    Last Post: 07-01-2004, 07:27 AM
  5. Replies: 5
    Last Post: 11-20-2001, 12:48 PM