- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
阅读:
以及与此问题相关的其他问题和万维网上的其他内容,所以请继续阅读...
很快,我想,我从来没有alloc
像char *
这样的内存,但我认为它可能有效:
void _alloc(char *line)
*line = malloc(sizeof(char) * BUFSIZE);
忽略 _alloc
和 main
中的错误处理程序。最主要的是 SEGFAULT
这样的部分:
main()
{
char *text, *start;
_alloc(text);
start = text;
// add contents in text briefly
*text++ = 'd'; *text = '\0'; // SEGFAULT
fprintf(stdout, "%s", start);
}
我有一张 _alloc 函数的图片是这样工作的:
main()
{
char *text;
/// function _alloc:
char *tmp = text;
*tmp = malloc ( sizeof(char) * BUFSIZE);
/// end
}
还有……当我尝试这个时,它给了我警告:赋值从没有转换和 SEGF 的指针生成整数。我的照片是:
tmp = text; // tmp point at address of line
*tmp = malloc(...); // tmp change to what point line...
而且我看到我需要 _alloc(char **) ,但不知道它是如何与 ** 一起工作的。我试着用 array[][] 来操作,我知道。但是无法获取图片,为什么它需要是char **?以下是部分答案:
Here,
line
is a local variable within_alloc
. Pointers are passed by value in C, so a receives a copy of the pointer in main when you do_alloc(text);
< answer user nos (modified with my name of variables) on Scope of malloc used in a function
附言我知道写这样的函数更简单 char *_alloc();
最佳答案
让我们举一个简单的例子,用一个整数。
假设我们有以下功能:
void foo(int n)
{
n = 3;
}
int main(void)
{
int number = 5;
foo(number);
}
我想你会同意 number
在 foo(number)
之后没有被修改。这是因为 n
只是 number
的一个副本,它是一个局部变量,在 foo
退出后被销毁。
这就是我们使用指针的原因。但是指针是变量,两者是完全一样的东西。看这个例子:
void bar(int *n)
{
*n = 3;
int *temp = malloc(sizeof(int));
*temp = 6;
n = temp;
}
int main(void)
{
int number = 5;
int *number_p = &number;
bar(number_p);
}
这里,number = 3
在 bar(number_p)
之后,因为我们将整数的地址传递给 bar
。但是,地址本身是原始指针的副本。因此,指令 n = temp;
不会对 number_p
做任何事情,因为 n 只是一个局部变量。
这就是为什么我们需要使用指向指针的指针。
通过以下示例,您可以修改 foo
函数内部的原始指针:
void foo(int **n)
{
**n = 3;
int *temp = malloc(sizeof(int));
*temp = 6;
*n = temp;
}
int main(void)
{
int number = 5;
int *number_p = &number;
foo(&number_p); //We pass the adress of the pointer number_p
}
在foo(&number_p)
之后,number
是3,number_p
是指向temp
的指针,因为我们能够在 foo
中修改地址本身。
在你的例子中,你需要修改_alloc
函数中的指针,所以签名应该是
void _alloc(char **line)
指针应该修改为
*line = malloc(...);
此外,_alloc
应该这样调用
char* s;
_alloc(&s);
对不起,我的英语不好,为了清楚起见,我已经尽力了。
关于c - 在函数中使用 char **,我们可以为传递给她的 char * 分配内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28564847/
这个问题在这里已经有了答案: Why don't Java's +=, -=, *=, /= compound assignment operators require casting? (11 个
当我尝试运行以下代码时,List(.of) 无法编译并给出主题错误。 package collections; import java.util.LinkedHashSet; import java.
我正在尝试编译使用 ChatScript 库的程序。这是我在名为 main.cpp 的文件中的代码: #include #include "common.h" using namespace std
我想在我的程序中外部使用 ChatScript。在documents它说: Embedding Step #1 First, you will need to modify `common.h and
假设我有一个 char,我想用一行代码将其 strcat() 转换为 char 数组。对于 [一个非实用的] 示例: strcat("ljsdflusdfg",getchar()); 或者我想做相反的
我有以下类型签名: *Main Lib> let f :: a -> a -> a -> a; f = undefined *Main Lib> let x :: Char; x = undefin
我正在学习如何在 C 中使用指针(使用 malloc 和 free),但我在这个练习中遇到了一些麻烦。我只想制作一个指针数组,我想在其中保存每个单词的方向。然后我想为一个特定的词做一个 free(),
我有一个字符*: char* version = "10.5.108"; 我想通过字符分隔符获取两个新的 char*。 char delimiter = '.'; 执行以下代码后: printf("|
最近在学习Cpp,今天在学习使用Clion做测试的时候,发生了奇怪的事情。 这是我的代码 int main() { char c = 'b'; char carr[1]{'a'};
我对 c 很陌生,我正在审查一些代码。我遇到了这个: static char * fromDataType; static char * toDataType; static char * fromR
我有一个像这样的动态结构: struct network { int count; char** ips; } 如果我知道每个字符串数组都是 16 个字节(即 INET_ADDRSTR
我有一个旧程序,其中使用了一些库函数,但我没有那个库。 所以我正在使用 C++ 库编写该程序。在那个旧代码中有一些函数是这样调用的 *string = newstrdup("这里有一些字符串"); 字
我正在编写一个函数,该函数接受 ArrayList,然后将每个 char[] 复制到另一个增加长度的 char[] 中,然后将新的 char[] 添加到新的 ArrayList 中。当我尝试复制数组时
我正在寻找 map >并生成每个可能的 map从它。 我知道这可能会占用大量内存并需要一些时间。 每个map需要包含每个字母 a-z,并映射到唯一的 a-z 字符。 IE。啊bjcp迪EVfh嘎血红蛋
#define NAME_LEN 20 #include "stdio.h" #include "stdlib.h" #include "string.h" #pragma warning(disab
所以我必须创建一个函数来找到一对带有第一个字母并返回第二个字母的函数。 我实际上找到了一个答案,但是使用 map 功能却找不到。 lookUp :: Char -> [(Char, Cha
我最近接受采访并要求写mystrcat(*s1, *s2, *s3) 其中s1 和s2 是源字符串连接结果由 s3 给出。有人告诉我,不要担心 s3 的内存分配,并假设 s1 和 s2 不是空/无效字
今天我与一位同事讨论了他(对我来说)不寻常的“main”函数签名。他喜欢这样声明: int main(int argc, char* (*argv)[]) { printf("at index
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: What's the difference between new char[10] and new cha
通常字符串文字是 const char[] 类型。但是当我把它当作其他类型时,我得到了奇怪的结果。 unsigned char *a = "\355\1\23"; 使用此编译器会抛出警告“初始化中的指
我是一名优秀的程序员,十分优秀!