如何在c中的数组中存储字符串?

如何在c中的数组中存储字符串?,c,arrays,cs50,C,Arrays,Cs50,我尝试使用strlen来计算字符数,并将变量n命名为n,并创建了一个名为[n+1]的数组,但变量n不是全局变量,因此我遇到了一些问题,因为计算机不理解n是什么。为了计算n,我创建了另一个函数 #include <cs50.h> #include <stdio.h> #include <math.h> #include <ctype.h> #include <string.h> int count_characters(string t

我尝试使用strlen来计算字符数,并将变量n命名为n,并创建了一个名为[n+1]的数组,但变量n不是全局变量,因此我遇到了一些问题,因为计算机不理解n是什么。为了计算n,我创建了另一个函数

#include <cs50.h>
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <string.h>

int count_characters(string text);
int n;
int main(void)
charcters [n+1]
{
 string t = get_string("text: ");
 printf("letter(s)");

}

int count_characters(string text)
{
 n = strlen(text);
 return n;
}
#包括
#包括
#包括
#包括
#包括
整数计数字符(字符串文本);
int n;
内部主(空)
字符[n+1]
{
字符串t=获取字符串(“文本:”);
printf(“信函”);
}
整数计数字符(字符串文本)
{
n=strlen(文本);
返回n;
}

您在
n
中使用的值必须在分配给
n
之后

您想要的可能是:

#include <cs50.h>
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <string.h>

int count_characters(string text);
int n;
int main(void)
{
 string t = get_string("text: ");
 printf("letter(s)");
 /* call the function to have it assign to n */
 count_characters(t);
 /* now the length is assigned to n, so use it */
 /* also don't forget the type name for elements */
 char charcters [n+1];
 /* store a string */
 strcpy(charcters, t);

}

int count_characters(string text)
{
 n = strlen(text);
 return n;
}
#包括
#包括
#包括
#包括
#包括
整数计数字符(字符串文本);
int n;
内部主(空)
{
字符串t=获取字符串(“文本:”);
printf(“信函”);
/*调用函数将其分配给n*/
计数字符(t);
/*现在长度被指定为n,所以使用它*/
/*也不要忘记元素的类型名*/
字符[n+1];
/*存储字符串*/
strcpy(characters,t);
}
整数计数字符(字符串文本)
{
n=strlen(文本);
返回n;
}

您发布的代码是否编译?在我看来似乎不是。