我试图在c中创建一个字符串数组,如果我使用以下代码:
char (*a[2])[14];
a[0]="blah";
a[1]="hmm";
GCC给我“警告:来自不兼容的指针类型的赋值”。正确的做法是什么?
编辑:我很好奇为什么这应该给编译器警告,因为如果我做printf([1]);,它正确地打印“嗯”。
我试图在c中创建一个字符串数组,如果我使用以下代码:
char (*a[2])[14];
a[0]="blah";
a[1]="hmm";
GCC给我“警告:来自不兼容的指针类型的赋值”。正确的做法是什么?
编辑:我很好奇为什么这应该给编译器警告,因为如果我做printf([1]);,它正确地打印“嗯”。
当前回答
在c语言中有几种方法来创建一个字符串数组。如果所有的字符串都是相同的长度(或者至少有相同的最大长度),你只需声明一个2-d的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");
你也可以添加一个初始化式列表:
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.
你可以存储一个指向char的1维数组,而不是使用char的2维数组:
char *strs[NUMBER_OF_STRINGS];
注意,在这种情况下,您只分配了内存来保存指向字符串的指针;字符串本身的内存必须分配到其他地方(作为静态数组或使用malloc()或calloc())。你可以像前面的例子一样使用初始化列表:
char *strs[NUMBER_OF_STRINGS] = {"foo", "bar", "bletch", ...};
不是复制字符串常量的内容,而是简单地存储指向它们的指针。注意,字符串常量可能不可写;你可以像这样重新分配指针:
strs[i] = "bar";
strs[i] = "foo";
但是你可能不能改变字符串的内容;也就是说,
strs[i] = "bar";
strcpy(strs[i], "foo");
可能不被允许。
你可以使用malloc()为每个字符串动态分配缓冲区,并复制到该缓冲区:
strs[i] = malloc(strlen("foo") + 1);
strcpy(strs[i], "foo");
顺便说一句,
char (*a[2])[14];
将a声明为指向14元素char数组的指针的2元素数组。
其他回答
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");
}
来试试这个!!
在ANSI C中:
char* strings[3];
strings[0] = "foo";
strings[1] = "bar";
strings[2] = "baz";
或者你可以声明一个结构类型,它包含一个字符数组(1个字符串),它们创建一个结构数组,因此是一个多元素数组
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
}
与其他方法相比,这种方法的优点之一是允许你直接扫描到字符串,而不必使用strcpy;
我在某种程度上缺少了更动态的字符串数组,其中字符串的数量可以根据运行时的选择而变化,但字符串应该是固定的。
我最后的代码片段是这样的:
#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获取数组字符串的指针,count使用_countof函数获取字符串的数量。(类似sizeof(arr) / sizeof(arr[0]))。
还有一个额外的Ansi到unicode的转换使用A2T宏,但对于你的情况可能是可选的。
你好,你可以试试这个:
char arr[nb_of_string][max_string_length];
strcpy(arr[0], "word");
这是一个很好的例子,在c语言中使用字符串数组
#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;
}