如何在C中创建字符串数组?

时间:2023-02-07 07:41:47

I am trying to create an array of strings in C. If I use this code:

我正在尝试在c中创建一个字符串数组,如果我使用这个代码:

char (*a[2])[14];
a[0]="blah";
a[1]="hmm";

gcc gives me "warning: assignment from incompatible pointer type". What is the correct way to do this?

gcc给我“警告:不兼容指针类型的赋值”。正确的方法是什么?

edit: I am curious why this should give a compiler warning since if I do printf(a[1]);, it correctly prints "hmm".

编辑:我很好奇为什么应该给编译器一个警告,因为如果我执行printf(一个[1]),它会正确地输出“hmm”。

14 个解决方案

#1


175  

If you don't want to change the strings, then you could simply do

如果你不想改变字符串,那么你可以做的很简单

const char *a[2];
a[0] = "blah";
a[1] = "hmm";

When you do it like this you will allocate an array of two pointers to const char. These pointers will then be set to the addresses of the static strings "blah" and "hmm".

当您这样做时,您将为const char分配一个由两个指针组成的数组。然后将这些指针设置为静态字符串“blah”和“hmm”的地址。

If you do want to be able to change the actual string content, the you have to do something like

如果您希望能够更改实际的字符串内容,则必须执行类似的操作。

char a[2][14];
strcpy(a[0], "blah");
strcpy(a[1], "hmm");

This will allocate two consecutive arrays of 14 chars each, after which the content of the static strings will be copied into them.

这将分配两个连续的数组,每个数组包含14个字符,之后静态字符串的内容将被复制到它们中。

#2


138  

There are several ways to create an array of strings in C. If all the strings are going to be the same length (or at least have the same maximum length), you simply declare a 2-d array of char and assign as necessary:

在c中创建字符串数组有几种方法,如果所有字符串的长度都相同(或至少具有相同的最大长度),只需声明一个2d char数组,并根据需要进行分配:

char strs[NUMBER_OF_STRINGS][STRING_LENGTH+1];
...
strcpy(strs[0], aString); // where aString is either an array or pointer to char
strcpy(strs[1], "foo");

You can add a list of initializers as well:

您还可以添加一个初始化器列表:

char strs[NUMBER_OF_STRINGS][STRING_LENGTH+1] = {"foo", "bar", "bletch", ...};

This assumes the size and number of strings in the initializer match up with your array dimensions. In this case, the contents of each string literal (which is itself a zero-terminated array of char) are copied to the memory allocated to strs. The problem with this approach is the possibility of internal fragmentation; if you have 99 strings that are 5 characters or less, but 1 string that's 20 characters long, 99 strings are going to have at least 15 unused characters; that's a waste of space.

这假定初始化器中的字符串大小和数量与您的数组维数匹配。在这种情况下,每个字符串文字的内容(它本身是一个零终止的字符数组)被复制到分配给strs的内存中。这种方法的问题是内部碎片化的可能性;如果有99个字符串是5个字符或更少,但是1个字符串是20个字符,那么99个字符串至少有15个未使用的字符;那是浪费空间。

Instead of using a 2-d array of char, you can store a 1-d array of pointers to char:

您可以存储1-d数组的指针,而不是使用2-d数组的char:

char *strs[NUMBER_OF_STRINGS];

Note that in this case, you've only allocated memory to hold the pointers to the strings; the memory for the strings themselves must be allocated elsewhere (either as static arrays or by using malloc() or calloc()). You can use the initializer list like the earlier example:

注意,在这种情况下,您只分配了内存来保存指向字符串的指针;字符串本身的内存必须分配到其他地方(要么作为静态数组,要么使用malloc()或calloc())))。您可以使用初始化列表,如前面的示例:

char *strs[NUMBER_OF_STRINGS] = {"foo", "bar", "bletch", ...};

Instead of copying the contents of the string constants, you're simply storing the pointers to them. Note that string constants may not be writable; you can reassign the pointer, like so:

