這裏是我的源代碼,這是不言自明的,我希望在這裏得到一些幫助。一切工作正常矩陣1但循環中的代碼矩陣2生成一個錯誤。 *(matrix1+i)
和*matrix1[i]
有什麼區別?我沒有看到任何=(
*(matrix1 + i)和* matrix1 [i]之間的區別?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void example(){
int i;
int qty = 5;
char **matrix1 = (char**)malloc(qty*sizeof(char));//matrix example(1)
char **matrix2 = (char**)malloc(qty*sizeof(char));//matrix example(2)
for(i=0;i<qty;i++){
//Why the heck may i allocate each vector in this way:
*(matrix1+i) = (char*)malloc(100*sizeof(char));
//...but not in this way(the code below generates an error):
*matrix2[i] = (char*)malloc(100*sizeof(char));
//And why the heck may i use strcpy this way:
strcpy(*(matrix1+i),"some string");
//...but not in this way(the code below generates an error):
strcpy(*matrix2[i],"some string");
}
}
int main(void){
example();
getch();
return 0;
}
不要把'',alloc'和朋友的結果放在C中。提示:'* matrix1 [i]'和'**(matrix1 + i)'是一樣的。注意區別? – Olaf
'* matrix [i]'與'**(matrix + i)'相同。 –
是的,我明白了,現在看來對我來說很明顯=) – Ganzert