对指针感到困惑吗?
我正在尝试将响应写入变量,但我不知道该怎么做。
这不起作用 - 搞砸了内存,但没有保护错误:
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 list
是char**
。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在C中,指针可以用来表示数组,单个字符串就是一个
char
数组,或者换句话说,一个char *
。这意味着char **
是一个字符串数组。因此,如果您想将字符放入第一个字符串中(假设内存已经分配给它),您应该在第一个循环中使用list[0][i] = 'a';
-即,将'a'
放入第0
字符串的i
位置。解释
char **
的另一种方法(我怀疑你应该使用的就是它)是一个指针,它指向一个指向 < 数组的指针。代码>字符。在这种情况下,您可以使用“外部”指针来修改内部指针所指向的内容;这可以用来首先分配字符串,然后写入它:在内存中,这看起来像这样:
In C, a pointer can be used to represent an array, and a single string is an array of
char
, or in other words, achar *
. This means that achar **
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 uselist[0][i] = 'a';
in the first loop - i.e., put'a'
into positioni
of the0
th 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 ofchar
. 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:In memory, this looks like this:
由于您想要一个字节数组,因此
char**
是错误的 - 这是一个指向char
的指针。您需要char*
作为数组,但如果它是固定长度,我会将其声明为char list[20]
。看起来你想写这样的东西:
或者如果你想使用堆分配
因为我们只能猜测,我认为你的意思是说
*list
是一个字节数组。在这种情况下,代码将如下所示:Since you want an array of bytes then
char**
is wrong – that's a pointer to a pointer tochar
. You wantchar*
for an array, but if it's fixed length I'd declare it aschar list[20]
.It seems like you want to write something like this:
Or if you want heap allocation use
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:不是“字节数组”,而是“指向指向字符的指针的指针”,您可以将其视为“字符列表的列表”。另外,如果您以这种方式定义它,则需要分配足够的内存来保存数据。
当您编写时:
它会弄乱内存,因为您将字符“a”放置在指定用于保存指针的位置。实际上,在大多数编译器中,字符文字都是 int 类型,因此您实际上将 int 形式的“a”存储为指向内存位置的指针,这可能会导致各种内存损坏。
如果您希望“list”位于堆栈中,则将其定义为:
如果您希望“list”位于堆中,则将其定义为:
无论哪种情况,都将其访问为:
假定 int 的大小也是不安全的相当于您在第二个示例中所做的指针的大小。至少我认为这就是你想要做的。
此外,如果您存储来自数据流或类似内容的原始字节,您可能应该使用“unsigned char”而不是“char”,并且为了更安全,请使用“int8_t”,因为您不能总是保证“char”是8位尽管它存在于大多数平台上。
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:
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:
If you want 'list' to be in the heap then define it as:
In either case access it as:
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.
您是如何初始化您的
列表
的?在这种情况下不起作用,您必须拥有类似
或动态分配的东西。
How did you initialize your
list
?doesn't work in this case you'll have to have something like
or dynamically allocated.