我有以下代码.也许我没有理解指针算法以及我应该有但是为什么int_pointer增加4而不是1?使用char_pointer,为什么它不会增加4而不是1?
#include <stdio.h>
int main() {
int i;
char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
int int_array[5] = {1, 2, 3, 4, 5};
char *char_pointer;
int *int_pointer;
char_pointer = int_array; // The char_pointer and int_pointer now
int_pointer = char_array; // point to incompatible data types.
for(i=0; i < 5; i++) { // Iterate through the int array with the int_pointer.
printf("[integer pointer] points to %p, which contains the char '%c'\n",
int_pointer, *int_pointer);
int_pointer = int_pointer + 1;
}
for(i=0; …Run Code Online (Sandbox Code Playgroud) 我正在尝试为三指针分配内存.我有以下内容:
int i, j;
int n = 4;
int ***X = (int ***) malloc(sizeof(int) * n);
for(i = 0; i < n; i++){
printf("h\n");
X[i] = (int **) malloc(sizeof(int) * n);
for(j = 0; j < n; j++){
printf("j\n");
X[i][j] = (int *) malloc(sizeof(int) * n);
}
}
X[0][0][0] = 14;
X[1][2][2] = 15;
Run Code Online (Sandbox Code Playgroud)
当我在Linux上运行它时,我得到*** glibc detected *** triplePointer: double free or corruption (out): 0x0000000000ea3050 ***错误,我完全不知道它是什么意思.但是当我在Windows上使用-Wall标志运行它时,我没有错误.有人可以帮我找到我的错误所在.
此外,我目前正在通过声明进行硬编码X[0][0][0] = 14;.有没有办法可以通过一些随机值填充这个三重指针的所有插槽?
在K&R的C编程语言中,我继续看到代码
while(*s++ = *t++) ;,这只是第5章指针中的一个例子.这对我来说完全令人困惑,因为我虽然while循环如下:
while (something is true)
do what your assigned to do
Run Code Online (Sandbox Code Playgroud)
是某种特殊情况与C或我错过了什么?作为初学C程序员,这只是我的头脑.关于如何while(*s++ = *t++) ;工作的任何解释都会很棒.
我不会对表达的含义感到困惑(*s++ = *t++).我只是想知道为什么while循环体中没有任何东西.