而不是复制字符串常量的内容,而是将指针存储到它们。注意,字符串常量可能不可写;可以重新分配指针,如下所示:

strs[i] = "bar";
strs[i] = "foo"; 

But you may not be able to change the string's contents; i.e.,

但是您可能无法更改字符串的内容;也就是说,

strs[i] = "bar";
strcpy(strs[i], "foo");

may not be allowed.

可能不被允许。

You can use malloc() to dynamically allocate the buffer for each string and copy to that buffer:

您可以使用malloc()动态分配每个字符串的缓冲区,并将其复制到该缓冲区:

strs[i] = malloc(strlen("foo") + 1);
strcpy(strs[i], "foo");

BTW,

顺便说一句,

char (*a[2])[14];

Declares a as a 2-element array of pointers to 14-element arrays of char.

将a声明为指向char的14元素数组的指针的2元素数组。

#3


71  

Ack! Constant strings:

Ack !常量字符串:

const char *strings[] = {"one","two","three"};

If I remember correctly.

如果我没记错的话。

Oh, and you want to use strcpy for assignment, not the = operator. strcpy_s is safer, but it's neither in C89 nor in C99 standards.

噢,你想用strcpy作为赋值,而不是=运算符。strcpy_s更安全,但它既不在C89中,也不在C99标准中。

char arr[MAX_NUMBER_STRINGS][MAX_STRING_SIZE]; 
strcpy(arr[0], "blah");

Update: Thomas says strlcpy is the way to go.

更新:Thomas说strlcpy是正确的。

#4


10  

In ANSI C:

在ANSI C:

char* strings[3];
strings[0] = "foo";
strings[1] = "bar";
strings[2] = "baz";

#5


10  

Here are some of your options:

以下是你的一些选择:

char a1[][14] = { "blah", "hmm" };
char* a2[] = { "blah", "hmm" };
char (*a3[])[] = { &"blah", &"hmm" };  // only since you brought up the syntax -

printf(a1[0]); // prints blah
printf(a2[0]); // prints blah
printf(*a3[0]); // prints blah

The advantage of a2 is that you can then do the following with string literals

a2的优点是,您可以使用字符串文本来执行以下操作。

a2[0] = "hmm";
a2[1] = "blah";

And for a3 you may do the following:

对于a3,你可以这样做:

a3[0] = &"hmm";
a3[1] = &"blah";

For a1 you will have to use strcpy even when assigning string literals. The reason is that a2, and a3 are arrays of pointers and you can make their elements (i.e. pointers) point to any storage, whereas a1 is an array of 'array of chars' and so each element is an array that "owns" its own storage (which means it gets destroyed when it goes out of scope) - you can only copy stuff into its storage.

对于a1,您将不得不使用strcpy,即使在分配字符串常量时也是如此。原因是a2和a3数组的指针,你可以让他们的元素(即指针)指向任何存储,而a1是一个字符数组的数组,每个数组元素是“拥有”自己的存储(这意味着它被摧毁时超出范围)——你可以只复制内容到它的存储。

This also brings us to the disadvantage of using a2 and a3 - since they point to static storage (where string literals are stored) the contents of which cannot be reliably changed (viz. undefined behavior), if you want to assign non-string literals to the elements of a2 or a3 - you will first have to dynamically allocate enough memory and then have their elements point to this memory, and then copy the characters into it - and then you have to be sure to deallocate the memory when done.

这也给我们带来了使用a2和a3 -因为他们的缺点指出静态存储(存储字符串位置)的内容不能可靠地改变了(viz.未定义行为),如果你想分配non-string文字a2和a3的元素——你首先要动态地分配足够的内存,然后有自己的元素指向该内存,然后将字符复制到它,然后你必须完成后一定要释放内存。

Bah - I miss C++ already ;)

Bah -我已经怀念c++了;)

p.s. Let me know if you need examples.

如果你需要例子,请告诉我。

