- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在处理的函数需要两个字符串作为输入(以及一些其他参数),并在输出时返回两个不同的字符串。我认为最合乎逻辑的方法是为错误返回一个 int
并将两个指向 C 字符串的指针作为参数。我就是这样做的:
int annealc(const char *sequence1, const char *sequence2, float startTemp, float endTemp, float tempStep, int cycles, char *retVal1, char *retVal2, int *score)
{
...
retVal1 = malloc(MAX(len1, len2));
retVal2 = malloc(MAX(len1, len2));
...
strcpy(retVal1, sequence1);
strcpy(retVal2, sequence2);
...
//Somewhere along the line retVal is exchanged for another string
free(retVal1);
free(retVal2);
retVal1 = currentSeq1;
retVal2 = currentSeq2;
...
*score = bestScore;
return 0;
}
//Intended use:
int error = annealc(content1, content2,
30, 5, 1, 5,
alignment1, alignment2, &score);
我认为我没有正确使用指针的语法,因为当函数返回时,alignment1
或 alignment2
中都没有值。那么,我做错了什么?
感谢您的帮助!
最佳答案
您是对的,您没有使用正确的参数类型。您想要在函数中设置的指针,并希望在返回后具有值,将其类型化为指向指针的指针。
类似的东西。
int annealc
(
const char *sequence1,
const char *sequence2,
float startTemp,
float endTemp,
float tempStep,
int cycles,
char **retVal1,
char **retVal2,
int *score
)
{
...
*retVal1 = malloc(MAX(len1, len2));
*retVal2 = malloc(MAX(len1, len2));
...
strcpy(*retVal1, sequence1);
strcpy(*retVal2, sequence2);
...
//Somewhere along the line retVal is exchanged for another string
free(*retVal1);
free(*retVal2);
*retVal1 = currentSeq1;
*retVal2 = currentSeq2;
...
*score = bestScore;
return 0;
}
//Intended use:
int error = annealc(content1, content2,
30, 5, 1, 5,
&alignment1, &alignment2, &score);
原因是在您的版本中,参数 retVal1 和 retVal2 只是键入为 char 指针。这意味着这些变量只保存一个地址值,可以通过使用 *retVal1 或 *retVal2 来取消引用以检查或修改该地址处的字符。
当您将它们键入为指向指针的指针时,它们现在持有对某个其他指针地址的引用。取消引用指向指针的指针允许您检查或修改其他指针引用的地址。
下面的例子最能说明这一点:
#include <stdio.h>
#include <string.h>
#include <memory.h>
void sample1(char *inner)
{
printf("IN SAMPLE1 BEFORE MALLOC: Address of inner is %p\n", &inner);
printf("IN SAMPLE1 BEFORE MALLOC: Address held by inner is %p\n", inner);
printf("IN SAMPLE1 BEFORE MALLOC: The char referenced by inner is %c\n", *inner);
inner = (char *)malloc(4);
strcpy(inner, "WXYZ");
printf("IN SAMPLE1 AFTER MALLOC: Address of inner is %p\n", &inner);
printf("IN SAMPLE1 AFTER MALLOC: Address held by inner is %p\n", inner);
printf("IN SAMPLE1 AFTER MALLOC: The char referenced by inner is %c\n\n", *inner);
}
void sample2(char **inner)
{
printf("IN SAMPLE2 BEFORE MALLOC: Address of inner is %p\n", &inner);
printf("IN SAMPLE2 BEFORE MALLOC: Address of the pointer held by inner is %p\n", inner);
printf("IN SAMPLE2 BEFORE MALLOC: Address of the char referenced by the address held by inner is %p\n", *inner);
printf("IN SAMPLE2 BEFORE MALLOC: The char referenced by the address held by inner is %c\n\n", **inner);
*inner = (char *)malloc(sizeof(char));
**inner = 'W';
printf("IN SAMPLE2 AFTER MALLOC: Address of inner is %p\n", &inner);
printf("IN SAMPLE2 AFTER MALLOC: Address of the pointer held by inner is %p\n", inner);
printf("IN SAMPLE2 AFTER MALLOC: Address of the char referenced by the address held by inner is %p\n", *inner);
printf("IN SAMPLE2 AFTER MALLOC: The char referenced by the address held by inner is %c\n\n", **inner);
}
int main(int argc, char *argv[])
{
char myChar = 'A';
char *outer = &myChar;
printf("Addres of myChar is %p\n\n", &myChar);
printf("BEFORE sample1: Address of outer is %p\t\t\t\t\t\tThis is where the variable 'outer' resides in memory.\n", &outer);
printf("BEFORE sample1: Address of the char held by outer is %p\t\t\t\tThis matches the address of 'myChar' because that is what we set 'outer' to.\n", outer);
printf("BEFORE sample1: The char referenced by the address held by outer is %c\t\t\tThis is the same as the value held by 'myChar'.\n\n", *outer);
sample1(outer);
printf("AFTER sample1: Address of outer is %p\t\t\t\t\t\tThe variable 'outer' is unaffected by the call to sample1.\n", &outer);
printf("AFTER sample1: Address of the char held by outer is %p\n", outer);
printf("AFTER sample1: The char referenced by outer is %c\n\n\n", *outer);
printf("BEFORE sample2: Address of outer is %p\t\t\t\t\t\tThis is all the same as before sample1 since 'outer' was not affected.\n", &outer);
printf("BEFORE sample2: Address of the char held by outer is %p\n", outer);
printf("BEFORE sample2: The char referenced by the address held by outer is %c.\n\n", *outer);
printf("BEFORE sample2: Address of outer is %p\n", &outer);
printf("BEFORE sample2: Address referenced by outer is %p\n", outer);
printf("BEFORE sample2: The char referenced by the address held by outer is %c\n\n", *outer);
sample2(&outer);
printf("AFTER sample2: Address of outer is %p\t\t\t\t\t\tThe variable 'outer' still resides in the same place in memory; this didn't change.\n", &outer);
printf("AFTER sample2: Address referenced by outer is %p\t\t\t\t\tThe address held by 'outer' now matches what was assigned inside the call to sample2.\n", outer);
printf("AFTER sample2: The char referenced by the address held by outer is %c\t\t\t...as well as now references the character 'W' which was stored in the memory allocated inside the call to sample2.\n", *outer);
return 0;
}
/*
Example Output:
Addres of myChar is 002EFD33
BEFORE sample1: Address of outer is 002EFD24 This is where the variable 'outer' resides in memory.
BEFORE sample1: Address of the char held by outer is 002EFD33 This matches the address of 'myChar' because that is what we set 'outer' to.
BEFORE sample1: The char referenced by the address held by outer is A This is the same as the value held by 'myChar'.
IN SAMPLE1 BEFORE MALLOC: Address of inner is 002EFC50
IN SAMPLE1 BEFORE MALLOC: Address held by inner is 002EFD33
IN SAMPLE1 BEFORE MALLOC: The char referenced by inner is A
IN SAMPLE1 AFTER MALLOC: Address of inner is 002EFC50
IN SAMPLE1 AFTER MALLOC: Address held by inner is 0075B0F8
IN SAMPLE1 AFTER MALLOC: The char referenced by inner is W
AFTER sample1: Address of outer is 002EFD24 The variable 'outer' is unaffected by the call to sample1.
AFTER sample1: Address of the char held by outer is 002EFD33
AFTER sample1: The char referenced by outer is A
BEFORE sample2: Address of outer is 002EFD24 This is all the same as before sample1 since 'outer' was not affected.
BEFORE sample2: Address of the char held by outer is 002EFD33
BEFORE sample2: The char referenced by the address held by outer is A.
BEFORE sample2: Address of outer is 002EFD24
BEFORE sample2: Address referenced by outer is 002EFD33
BEFORE sample2: The char referenced by the address held by outer is A
IN SAMPLE2 BEFORE MALLOC: Address of inner is 002EFC50
IN SAMPLE2 BEFORE MALLOC: Address of the pointer held by inner is 002EFD24
IN SAMPLE2 BEFORE MALLOC: Address of the char referenced by the address held by inner is 002EFD33
IN SAMPLE2 BEFORE MALLOC: The char referenced by the address held by inner is A
IN SAMPLE2 AFTER MALLOC: Address of inner is 002EFC50
IN SAMPLE2 AFTER MALLOC: Address of the pointer held by inner is 002EFD24
IN SAMPLE2 AFTER MALLOC: Address of the char referenced by the address held by inner is 0075B128
IN SAMPLE2 AFTER MALLOC: The char referenced by the address held by inner is W
AFTER sample2: Address of outer is 002EFD24 The variable 'outer' still resides in the same place in memory; this didn't change.
AFTER sample2: Address referenced by outer is 0075B128 The address held by 'outer' now matches what was assigned inside the call to sample2.
AFTER sample2: The char referenced by the address held by outer is W ...as well as now references the character 'W' which was stored in the memory allocated inside the call to sample2.
*/
关于C 字符串函数参数在返回时为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19063330/
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
在编码时,我问了自己这个问题: 这样更快吗: if(false) return true; else return false; 比这个? if(false) return true; return
如何在逻辑条件下进行“返回”? 在这样的情况下这会很有用 checkConfig() || return false; var iNeedThis=doSomething() || return fa
这是我的正则表达式 demo 如问题所述: 如果第一个数字是 1 则返回 1 但如果是 145 则返回 145 但如果是 133 则返回 133 样本数据a: K'8134567 K'81345678
在代码高尔夫问答部分查看谜题和答案时,我遇到了 this solution返回 1 的最长和最晦涩的方法 引用答案, int foo(void) { return! 0; } int bar(
我想在下面返回 JSON。 { "name": "jackie" } postman 给我错误。说明 Unexpected 'n' 这里是 Spring Boot 的新手。 1日龄。有没有正确的方法来
只要“is”返回 True,“==”不应该返回 True 吗? In [101]: np.NAN is np.nan is np.NaN Out[101]: True In [102]: np.NAN
我需要获取所有在 6 号或 7 号房间或根本不在任何房间的学生的详细信息。如果他们在其他房间,简单地说,我不希望有那个记录。 我的架构是: students(roll_no, name,class,.
我有一个表单,我将它发送到 php 以通过 ajax 插入到 mysql 数据库中。一切顺利,php 返回 "true" 值,但在 ajax 中它显示 false 消息。 在这里你可以查看php代码:
我在 Kotlin 中遇到了一个非常奇怪的无法解释的值比较问题,以下代码打印 假 data class Foo ( val a: Byte ) fun main() { val NUM
请注意,这并非特定于 Protractor。问题在于 Angular 2 的内置 Testability service Protractor 碰巧使用。 Protractor 调用 Testabil
在调试窗口中,以下表达式均返回 1。 Application.WorksheetFunction.CountA(Cells(4 + (i - 1) * rows_per_record, 28) & "
我在本地使用 jsonplaceholder ( http://jsonplaceholder.typicode.com/)。我正在通过 extjs rest 代理测试我的 GET 和 POST 调用
这是 Postman 为成功调用我的页面而提供的(修改后的)代码段。 var client = new RestClient("http://sub.example.com/wp-json/wp/v2
这个问题在这里已经有了答案: What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must
我想我对 C 命令行参数有点生疏。我查看了我的一些旧代码,但无论这个版本是什么,都会出现段错误。 运行方式是 ./foo -n num(其中 num 是用户在命令行中输入的数字) 但不知何故它不起作用
我已经编写了一个类来处理命名管道连接,如果我创建了一个实例,关闭它,然后尝试创建另一个实例,调用 CreateFile() 返回 INVALID_HANDLE_VALUE,并且 GetLastErro
即使 is_writable() 返回 true,我也无法写入文件。当然,该文件存在并且显然是可读的。这是代码: $file = "data"; echo file_get_contents($fil
下面代码中的变量 $response 为 NULL,尽管它应该是 SOAP 请求的值。 (潮汐列表)。当我调用 $client->__getLastResponse() 时,我从 SOAP 服务获得了
我一直在网上的不同论坛上搜索答案,但似乎没有与我的情况相符的... 我正在使用 Windows 7,VS2010。 我有一个使用定时器来调用任务栏刷新功能的应用程序。在该任务栏函数中包含对 LoadI
我是一名优秀的程序员,十分优秀!