如下所示,我想将
char** x
定义的变量以只读方式传递给函数。
参考书显示了线性搜索源代码,通过将其作为由
char *x
定义的临时参数传递给 search
函数,对由 const int a[]
定义的变量执行线性搜索。
所以我想到了一个想法,如果是字符串呢?然后我写了下面的代码。
gcc、clang 和 MSVC 分析可以在这里找到。
// Linear Search
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int scanf_s(const char *format ,...); // for gcc and clang environment
// int search(const char **a, int n, char *key) { //※[1]not allowed in c lang specification⇒NG
// int search(char* const *a, int n, char *key) { //※[2]⇒NG
// int search(const char* const *a, int n, char *key) { //※[3]warning occured in gcc and clang!!
// int search(char const * const *a, int n, char *key) {//※[4]same as above
// int search(const char* const a[], int n, char* key) {//※[5]same as above
// int search(const char* a[], int n, char* key) { //※[6]I thought this was ok, but warning occured in gcc and clang!!
int search(char** a, int n, char* key) { //in conclusion, gcc,clang and MSVC only allowing this!!
int i = 0;
for (i = 0; i < n; i++) {
if (strcmp(a[i], key) == 0)
return i;
}
return -1;
/* or while style!! the reference book shows this style!!
while (1) {
if (i == n)
return -1;
if (strcmp(a[i], key) == 0)
return i;
i++;
}
*/
}
int main(void) {
char** x;
char* ky;
int nx;
int idx;
int i;
puts("Linear Search");
printf("How many Elements??:");
scanf_s("%d", &nx);
x = malloc(sizeof(char*) * nx);
if (x == NULL) {
printf("Pointer to Pointer x malloc failed!!\n");
exit(EXIT_FAILURE);
}
for (i = 0; i < nx; i++) {
printf("x[%d]:", i);
x[i] = malloc(sizeof(char) * 35);
if (x[i] == NULL) {
printf("Pointer Array x[] malloc failed!!\n", i);
exit(EXIT_FAILURE);
}
scanf_s("%s", x[i], 35);
}
printf("Target Value:");
ky = malloc(sizeof(char) * 35);
if (x == NULL) {
printf("target value malloc failed!!\n");
exit(EXIT_FAILURE);
}
// Or
// ky = calloc(35, sizeof(char));
scanf_s("%s", ky, 35);
idx = search(x, nx, ky);
if (idx == -1)
puts("no target value.");
else
printf("%s is in x[%d]!!\n", ky, idx);
free(ky);
for (i = 0; i < nx; i++) {
free(x[i]);
}
free(x);
system("pause");
return 0;
}
带有
const
的代码会使 gcc 和 clang 显示警告,尽管 MSVC 可以编译而没有任何警告,如注释(※[1]~[6])所示。
那么,在
search
函数必须接受 char** x
定义的变量作为只读参数的情况下,如何编写代码呢?
编译器希望数组中的字符串是
const char*
类型。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define STRING_LENGTH 36
int scanf_s(const char *format ,...);
unsigned int search(const char* const stringArray[], unsigned int stringCount, const char* stringToSearch)
{
for(unsigned int i = 0; i < stringCount; i++)
{
if(strcmp(stringArray[i], stringToSearch) == 0)
{
return i;
}
}
return UINT_MAX;
}
int main(void)
{
printf("Enter the count of strings: ");
unsigned int strCount;
scanf_s("%u", &strCount);
char* *strArray = malloc(sizeof(char*) * strCount);
if(strArray == NULL)
{
printf("Could not get memory for the string array!\n");
return EXIT_FAILURE;
}
for(unsigned int i = 0; i < strCount; i++)
{
strArray[i] = calloc(sizeof(char), STRING_LENGTH);
if(strArray[i] == NULL)
{
printf("Could not get memory for the next string!\n");
return EXIT_FAILURE;
}
printf("Enter %uth string (%i characters at most): ", (i + 1), (STRING_LENGTH - 1));
scanf_s("%s", strArray[i], STRING_LENGTH);
}
char* strToSearch = calloc(sizeof(char), STRING_LENGTH);
if(strToSearch == NULL)
{
printf("Could not get memory for the string to be searched!\n");
return EXIT_FAILURE;
}
printf("Enter string to be searched (%i characters at most) : ", (STRING_LENGTH - 1));
scanf_s("%s", strToSearch, STRING_LENGTH);
unsigned int result = search((const char* const*)strArray, strCount, strToSearch);
if(result != UINT_MAX) {
printf("String found at: %u\n", result);
} else {
printf("String was not found!\n");
}
free(strToSearch);
for(unsigned int i = 0; i < strCount; i++)
{
free(strArray[i]);
}
free(strArray);
return EXIT_SUCCESS;
}
我在您提供的链接中尝试了此代码。 当我用 gcc 编译并运行它时,示例输出如下:
Enter the count of strings: 3
Enter 1th string (35 characters at most): Goodbye
Enter 2th string (35 characters at most): Cruel
Enter 3th string (35 characters at most): World
Enter string to be searched (35 characters at most) : Cruel
String found at: 1
注意:最后一个版本更好。 :)
检查这里