#6


10  

Or you can declare a struct type, that contains a character arry(1 string), them create an array of the structs and thus a milti element array

或者您可以声明一个struct类型,它包含一个字符arry(1 string),它们创建一个结构的数组,从而创建一个milti元素数组。

typedef struct name
{
   char name[100]; // 100 character array
}name;

main()
{
   name yourString[10]; // 10 strings
   printf("Enter something\n:);
   scanf("%s",yourString[0].name);
   scanf("%s",yourString[1].name);
   // maybe put a for loop and a few print ststements to simplify code
   // this is just for example 
 }

One of the advantages of this over any other method is that this allows you to scan directly into the string without having to use strcpy;

这种方法的优点之一是,它允许您直接扫描字符串,而不必使用strcpy;

#7


6  

The string literals are const char *s.

字符串文字是const char *s。

And your use of parenthesis is odd. You probably mean

括号的使用是奇数。你的意思是

const char *a[2] = {"blah", "hmm"};

which declares an array of two pointers to constant characters, and initializes them to point at two hardcoded string constants.

它向常量字符声明两个指针的数组,并将它们初始化为指向两个硬编码的字符串常量。

#8


5  

If the strings are static, you're best off with:

如果字符串是静态的,那么最好是:

const char *my_array[] = {"eenie","meenie","miney"};

While not part of basic ANSI C, chances are your environment supports the syntax. These strings are immutable (read-only), and thus in many environments use less overhead than dynamically building a string array.

虽然不是基本的ANSI C的一部分,但是您的环境很可能支持语法。这些字符串是不可变的(只读),因此在许多环境中,使用的开销比动态构建字符串数组要少。

For example in small micro-controller projects, this syntax uses program memory rather than (usually) more precious ram memory. AVR-C is an example environment supporting this syntax, but so do most of the other ones.

例如,在小型微控制器项目中,这种语法使用程序内存,而不是(通常)更宝贵的ram内存。AVR-C是支持这种语法的一个示例环境,但是其他大多数都是这样。

#9


5  

If you don't want to keep track of number of strings in array and want to iterate over them, just add NULL string in the end:

如果您不想跟踪数组中的字符串数量,并希望对它们进行迭代,那么只需在末尾添加空字符串:

char *strings[]={ "one", "two", "three", NULL };

int i=0;
while(strings[i]) {
  printf("%s\n", strings[i]);
  //do something
  i++;
};

#10


3  

Your code is creating an array of function pointers. Try

您的代码正在创建一个函数指针数组。试一试

char* a[size];

or

char a[size1][size2];

instead.

代替。

See wikibooks to arrays and pointers

参见*到数组和指针

#11


0  

char name[10][10]
int i,j,n;//here "n" is number of enteries
printf("\nEnter size of array = ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
    for(j=0;j<1;j++)
    {
        printf("\nEnter name = ");
        scanf("%s",&name[i]);
    }
}
//printing the data
for(i=0;i<n;i++)
{
    for(j=0;j<1;j++)
    {
        printf("%d\t|\t%s\t|\t%s",rollno[i][j],name[i],sex[i]);
    }
    printf("\n");
}

Here try this!!!

在这里试试这个! ! !

#12


0  

I was missing somehow more dynamic array of strings, where amount of strings could be varied depending on run-time selection, but otherwise strings should be fixed.

我缺少了更动态的字符串数组,其中字符串的数量可以根据运行时的选择而变化,但是其他字符串应该是固定的。

I've ended up of coding code snippet like this:

我最后编写了这样的代码片段:

#define INIT_STRING_ARRAY(...)          \
    {                                   \
        char* args[] = __VA_ARGS__;     \
        ev = args;                      \
        count = _countof(args);         \
    }

void InitEnumIfAny(String& key, CMFCPropertyGridProperty* item)
{
    USES_CONVERSION;
    char** ev = nullptr;
    int count = 0;

    if( key.Compare("horizontal_alignment") )
        INIT_STRING_ARRAY( { "top", "bottom" } )

    if (key.Compare("boolean"))
        INIT_STRING_ARRAY( { "yes", "no" } )

    if( ev == nullptr )
        return;

    for( int i = 0; i < count; i++)
        item->AddOption(A2T(ev[i]));

    item->AllowEdit(FALSE);
}

char** ev picks up pointer to array strings, and count picks up amount of strings using _countof function. (Similar to sizeof(arr) / sizeof(arr[0])).

char** ev获取指向数组字符串的指针,计数使用_countof函数获取字符串数量。(类似于sizeof(arr) / sizeof(arr[0]))

And there is extra Ansi to unicode conversion using A2T macro, but that might be optional for your case.

使用A2T宏可以进行额外的Ansi到unicode转换,但这对于您的情况来说可能是可选的。

#13


0  

hello you can try this bellow :

大家可以试试下面这个:

 char arr[nb_of_string][max_string_length]; 
 strcpy(arr[0], "word");

a nice example of using, array of strings in c if you want it

一个使用字符串数组的很好的例子

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


int main(int argc, char *argv[]){

int i, j, k;

// to set you array
//const arr[nb_of_string][max_string_length]
char array[3][100];

char temp[100];
char word[100];

for (i = 0; i < 3; i++){
    printf("type word %d : ",i+1);
    scanf("%s", word);
    strcpy(array[i], word);
}

for (k=0; k<3-1; k++){
    for (i=0; i<3-1; i++)
    {
        for (j=0; j<strlen(array[i]); j++)
        {
            // if a letter ascii code is bigger we swap values
            if (array[i][j] > array[i+1][j])
            {
                strcpy(temp, array[i+1]);
                strcpy(array[i+1], array[i]);
                strcpy(array[i], temp);

                j = 999;
            }

            // if a letter ascii code is smaller we stop
            if (array[i][j] < array[i+1][j])
            {
                    j = 999;
            }

        }
    }
}

for (i=0; i<3; i++)
{
    printf("%s\n",array[i]);
}

return 0;
}

#14


-6  

A good way is to define a string your self.

一个好的方法是定义一个字符串你自己。

#include <stdio.h>
typedef char string[]
int main() {
    string test = "string";
    return 0;
}

It's really that simple.

真的那么简单。

#1


175  

If you don't want to change the strings, then you could simply do

如果你不想改变字符串,那么你可以做的很简单

const char *a[2];
a[0] = "blah";
a[1] = "hmm";

When you do it like this you will allocate an array of two pointers to const char. These pointers will then be set to the addresses of the static strings "blah" and "hmm".

当您这样做时,您将为const char分配一个由两个指针组成的数组。然后将这些指针设置为静态字符串“blah”和“hmm”的地址。

If you do want to be able to change the actual string content, the you have to do something like

如果您希望能够更改实际的字符串内容,则必须执行类似的操作。

char a[2][14];
strcpy(a[0], "blah");
strcpy(a[1], "hmm");

This will allocate two consecutive arrays of 14 chars each, after which the content of the static strings will be copied into them.

这将分配两个连续的数组,每个数组包含14个字符,之后静态字符串的内容将被复制到它们中。

#2


138  

There are several ways to create an array of strings in C. If all the strings are going to be the same length (or at least have the same maximum length), you simply declare a 2-d array of char and assign as necessary:

在c中创建字符串数组有几种方法,如果所有字符串的长度都相同(或至少具有相同的最大长度),只需声明一个2d char数组,并根据需要进行分配:

char strs[NUMBER_OF_STRINGS][STRING_LENGTH+1];
...
strcpy(strs[0], aString); // where aString is either an array or pointer to char
strcpy(strs[1], "foo");

You can add a list of initializers as well:

您还可以添加一个初始化器列表:

char strs[NUMBER_OF_STRINGS][STRING_LENGTH+1] = {"foo", "bar", "bletch", ...};

This assumes the size and number of strings in the initializer match up with your array dimensions. In this case, the contents of each string literal (which is itself a zero-terminated array of char) are copied to the memory allocated to strs. The problem with this approach is the possibility of internal fragmentation; if you have 99 strings that are 5 characters or less, but 1 string that's 20 characters long, 99 strings are going to have at least 15 unused characters; that's a waste of space.

这假定初始化器中的字符串大小和数量与您的数组维数匹配。在这种情况下,每个字符串文字的内容(它本身是一个零终止的字符数组)被复制到分配给strs的内存中。这种方法的问题是内部碎片化的可能性;如果有99个字符串是5个字符或更少,但是1个字符串是20个字符,那么99个字符串至少有15个未使用的字符;那是浪费空间。

Instead of using a 2-d array of char, you can store a 1-d array of pointers to char:

您可以存储1-d数组的指针,而不是使用2-d数组的char:

char *strs[NUMBER_OF_STRINGS];

Note that in this case, you've only allocated memory to hold the pointers to the strings; the memory for the strings themselves must be allocated elsewhere (either as static arrays or by using malloc() or calloc()). You can use the initializer list like the earlier example:

注意,在这种情况下,您只分配了内存来保存指向字符串的指针;字符串本身的内存必须分配到其他地方(要么作为静态数组,要么使用malloc()或calloc())))。您可以使用初始化列表,如前面的示例:

