1

I have a helper function that determines if a string exists within an array of strings:

bool exists_in(char *string, char *array[], int size){
    int i;
    for(i = 0; i < size; ++i){
        if(strcmp(string, array[i]) == 0)
            printf("%s\n", array[i]);
            return true;
    }
    return false;
}

Basically, I want to put an element inside the array if it isn't already in there. How can I do this with an array that isn't initialized to have values in it?

char *myArray[100] // initailize array

I want to call exists_in() on myArray, but this will give me a segfault 11 because there are no values in the myArray.

2
  • Note that your if statement's braces are missing. Commented Jan 14, 2018 at 22:37
  • Doh. That saved me a lot of trouble, thanks! Commented Jan 14, 2018 at 22:44

2 Answers 2

4

Use size to indicate the number of valid entries if you fill the array up from 0 to size-1.

If the array is not filled consecutively, or if you may want to delete items from it afterwards, initialize the very first 'empty' array with NULL in each element. (Do not forget to reset an element back to NULL if you delete it later.)

Then add an explicit test on NULL in your loop before the strcmp:

char *myArray[100] = { NULL }; // initialize array with NULL
// (all elements will be initialized to 0 with this construction)

...

bool exists_in(char *string, char *array[], size_t size)
{
    size_t i;
    for(i = 0; i < size; ++i)
    {
        if (array[i] && strcmp(string, array[i]) == 0)
        {
            printf("%s\n", array[i]);
            return true;
        }
    }
    return false;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Expanding the accepted answer by adding more utility functions

bool store_in(char *string, char *array[], size_t size);
bool delete_in(char *string, char *array[], size_t size);

and test program:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h> // bool C99

bool exists_in(char *string, char *array[], size_t size)
{
    size_t i;
    for(i = 0; i < size; ++i)
    {
        if (array[i] && strcmp(string, array[i]) == 0)
        {
            printf("Exists:  %s\n", array[i]);
            return true; // exists
        }
    }
    return false;
}

bool store_in(char *string, char *array[], size_t size)
{
    size_t i;
    for(i = 0; i < size; ++i)
    {
        if (array[i] == 0)
        {
            array[i] = string;

            printf("Storing: %s\n", array[i]);
            return true; // stored
        }
    }
    return false;
}

bool delete_in(char *string, char *array[], size_t size)
{
    size_t i;
    for(i = 0; i < size; ++i)
    {
        if (array[i] && strcmp(string, array[i]) == 0)
        {
            printf("Delete:  %s\n", array[i]);
            array[i] = NULL;
            return true; // deleted
        }
    }
    return false;
}


int main()
{
    char *my_array[100] = { NULL }; // initialize array with NULL
    // (all elements will be initialized to 0 with this construction)    

   //1.
   if (! exists_in("123", my_array, 100) ) // Does not exists 
       store_in("123",my_array, 100);      // It will store 
   printf("\n");

   //2.      
   if (! exists_in("123", my_array, 100))  // Exists
       store_in("123",my_array, 100);      // It will not store 
   printf("\n");    

    //3.
   if (exists_in("123", my_array, 100))  
       delete_in("123",my_array, 100);     // It will delete
    printf("\n");   

    //4.   
    if (! exists_in("123", my_array, 100)) // Does not exists 
       store_in("123",my_array, 100);     // It will store

    return (0);
}

Output:

Storing: 123

Exists:  123

Exists:  123
Delete:  123

Storing: 123

Comments

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.