假设我们在结构int *weights[]
中有一个名为Graph
的指针数组,我们将其初始化
int CreateGraph(Graph *G, int vexs){
G->n = vexs;
for(int i = 0; i < G->n; i++){
G->weights[i] = (int*)malloc(sizeof(int)*G->n);
}
for(int i = 0; i < G->n; i++){
for (int j = 0; j<G->n; j++) {
G->weights[i][j] = j;
}
}
return 1;//just for test
}
我们还有一个显示图表的功能
void show(Graph G){
for(int i = 0; i<G.n; i++){
for (int j = 0; j<G.n; j++) {
printf("%d",G.weights[i][j]);
}
}
}
主中的
Graph *g = (Graph *)malloc(sizeof(Graph));
CreateGraph(g, 3);
show(*g);
它崩溃了,xcode说EXC_BAD_ACCESS(代码= 1,地址= 0x0),但我有另一个工作版的功能秀
void success_show(Graph *G)
typedef struct {
int n;
int *weights[];
}Graph;
show(Graph G) success_show(Graph *G)
之间有什么区别。G->weights[i] = (int*)malloc(sizeof(int)*G->n);
答案 0 :(得分:3)
您必须为function loadProvince(){
$("#provinceCity").html("<option value=''>Select province</option>");
for (var i=0; i<province.length; i++){
$("#provinceCity").append("<option value='"+province[i]["id"]+"'>"+province[i]["name"]+"</option>");
}
}
$(document).ready(function(){
loadProvince();
$("#provinceCity").change(function(){
var selectedText = $("#provinceCity option:selected").text();
if (localStorage.getItem("selectedProvince") === null) {
localStorage.setItem("selectedProvince", selectedText);
}else if (localStorage.getItem("selectedProvince") !== null) {
localStorage.setItem("selectedCity", selectedText);
var selectedProvinceCity = localStorage.getItem("selectedProvince") + "-" + localStorage.getItem("selectedCity");
$('#provinceCity').append($('<option>', {
value: selectedProvinceCity,
text: selectedProvinceCity,
selected: "selected"
}));
$(this).addClass("reset");
}
for (var i=0; i<province.length; i++){
if ($(this).val() == province[i]["id"]){
$("#provinceCity").empty();
var cities = province[i]["city"];
$("#provinceCity").html("<option value=''>Select city</option>");
for (var j=0; j<cities.length; j++){
$("#provinceCity").append("<option value='"+cities[j]["id"]+"'>"+cities[j]["name"]+"</option>");
}
}
}
});
});
分配内存。
G->weights
然后为每个单独的指针元素分配内存。
G->weights = (int**) malloc(sizeof(int*)*G->n);