对指针感到困惑吗?

发布于 2024-11-08 17:38:04 字数 653 浏览 0 评论 0原文

我正在尝试将响应写入变量,但我不知道该怎么做。

这不起作用 - 搞砸了内存,但没有保护错误:

for (int i = 0; i < 20; i++) {
    list[i] = 'a';
}

与此相同 - 内存搞砸了:

for (int i = 0; i < 20; i++) {
    *(((int*)(list))+i) = 'a';
}
//I don't think this is a string issues as this doesn't help:
//*(((int*)(list))+20) = '\0';

这会导致总线错误:

for (int i = 0; i < 20; i++) {
    *list[i] = 'a';
}

这按预期工作:

*list = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

我做错了什么?

PS listchar**

I'm trying to write a response into a variable, and I can't figure out how to do it.

This doesn't work - screws up memory, but no protection errors:

for (int i = 0; i < 20; i++) {
    list[i] = 'a';
}

Same with this - memory screwed up:

for (int i = 0; i < 20; i++) {
    *(((int*)(list))+i) = 'a';
}
//I don't think this is a string issues as this doesn't help:
//*(((int*)(list))+20) = '\0';

This causes a bus error:

for (int i = 0; i < 20; i++) {
    *list[i] = 'a';
}

This works as desired:

*list = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

What am I doing wrong?

P.S. list is char**.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

治碍 2024-11-15 17:38:04

在C中,指针可以用来表示数组,单个字符串就是一个char数组,或者换句话说,一个char *。这意味着 char ** 是一个字符串数组。因此,如果您想将字符放入第一个字符串中(假设内存已经分配给它),您应该在第一个循环中使用 list[0][i] = 'a'; -即,将 'a' 放入第 0 字符串的 i 位置。

解释 char ** 的另一种方法(我怀疑你应该使用的就是它)是一个指针,它指向一个指向 < 数组的指针。代码>字符。在这种情况下,您可以使用“外部”指针来修改内部指针所指向的内容;这可以用来首先分配字符串,然后写入它:

*list = malloc(21); // Allocate 21 bytes and make the pointer that 'list' points to refer to that memory
for (int i = 0; i < 20; i++) {
    (*list)[i] = 'a';
}
(*list)[20] = '\0'; // Also, you need the null terminator at the end of the string

在内存中,这看起来像这样:

list ---> (another pointer) ---> |a|a|a|a|a|a|...|a|a|\0|

In C, a pointer can be used to represent an array, and a single string is an array of char, or in other words, a char *. This means that a char ** is an array of strings. So if you want to put characters into the first string (assuming that memory has already been allocated to it), you should use list[0][i] = 'a'; in the first loop - i.e., put 'a' into position i of the 0th string.

Another way of interpreting a char ** (which is the one I suspect is the one you're supposed to use) is that it is a pointer that points to a pointer that points to an array of char. In that case, you can use the "outer" pointer to modify what the inner pointer points to; this can be used to first allocate the string and then write to it:

*list = malloc(21); // Allocate 21 bytes and make the pointer that 'list' points to refer to that memory
for (int i = 0; i < 20; i++) {
    (*list)[i] = 'a';
}
(*list)[20] = '\0'; // Also, you need the null terminator at the end of the string

In memory, this looks like this:

list ---> (another pointer) ---> |a|a|a|a|a|a|...|a|a|\0|
九八野马 2024-11-15 17:38:04

由于您想要一个字节数组,因此 char** 是错误的 - 这是一个指向 char 的指针。您需要 char* 作为数组,但如果它是固定长度,我会将其声明为 char list[20]

看起来你想写这样的东西:

char list[20];
for (int i = 0; i < 20; i++) {
    list[i] = 'a';
}

或者如果你想使用堆分配

char *list = malloc(20);

因为我们只能猜测,我认为你的意思是说 *list 是一个字节数组。在这种情况下,代码将如下所示:

char **list = get_list_from_somewhere();
*list = malloc(20);
for (int i = 0; i < 20; i++) {
    *list[i] = 'a';
}

Since you want an array of bytes then char** is wrong – that's a pointer to a pointer to char. You want char* for an array, but if it's fixed length I'd declare it as char list[20].

It seems like you want to write something like this:

char list[20];
for (int i = 0; i < 20; i++) {
    list[i] = 'a';
}

Or if you want heap allocation use

char *list = malloc(20);

Since we are reduced to guessing, I think that you meant to say that *list is an array of bytes. In which case the code would be like so:

char **list = get_list_from_somewhere();
*list = malloc(20);
for (int i = 0; i < 20; i++) {
    *list[i] = 'a';
}
桃酥萝莉 2024-11-15 17:38:04
char** list

不是“字节数组”,而是“指向指向字符的指针的指针”,您可以将其视为“字符列表的列表”。另外,如果您以这种方式定义它,则需要分配足够的内存来保存数据。

当您编写时:

list[i] = 'a';

它会弄乱内存,因为您将字符“a”放置在指定用于保存指针的位置。实际上,在大多数编译器中,字符文字都是 int 类型,因此您实际上将 int 形式的“a”存储为指向内存位置的指针,这可能会导致各种内存损坏。

如果您希望“list”位于堆栈中,则将其定义为:

char list[20]

如果您希望“list”位于堆中,则将其定义为:

char* list;
list = malloc(sizeof(char) * 20);

无论哪种情况,都将其访问为:

list[i]

假定 int 的大小也是不安全的相当于您在第二个示例中所做的指针的大小。至少我认为这就是你想要做的。

此外,如果您存储来自数据流或类似内容的原始字节,您可能应该使用“unsigned char”而不是“char”,并且为了更安全,请使用“int8_t”,因为您不能总是保证“char”是8位尽管它存在于大多数平台上。

char** list

Isn't a 'array of bytes' its a 'pointer to a pointer pointing to char' which you can think of as a 'list of lists of char'. Also if you define it this way, you need to malloc enough memory to hold the data.

When you write:

list[i] = 'a';

Its messing up memory because your placing a char 'a' in a location specified to hold a pointer. Actually in most compilers character literals are of type int so your actually storing an int form of 'a' as a pointer to a memory location which can cause all sorts of memory corruption.

If you want 'list' to be on the stack then define it as:

char list[20]

If you want 'list' to be in the heap then define it as:

char* list;
list = malloc(sizeof(char) * 20);

In either case access it as:

list[i]

Also its not safe to assume the size of int being equivalent to the size of a pointer as you to have done in your second example. At least i think thats what you were trying to do.

Additionally if your storing raw bytes from a data stream or something of that sort you should likely be using 'unsigned char' not 'char' and for more safety use 'int8_t' as you can't always guarantee that 'char' is 8 bits although it is on the majority of platforms.

三人与歌 2024-11-15 17:38:04

您是如何初始化您的列表的?

char** list;

在这种情况下不起作用,您必须拥有类似

char* list[20];

或动态分配的东西。

How did you initialize your list?

char** list;

doesn't work in this case you'll have to have something like

char* list[20];

or dynamically allocated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文