char *strs[NUMBER_OF_STRINGS] = {"foo", "bar", "bletch", ...};

Instead of copying the contents of the string constants, you're simply storing the pointers to them. Note that string constants may not be writable; you can reassign the pointer, like so:

而不是复制字符串常量的内容,而是将指针存储到它们。注意,字符串常量可能不可写;可以重新分配指针,如下所示:

strs[i] = "bar";
strs[i] = "foo"; 

But you may not be able to change the string's contents; i.e.,

但是您可能无法更改字符串的内容;也就是说,

strs[i] = "bar";
strcpy(strs[i], "foo");

may not be allowed.

可能不被允许。

You can use malloc() to dynamically allocate the buffer for each string and copy to that buffer:

您可以使用malloc()动态分配每个字符串的缓冲区,并将其复制到该缓冲区:

strs[i] = malloc(strlen("foo") + 1);
strcpy(strs[i], "foo");

BTW,

顺便说一句,

char (*a[2])[14];

Declares a as a 2-element array of pointers to 14-element arrays of char.

将a声明为指向char的14元素数组的指针的2元素数组。

#3


71  

Ack! Constant strings:

Ack !常量字符串:

const char *strings[] = {"one","two","three"};

If I remember correctly.

如果我没记错的话。

Oh, and you want to use strcpy for assignment, not the = operator. strcpy_s is safer, but it's neither in C89 nor in C99 standards.

