0

I am not very experienced in C. I am trying to have some code that works like this:

Declare array of strings;

Function that erases array of strings and inserts a new set of strings (number unknown)

How can I do this? I know I can do const char *a[2]; but that requires entering an array size when I declare it. Can I make a variable for my string array that can support multiple sizes?

1
  • Which version of C are you using? C99? Commented Mar 19, 2014 at 2:04

3 Answers 3

4

You can use pointers to pointers.

char **strings;

Here is how you would create it: (where <size> is the size of the array)

strings = malloc(sizeof(char*) * <size>);

Now setting/getting elements is pretty simple:

strings[0] = "hello";
printf("%s", strings[0]);

Just a warning, the memory is not already set to fully null strings. If you want all of the strings to be empty by default then use calloc() instead of malloc():

strings = calloc(sizeof(char*) , <size>);
Sign up to request clarification or add additional context in comments.

5 Comments

That would make all the pointers to the strings NULL, not make all the strings NUL-terminated. You'd segfault if you dereferenced them, not get an empty string.
So if I put the "strings = malloc..." line in the function, can I call it multiple times and have it empty the array?
technically, yes that is what malloc does, but the smarter way would just be to loop over all of the elements and set them to "". Actually, if you are just trying to repopulate the array then you might as well just go through replacing the elements and then setting the remaining elements to "". Let me know if that is too confusing and if so then ill make an edit explaining it
Alright. The only problem is that the size will be different each time, meaning the first time I call it it may have 5 elements while the second time it might have 10 elements. I would need malloc then right?
in that case you 100% need another malloc
0

You can control array size like this:

   int size;
   char *a[size];
   printf("enter size");
   scanf("%d",&size);

Comments

0

You have to use a char**, a pointer to pointer to char, to by able to capture a dynamic array of strings.

If you want 10 strings, you have to use:

char** strings = (char**)malloc(sizeof(char*)*10);

To store a string in the first element of the array, you have to use:

strings[0] = malloc(strlen("abcd")+1);
strpcy(strings[0], "abcd");

Here's a program that demonstrates the whole thing:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
   char string[200];
   char** strings = NULL;
   int numStrings = 0;
   int n = 0;
   int i = 0;

   /* Read the number of strings */
   do
   {
      printf("Enter the number of strings (0 or higher): ");
      n = scanf("%d", &numStrings);
   } while ( n != 1 || numStrings < 0 );

   /* Eat up the remaining characters afte the integer */
   fgets(string, 199, stdin);

   if ( numStrings > 0 )
   {
      /* Read the strings */
      strings = (char**)malloc(numStrings*sizeof(char*));
      for ( i = 0; i != numStrings; ++i )
      {
         printf("Enter a string: ");
         fgets(string, 199, stdin);
         strings[i] = malloc(strlen(string)+1);
         strcpy(strings[i], string);
      }

      /* Print the strings back */
      for ( i = 0; i != numStrings; ++i )
      {
         printf("%s", strings[i]);
      }

      /* Now release the memory back to the system */

      /* First the individual strings */
      for ( i = 0; i != numStrings; ++i )
      {
         free(strings[i]);
      }

      /* Now the array */
      free(strings);
   }

   return 0;
}

1 Comment

parts of this is needlessly complex. for example the "storing a string" part can just be: strings[0] = "abcd"; It's both faster and shorter

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.