检查字符串是否存在于未初始化的字符串数组中
Checking if a string exists in an uninitialized 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;
}
基本上,如果数组中没有元素,我想将其放入数组中。我如何使用未初始化为其中包含值的数组来执行此操作?
char *myArray[100] // initailize array
我想在 myArray 上调用 exists_in(),但这会给我一个段错误 11,因为 myArray 中没有值。
使用size
表示有效条目的数量,如果你从0到size-1
填充数组。
如果数组不是连续填充的,或者如果您以后可能想从中删除项目,请在每个元素中使用 NULL
初始化第一个 'empty' 数组。 (如果您稍后删除某个元素,请不要忘记将其重置回 NULL
。)
然后在 strcmp
:
之前的循环中添加对 NULL
的显式测试
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;
}
通过添加更多实用函数来扩展已接受的答案
bool store_in(char *string, char *array[], size_t size);
bool delete_in(char *string, char *array[], size_t size);
和测试程序:
#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);
}
输出:
Storing: 123
Exists: 123
Exists: 123
Delete: 123
Storing: 123
我有一个辅助函数来确定一个字符串是否存在于字符串数组中:
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;
}
基本上,如果数组中没有元素,我想将其放入数组中。我如何使用未初始化为其中包含值的数组来执行此操作?
char *myArray[100] // initailize array
我想在 myArray 上调用 exists_in(),但这会给我一个段错误 11,因为 myArray 中没有值。
使用size
表示有效条目的数量,如果你从0到size-1
填充数组。
如果数组不是连续填充的,或者如果您以后可能想从中删除项目,请在每个元素中使用 NULL
初始化第一个 'empty' 数组。 (如果您稍后删除某个元素,请不要忘记将其重置回 NULL
。)
然后在 strcmp
:
NULL
的显式测试
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;
}
通过添加更多实用函数来扩展已接受的答案
bool store_in(char *string, char *array[], size_t size);
bool delete_in(char *string, char *array[], size_t size);
和测试程序:
#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);
}
输出:
Storing: 123
Exists: 123
Exists: 123
Delete: 123
Storing: 123