噢,你想用strcpy作为赋值,而不是=运算符。strcpy_s更安全,但它既不在C89中,也不在C99标准中。

char arr[MAX_NUMBER_STRINGS][MAX_STRING_SIZE]; 
strcpy(arr[0], "blah");

Update: Thomas says strlcpy is the way to go.

更新:Thomas说strlcpy是正确的。

#4


10  

In ANSI C:

在ANSI C:

char* strings[3];
strings[0] = "foo";
strings[1] = "bar";
strings[2] = "baz";

#5


10  

Here are some of your options:

以下是你的一些选择:

char a1[][14] = { "blah", "hmm" };
char* a2[] = { "blah", "hmm" };
char (*a3[])[] = { &"blah", &"hmm" };  // only since you brought up the syntax -

printf(a1[0]); // prints blah
printf(a2[0]); // prints blah
printf(*a3[0]); // prints blah

The advantage of a2 is that you can then do the following with string literals

a2的优点是,您可以使用字符串文本来执行以下操作。

a2[0] = "hmm";
a2[1] = "blah";

And for a3 you may do the following:

对于a3,你可以这样做:

a3[0] = &"hmm";
a3[1] = &"blah";

For a1 you will have to use strcpy even when assigning string literals. The reason is that a2, and a3 are arrays of pointers and you can make their elements (i.e. pointers) point to any storage, whereas a1 is an array of 'array of chars' and so each element is an array that "owns" its own storage (which means it gets destroyed when it goes out of scope) - you can only copy stuff into its storage.

