0

I need to use char **userIDArray to store a list of user ID's (32 chars max), which will be added to the list one by one. The number of ID's to be stored is unknown.

My initial idea was to create another char **start - a pointer to the beginning of the array. Then I would allocate space in *userIDArray. The code should be something like:

if (arraySize == 0)
{
     userIDArray = malloc(sizeof(*userIDArray));
     *userIDArray = malloc(32 * sizeof(char));
     strcpy(*userIDArray, userID);
     start = userIDArray;
}
else
{
     int i = 0;
     while(i < arraySize && strcmp(*userIDArray, userID) != 0)
     {
          i++;
          userIDArray++;
     }
     if(strcmp(*userIDArray, userID) == 0)
     {
        printf("already in the array");
     }
     else
     {
         arraySize++;
         start = realloc(start, arraySize * sizeof(*userIDArray));
         *userIDArray = malloc(32 * sizeof(char));
         strcpy(*userIDArray, userID);
     }
     userIDArray = start;
}

This gives me all kinds of errors. Is there any simpler way to add to multidimensional arrays?

3
  • As written, your example will not compile at all because your code is not sitting inside a function. Please show what you believe to be syntactically correct program. Commented Jun 6, 2014 at 20:36
  • this is just a fragment of course. I wanted to know whether the general idea is good. Commented Jun 6, 2014 at 20:38
  • If you need O(1) random access, the idea is sound, but make sure you double the size of the array each time you need to allocate more space, in order to get amortized O(1) append. Commented Jun 6, 2014 at 20:46

1 Answer 1

3

The number of ID's to be stored is unknown.

In this case, an array is a poor choice for storage. If you create an array of strings, a char**, then you'll need to allocate enough space for the maximum number of strings up front or continuously reallocate userIDArray.

A better design would be to store the data in a structure such as a linked list, which is easy to add or remove from.

Sign up to request clarification or add additional context in comments.

2 Comments

I would argue that a dynamically-sized array is not necessarily a poor choice for a data structure. Plenty of languages implement lists as array lists on top of plain C arrays. Sure, the syntax might be ugly because you have to manage the array doubling yourself, but it also gives you O(1) random access.
That's fair. I thought about going into that, but didn't since this looks like it's likely a CS assignment. OP: If you really and the dynamically sized array, most languages that do what univerio is talking about allocate excess space in order to minimize the number of allocations. You might should consider a struct that contains the size allocated, the number of elements added, and the pointer to the data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.