English 中文(简体)
对指针感到困惑?
原标题:Confused about pointers?
  • 时间:2011-05-21 19:05:16
  •  标签:
  • c

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

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

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) =   ;

这会导致总线错误:

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

这符合要求:

*list = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

我做错了什么

P.S.列表char**

最佳回答

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

解释char**(我怀疑这是你应该使用的)的另一种方法是,它是一个指向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] =   ; // Also, you need the null terminator at the end of the string

在记忆中,它看起来是这样的:

list ---> (another pointer) ---> |a|a|a|a|a|a|...|a|a||
问题回答

由于您想要一个字节数组,那么char**是错误的——这是指向char的指针。您想要<code>char*</code>作为数组,但如果它是固定长度的,我会将其声明为<code>char list〔20〕</code〕。

你似乎想写这样的东西:

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 ;
}
char** list

字节数组不是一个指向char的指针吗?您可以将其视为char列表。此外,如果您以这种方式定义它,则需要malloc足够的内存来容纳数据。

当你写:

list[i] =  a ;

它扰乱了内存,因为你把一个字符放在了指定用来存放指针的位置。事实上,在大多数编译器中,字符文字都是int类型的,所以您实际上将的int形式存储为指向内存位置的指针,这可能会导致各种内存损坏。

若希望列表在堆栈上,则将其定义为:

char list[20]

若您希望列表在堆中,那个么将其定义为:

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

在任何一种情况下,都可以将其访问为:

list[i]

同样,假设int的大小等于指针的大小是不安全的,就像您在第二个例子中所做的那样。至少我认为这就是你想要做的。

此外,如果您存储来自数据流或类似数据流的原始字节,您可能应该使用无符号的char,而不是char,为了更安全,请使用int8_t,因为尽管在大多数平台上,您不能始终保证char是8位。

您是如何初始化列表的?

char** list;

在这种情况下不起作用,你必须吃这样的东西

char* list[20];

或动态分配。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...