Thread: skeletal 'dynamic array of strings' function - tweak needed

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    2

    skeletal 'dynamic array of strings' function - tweak needed

    Hello, I am trying to use a function to add strings to a 'dynamic array of strings' and wondered if someone might help me fnish this skeletal piece of code.
    The function fill_array_x() works, the function fill_array_y() crashes where indicated. I'm looking for a simple fix here - hopefully a missing * or & will wrap it up. Later I will look at using structs, etc. All constructive advice welcome.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    char *names[] = { "one", "two"};
    char **my_str_array = NULL;
    /*-------------------------------------*/
    int fill_array_x() /* this works for the global string */
    {
        if (!(my_str_array = malloc(( 2 * sizeof(char*)))))  return(-1);
     my_str_array[0] = names[0];
     my_str_array[1] = names[1];
        return 0;
    }
    /*-------------------------------------*/
    int fill_array_y(char ***str_array) /* this generic function has issues */
    {
        if (!(*str_array = malloc(( 2 * sizeof(char*)))))  return(-1);
     *str_array[0] = names[0];
     *str_array[1] = names[1]; /*crashes here*/
        return 0;
    }
    /*-------------------------------------*/
    int main()
    {
     int i;
    
    /* if (fill_array_x(my_str_array) !=0) return 9; */ /* works */
       if (fill_array_y(&my_str_array) !=0) return 9;   /* does not work*/
    
     for (i = 0; i < 2; i++)
         printf( "'%s'\n", my_str_array[i]);
    
     free(my_str_array);
     return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    *str_array[0] = names[0];
    *str_array[1] = names[1]; /*crashes here*/


    These you need to write as
    (*str_array)[0] = names[0];
    (*str_array)[1] = names[1];
    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.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    2

    Thanks!

    Quote Originally Posted by Salem View Post
    *str_array[0] = names[0];
    *str_array[1] = names[1]; /*crashes here*/


    These you need to write as
    (*str_array)[0] = names[0];
    (*str_array)[1] = names[1];
    That did it. Thanks for the fast and clear reply!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and Using a dynamic array of C strings?
    By swbluto in forum C Programming
    Replies: 11
    Last Post: 11-17-2009, 07:09 PM
  2. beginner: dynamic array of strings
    By pc2-brazil in forum C++ Programming
    Replies: 10
    Last Post: 04-29-2008, 04:29 PM
  3. Dynamic allocation of an array of strings
    By earth_angel in forum C Programming
    Replies: 3
    Last Post: 06-24-2005, 09:40 AM
  4. some major pointers/dynamic array help needed
    By kv2 in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2004, 11:32 PM
  5. Inputing strings into a dynamic array
    By Nakeerb in forum C++ Programming
    Replies: 1
    Last Post: 10-12-2002, 02:38 AM