- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
传入的两个数组是常量,所以我新建了两个数组。第一个数组存储一组字符,第二个数组存储第二组字符。到目前为止,我假设第一组比第二组大。 (a、b、c、d > x、y)。该程序希望完成的是创建两个包含相同字母但较短的数组的新数组,在这种情况下 arr2 (newarr2) 重复最后一个字符,直到它与第一个数组的长度匹配。正确解决方案的例子。(a,b,c,d < x,y) --> equate_arr --> (a,b,c,d = x,y,y,y)
void equate_arr(char arg2[], char arg1[]){
size_t i = 0;
size_t len1 = strlen(arg1);
size_t len2 = strlen(arg2);
char newarr2[512];
char newarr1[512];
while(i < (strlen2 - 1))
{
newarr2[i] = arg2[i];
i++;
}
i = 0;
while(i < (strlen1 - 1))
{
newarr1[i] = arg1[i];
i++;
}
i = 0;
while(strlen(newarr2) < strlen(newarr1))
{
newarr2[strlen(newarr2)] = newarr2[strlen(newarr2)-1]
}
}
目前我不知道发生了什么,因为一旦我在我的代码中修改了这个函数,程序似乎就不再运行了。很抱歉询问我正在做的这个项目,但我确实需要一些帮助。
如果需要,我可以把整个程序放在这里。
已修改
void tr_non_eq(char arg1[], char arg2[], int len1, int len2)
{
int i = 0;
char* arr2;
arr2 = (char*)calloc(len1+1,sizeof(char));
while(i < len2)
{
arr2[i] = arg2[i];
i++;
}
while(len2 < len1)
{
arr2[len2] = arg2[len2-1];
len2++;
}
tr_str(arg1, arr2);
}
现在使用输入 (a,b,c,d,e,f) 和 (x,y) 和一个字符串“cabbage”来翻译程序打印出“yxyyx”并打印出字符串“abcdef” “xyy”表示 promise 。我不太清楚为什么 arr2 数组没有按预期填充“y”字符。
最佳答案
正如 de-duplicator 所说,就您的代码而言,它实际上什么也做不了。更重要的是,它试图做的事情充满了危险。
您使用 strlen
的事实确定你的论点的长度是一个明确的指标 equate_arr
不希望收到两个 char
数组.相反,它需要两个以 NUL 结尾的 C 风格字符串。所以声明应该更像是:
void equate_arr(const char *arg2, const char *arg1)
这让合约更清晰一些。
但请注意返回类型:void
.这表示您的函数不会向调用者返回任何值。那么,您打算如何返回修改后的数组?
下一个大危险在于这些行:
char newarr2[512];
char newarr1[512];
如果使用大于 511 个字符(加上 NUL)的字符串调用此函数,会发生什么情况? “缓冲区溢出”这个短语应该在这里跳出来。
你需要的是malloc
缓冲区大到足以容纳传入的最长字符串的副本。但这提出了一个问题,即如何将新数组交还给调用者(还记得 void
返回类型吗?)。
这里还有许多其他问题,主要归结为没有明确定义此功能要满足的契约(Contract)。
在我仔细观察的时候再看一个
while(strlen(newarr2) < strlen(newarr1))
{
newarr2[strlen(newarr2)] = newarr2[strlen(newarr2)-1]
}
第一次通过此循环会覆盖 newarr2
中的终止 NUL ,这意味着下一次调用 strlen
陷入未定义的行为,因为它完全受堆栈中任何垃圾的支配。
如果您不清楚 C 风格的字符串,请查看我对 this 的回答详细介绍了它们的问题。
以下是白板代码(即未编译、未测试),它可以完成您想要实现的目标。仅供引用
// Pad a string so that it is the same length as another. Padding is done
// by replicating the final character.
//
// @param padThis: A C-style string in a non-constant buffer.
// @param bufLength: The size of the buffer containing padThis
// @param toMatchThis: A (possibly) const C-style string to act
// as a template for length
//
// Pre-conditions:
// - Both padThis and toMatchThis reference NUL-terminated sequences
// of chars
// - strlen(padThis) < bufLength. Violating this will exit the program.
// - strlen(toMatchThis) < bufLength. If not, padThis will be padded
// to bufLength characters.
//
// Post-conditons:
// - The string referenced by toMatchThis is unchanged
// - The original string at padThis has been padded if necessary to
// min(bufLength, strlen(toMatchThis))
void padString(char * padThis, size_t bufLength, const char * toMatchThis)
{
size_t targetLength = strlen(toMatchThis);
size_t originalLength = strlen(padThis);
if (originalLength >= bufLength)
{
fprintf(stderr, "padString called with an original which is longer than the buffer!\n");
exit(EXIT_FAILURE);
}
if (targetLength >= bufLength)
targetLength = bufLength -1; // Just pad until buffer full
if (targetLength <= strlen(padThis))
return; // Nothing to do
// At this point, we know that some padding needs to occur, and
// that the buffer is large enough (assuming the caller is not
// lying to us).
char padChar = padThis[originalLength-1];
size_t index = originalLength;
while (index < targetLength)
padThis[index++] = padChar;
padThis[index] = '\0';
}
关于c - 使两个数组长度相同。中(89),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28388395/
将 KLV 字符串拆分为键、长度、值作为元素的列表/元组的更有效方法是什么? 为了添加一点背景,前 3 位数字作为键,接下来的 2 位表示值的长度。 我已经能够使用以下代码解决该问题。但我不认为我的代
首先,我试图从文件中提取视频持续时间,然后在无需实际上传文件的情况下显示它。 当用户选择视频时 - 信息将显示在其下方,包括文件名、文件大小、文件类型。不管我的技能多么糟糕 - 我无法显示持续时间。我
我是 Scala 编程新手,这是我的问题:如何计算每行的字符串数量?我的数据框由一列 Array[String] 类型组成。 friendsDF: org.apache.spark.sql.DataF
我有一个React Web应用程序(create-react-app),该应用程序使用react-hook-forms上传歌曲并使用axios将其发送到我的Node / express服务器。 我想确
如果给你一个网络掩码(例如 255.255.255.0),你如何在 Java 中获得它的长度/位(例如 8)? 最佳答案 如果您想找出整数低端有多少个零位,请尝试 Integer.numberOfTr
我需要使用 jQuery 获取 div 数量的长度。 我可以得到它,但在两个单击事件中声明变量,但这似乎是错误的,然后我还需要使用它来根据数字显示隐藏按钮。我觉得我不必将代码加倍。 在这里摆弄 htt
我对此感到非常绝望,到目前为止我在 www 上找不到任何东西。 情况如下: 我正在使用 Python。 我有 3 个数组:x 坐标、y 坐标和半径。 我想使用给定的 x 和 y 坐标创建散点图。 到目
我有一个表单,我通过 jQuery 的加载函数动态添加新的输入和选择元素。有时加载的元素故意为空,在这种情况下我想隐藏容器 div,这样它就不会破坏样式。 问题是,我似乎无法计算加载的元素,因此不知道
我决定通过替换来使我的代码更清晰 if (wrappedSet.length > 0) 类似 if (wrappedSet.exists()) 是否有任何 native jq 函数可以实现此目的?或者
简单的问题。如果我有一个如下表: CREATE TABLE `exampletable` ( `id` int(11) NOT NULL AUTO_INCREMENT, `textfield`
我正在使用经典 ASP/MySQL 将长用户输入插入到我的数据库中,该输入是从富文本编辑器生成的。该列设置为 LONG-TEXT。 作为参数化查询(准备语句)的新手,我不确定用于此特定查询的数据长度。
我正在获取 Stripe 交易费用的值(value)并通过禁用的文本字段显示它。 由于输入文本域,句子出现较大空隙 This is the amount $3.50____________that n
我有一个 div,其背景图像的大小设置为包含。但是,图像是视网膜计算机(Macbook Pro 等)的双分辨率图像,所以我希望能够以某种方式让页面知道即使我说的是背景大小:包含 200x200 图像,
我正在开发一个具有“已保存”和“已完成”模块的小部件。当我删除元素时,它会从 dom 中删除/淡化它,但是当我将其标记为完成时,它会将其克隆到已完成的选项卡。这工作很棒,但顶部括号内的数字不适合我。这
我有一个来自 json 提要的数组,我知道在 jArray 中有一个联盟,但我需要计算出该数组的计数,以防稍后将第二个添加到提要中。目前 log cat 没有注销“teamFeedStructure”
目标:给定一个混合类型的数组,确定每个级别的元素数量。如果同一层有两个子数组,则它们的每个元素都计入该层元素的总数。 方法: Array.prototype.elementsAtLevels = fu
我需要帮助为 Java 中的单链表制作 int size(); 方法。 这是我目前所拥有的,但它没有返回正确的列表大小。 public int size() { int size = 0;
我正在为学校作业创建一个文件服务器应用程序。我目前拥有的是一个简单的 Client 类,它通过 TCP 发送图像,还有一个 Server 类接收图像并将其写入文件。 这是我的客户端代码 import
我有这对功能 (,) length :: Foldable t => t a -> b -> (Int, b) 和, head :: [a] -> a 我想了解的类型 (,) length he
我正在GitHub Pages上使用Jekyll来构建博客,并希望获得传递给YAML前题中Liquid模板的page.title字符串的长度,该字符串在每个帖子的YAML主题中。我还没有找到一种简单的
我是一名优秀的程序员,十分优秀!