小编use*_*690的帖子

如何在C中创建固定长度的"字符串"数组?

我试图在C中创建一个固定长度的"字符串"数组,但一直有点麻烦.我遇到的问题是我遇到了分段错误.

这是我的程序的目标:我想使用从文本文件读取的数据按索引设置数组的字符串.这是我当前代码的要点(我道歉,我无法添加我的整个代码,但它非常冗长,可能只会引起混淆):

//"n" is set at run time, and 256 is the length I would like the individual strings to be
char (*stringArray[n])[256];
char currentString[256];

//"inputFile" is a pointer to a FILE object (a .txt file)
fread(&currentString, 256, 1, inputFile);
//I would like to set the string at index 0 to the data that was just read in from the inputFile
strcpy(stringArray[i], &currentString);
Run Code Online (Sandbox Code Playgroud)

c arrays string char low-level

6
推荐指数
1
解决办法
4155
查看次数

带平方根的while循环的运行时间/时间复杂度

这个问题看起来比较简单,但是我好像找不到n的运行时间。

这是问题所在:

j = n;
while(j >= 2) {
    j = j^(1/2)
}
Run Code Online (Sandbox Code Playgroud)

我真的不需要总运行时间,我只需要知道如何计算第二行和第三行被击中的次数(它们应该相同)。我也想知道是否有某种公式可以找到这个。我可以看到上面的内容相当于:

for(j = n; n >= 2; j = j^(1/2)
Run Code Online (Sandbox Code Playgroud)

请注意,操作的类型无关紧要,每次执行一行,都计为 1 个时间单位。所以第 1 行只是 1 个时间单位,第 2 行将是:

  • 如果 n 为 1,则为 0 个时间单位,
  • 如果 n 为 2,则为 1 个时间单位,
  • 如果 n 为 4,则为 2 个时间单位,
  • 如果 n 为 16,则为 3 个时间单位,依此类推。

在此先感谢任何提供帮助的人!这是非常赞赏!

performance code-analysis time-complexity

3
推荐指数
1
解决办法
1570
查看次数