对于a1,您将不得不使用strcpy,即使在分配字符串常量时也是如此。原因是a2和a3数组的指针,你可以让他们的元素(即指针)指向任何存储,而a1是一个字符数组的数组,每个数组元素是“拥有”自己的存储(这意味着它被摧毁时超出范围)——你可以只复制内容到它的存储。

This also brings us to the disadvantage of using a2 and a3 - since they point to static storage (where string literals are stored) the contents of which cannot be reliably changed (viz. undefined behavior), if you want to assign non-string literals to the elements of a2 or a3 - you will first have to dynamically allocate enough memory and then have their elements point to this memory, and then copy the characters into it - and then you have to be sure to deallocate the memory when done.

这也给我们带来了使用a2和a3 -因为他们的缺点指出静态存储(存储字符串位置)的内容不能可靠地改变了(viz.未定义行为),如果你想分配non-string文字a2和a3的元素——你首先要动态地分配足够的内存,然后有自己的元素指向该内存,然后将字符复制到它,然后你必须完成后一定要释放内存。

Bah - I miss C++ already ;)

Bah -我已经怀念c++了;)

p.s. Let me know if you need examples.

如果你需要例子,请告诉我。

#6


10  

Or you can declare a struct type, that contains a character arry(1 string), them create an array of the structs and thus a milti element array

或者您可以声明一个struct类型,它包含一个字符arry(1 string),它们创建一个结构的数组,从而创建一个milti元素数组。

typedef struct name
{
   char name[100]; // 100 character array
}name;

main()
{
   name yourString[10]; // 10 strings
   printf("Enter something\n:);
   scanf("%s",yourString[0].name);
   scanf("%s",yourString[1].name);
   // maybe put a for loop and a few print ststements to simplify code
   // this is just for example 
 }

One of the advantages of this over any other method is that this allows you to scan directly into the string without having to use strcpy;

这种方法的优点之一是,它允许您直接扫描字符串,而不必使用strcpy;

#7


6  

The string literals are const char *s.

字符串文字是const char *s。

And your use of parenthesis is odd. You probably mean

括号的使用是奇数。你的意思是

const char *a[2] = {"blah", "hmm"};

which declares an array of two pointers to constant characters, and initializes them to point at two hardcoded string constants.

它向常量字符声明两个指针的数组,并将它们初始化为指向两个硬编码的字符串常量。

#8


5  

If the strings are static, you're best off with:

如果字符串是静态的,那么最好是:

const char *my_array[] = {"eenie","meenie","miney"};

While not part of basic ANSI C, chances are your environment supports the syntax. These strings are immutable (read-only), and thus in many environments use less overhead than dynamically building a string array.

虽然不是基本的ANSI C的一部分,但是您的环境很可能支持语法。这些字符串是不可变的(只读),因此在许多环境中,使用的开销比动态构建字符串数组要少。

For example in small micro-controller projects, this syntax uses program memory rather than (usually) more precious ram memory. AVR-C is an example environment supporting this syntax, but so do most of the other ones.

例如,在小型微控制器项目中,这种语法使用程序内存,而不是(通常)更宝贵的ram内存。AVR-C是支持这种语法的一个示例环境,但是其他大多数都是这样。

#9


5  

If you don't want to keep track of number of strings in array and want to iterate over them, just add NULL string in the end:

如果您不想跟踪数组中的字符串数量,并希望对它们进行迭代,那么只需在末尾添加空字符串:

char *strings[]={ "one", "two", "three", NULL };

int i=0;
while(strings[i]) {
  printf("%s\n", strings[i]);
  //do something
  i++;
};

#10


3  

Your code is creating an array of function pointers. Try

您的代码正在创建一个函数指针数组。试一试

char* a[size];

or

char a[size1][size2];

instead.

代替。

See wikibooks to arrays and pointers

参见*到数组和指针

#11


0  

char name[10][10]
int i,j,n;//here "n" is number of enteries
printf("\nEnter size of array = ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
    for(j=0;j<1;j++)
    {
        printf("\nEnter name = ");
        scanf("%s",&name[i]);
    }
}
//printing the data
for(i=0;i<n;i++)
{
    for(j=0;j<1;j++)
    {
        printf("%d\t|\t%s\t|\t%s",rollno[i][j],name[i],sex[i]);
    }
    printf("\n");
}

Here try this!!!

在这里试试这个! ! !

#12


0  

I was missing somehow more dynamic array of strings, where amount of strings could be varied depending on run-time selection, but otherwise strings should be fixed.

我缺少了更动态的字符串数组,其中字符串的数量可以根据运行时的选择而变化,但是其他字符串应该是固定的。

I've ended up of coding code snippet like this:

我最后编写了这样的代码片段:

#define INIT_STRING_ARRAY(...)          \
    {                                   \
        char* args[] = __VA_ARGS__;     \
        ev = args;                      \
        count = _countof(args);         \
    }

void InitEnumIfAny(String& key, CMFCPropertyGridProperty* item)
{
    USES_CONVERSION;
    char** ev = nullptr;
    int count = 0;

    if( key.Compare("horizontal_alignment") )
        INIT_STRING_ARRAY( { "top", "bottom" } )

    if (key.Compare("boolean"))
        INIT_STRING_ARRAY( { "yes", "no" } )

    if( ev == nullptr )
        return;

    for( int i = 0; i < count; i++)
        item->AddOption(A2T(ev[i]));

    item->AllowEdit(FALSE);
}

char** ev picks up pointer to array strings, and count picks up amount of strings using _countof function. (Similar to sizeof(arr) / sizeof(arr[0])).

char** ev获取指向数组字符串的指针,计数使用_countof函数获取字符串数量。(类似于sizeof(arr) / sizeof(arr[0]))

And there is extra Ansi to unicode conversion using A2T macro, but that might be optional for your case.

使用A2T宏可以进行额外的Ansi到unicode转换,但这对于您的情况来说可能是可选的。

#13


0  

hello you can try this bellow :

大家可以试试下面这个:

 char arr[nb_of_string][max_string_length]; 
 strcpy(arr[0], "word");

a nice example of using, array of strings in c if you want it

一个使用字符串数组的很好的例子

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


int main(int argc, char *argv[]){

int i, j, k;

// to set you array
//const arr[nb_of_string][max_string_length]
char array[3][100];

char temp[100];
char word[100];

for (i = 0; i < 3; i++){
    printf("type word %d : ",i+1);
    scanf("%s", word);
    strcpy(array[i], word);
}

for (k=0; k<3-1; k++){
    for (i=0; i<3-1; i++)
    {
        for (j=0; j<strlen(array[i]); j++)
        {
            // if a letter ascii code is bigger we swap values
            if (array[i][j] > array[i+1][j])
            {
                strcpy(temp, array[i+1]);
                strcpy(array[i+1], array[i]);
                strcpy(array[i], temp);

                j = 999;
            }

            // if a letter ascii code is smaller we stop
            if (array[i][j] < array[i+1][j])
            {
                    j = 999;
            }

        }
    }
}

for (i=0; i<3; i++)
{
    printf("%s\n",array[i]);
}

return 0;
}

#14


-6  

A good way is to define a string your self.

一个好的方法是定义一个字符串你自己。

#include <stdio.h>
typedef char string[]
int main() {
    string test = "string";
    return 0;
}

It's really that simple.

真的那么简单。