指向结构数组的指针 [英] pointer to structure array

查看:81
本文介绍了指向结构数组的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我在尝试将指针传递给结构时遇到一些麻烦

数组(作为函数参数),执行时我得到了seg故障。


我可以通过谷歌搜索得到的最好的是:


#define MAX_GRAPH 256


typedef struct edge {

long delay;

long bandw;

} edge;


edge link [MAX_GRAPH] [MAX_GRAPH];


int print_structure(edge * graph [MAX_GRAPH] [MAX_GRAPH]){

int i,j; < ($ = 0; i< nodes; i ++){b / b $ j $ 0; j< nodes; j ++){

printf ([%d] [%d] .bandw =%d \ t \ t",i,j,graph [i] [j] - > bandw); //

赛格故障!

printf(" [%d] [%d] .delay =%d \ n",i,j,graph [i ] [j] - >延迟);

}

}

返回0;

}

int main(int argc,char * argv []){


....

print_structure(link);

....

}


有人能帮我发现问题吗?


谢谢,


Paulo

Hello,

I am having some trouble trying to pass a pointer to an structure
array (as function argument), when executing I get seg faults.

The best I could get out from googling was:

#define MAX_GRAPH 256

typedef struct edge{
long delay;
long bandw;
} edge;

edge link[MAX_GRAPH][MAX_GRAPH];

int print_structure(edge *graph[MAX_GRAPH][MAX_GRAPH]){
int i, j;
for(i=0;i<nodes;i++){
for(j=0;j<nodes;j++){
printf("[%d][%d].bandw = %d\t\t", i, j, graph[i][j]->bandw); //
SEG FAULT!
printf("[%d][%d].delay = %d\n", i, j, graph[i][j]->delay);
}
}
return 0;
}
int main(int argc, char* argv[]){

....
print_structure(link);
....
}

Could anybody help me spot the problem?

Thanks,

Paulo

推荐答案

es ****** @ gmail.com 写道:

您好,


我在尝试将指针传递给结构时遇到了一些麻烦:
数组(作为函数参数),执行时我遇到了seg错误。

我最好能从谷歌搜索中获得:


#define MAX_GRAPH 256


typedef struct edge {

长延迟;

长带;

}边缘;


边缘链接[MAX_GRAPH] [ MAX_GRAPH];


int print_structure(edge * graph [MAX_GRAPH] [MAX_GRAPH]){

int i,j;

for(i = 0; i< nodes; i ++){

for(j = 0; j< nodes; j ++){

printf(" [%d] [%d] .bandw =%d \ t \ t",i,j,graph [i] [j] - > bandw); //

赛格故障!

printf(" [%d] [%d] .delay =%d \ n",i,j,graph [i ] [j] - >延迟);

}

}

返回0;

}


int main(int argc,char * argv []){


...

print_structure(link) ;

...

}


有人能帮我发现问题吗?


谢谢,


Paulo
Hello,

I am having some trouble trying to pass a pointer to an structure
array (as function argument), when executing I get seg faults.

The best I could get out from googling was:

#define MAX_GRAPH 256

typedef struct edge{
long delay;
long bandw;
} edge;

edge link[MAX_GRAPH][MAX_GRAPH];

int print_structure(edge *graph[MAX_GRAPH][MAX_GRAPH]){
int i, j;
for(i=0;i<nodes;i++){
for(j=0;j<nodes;j++){
printf("[%d][%d].bandw = %d\t\t", i, j, graph[i][j]->bandw); //
SEG FAULT!
printf("[%d][%d].delay = %d\n", i, j, graph[i][j]->delay);
}
}
return 0;
}
int main(int argc, char* argv[]){

...
print_structure(link);
...
}

Could anybody help me spot the problem?

Thanks,

Paulo



打印print_struct不是根据其类型调用的(从其推断

定义)。 [删除定义中的*]

print_struct is called not according to its type (inferred from its
definition). [Remove the * in the definition]


es ****** @ gmail.com 写道:

>

您好,


我在尝试将指针传递给结构

数组(作为函数参数)时遇到了一些麻烦,执行时我遇到了seg错误。


最好的我可以从谷歌搜索中获得:


#define MAX_GRAPH 256

typedef struct edge {

long delay;

长带;

}边缘;


边缘链接[MAX_GRAPH] [MAX_GRAPH];


int print_structure(edge * graph [MAX_GRAPH] [MAX_GRAPH]){

int i,j;

for(i = 0; i< nodes; i ++){

for(j = 0; j< nodes; j ++){

printf(" [%d] [%d] .bandw =%d \\ n \\ t \t",i,j,graph [i] [j] - > bandw); //

赛格故障!

printf(" [%d] [%d] .delay =%d \ n",i,j,graph [i ] [j] - >延迟);

}

}

返回0;

}


int main(int argc,char * argv []){


...

print_structure(link) ;

...

}


有人能帮我发现问题吗?
>
Hello,

I am having some trouble trying to pass a pointer to an structure
array (as function argument), when executing I get seg faults.

The best I could get out from googling was:

#define MAX_GRAPH 256

typedef struct edge{
long delay;
long bandw;
} edge;

edge link[MAX_GRAPH][MAX_GRAPH];

int print_structure(edge *graph[MAX_GRAPH][MAX_GRAPH]){
int i, j;
for(i=0;i<nodes;i++){
for(j=0;j<nodes;j++){
printf("[%d][%d].bandw = %d\t\t", i, j, graph[i][j]->bandw); //
SEG FAULT!
printf("[%d][%d].delay = %d\n", i, j, graph[i][j]->delay);
}
}
return 0;
}

int main(int argc, char* argv[]){

...
print_structure(link);
...
}

Could anybody help me spot the problem?



/ * BEGIN new.c * /


#include< stdio.h>


#define MAX_GRAPH 3

typedef struct edge {

long delay;

long bandw;

}边缘;


size_t nodes = MAX_GRAPH;


void print_structure(edge(* graph)[MAX_GRAPH]) ;


int main(无效)

{

边连结[MAX_GRAPH] [MAX_GRAPH] = {

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16

};


print_structure(link);

返回0;

}


void print_structure(edge(* graph) )[MAX_GRAPH])

{

size_t i,j;


for(i = 0; i< nodes; i ++){

for(j = 0; j< nodes; j ++){

printf(" [%d] [%d] .bandw =%ld \t",i,j,graph [i] [j] .bandw);

printf(" [%d] [%d] .delay =%ld \ n", i,j,graph [i] [j] .delay);

}

}

}


/ * END new.c * /

-

pete

/* BEGIN new.c */

#include <stdio.h>

#define MAX_GRAPH 3

typedef struct edge {
long delay;
long bandw;
} edge;

size_t nodes = MAX_GRAPH;

void print_structure(edge (*graph)[MAX_GRAPH]);

int main(void)
{
edge link[MAX_GRAPH][MAX_GRAPH] = {
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16
};

print_structure(link);
return 0;
}

void print_structure(edge (*graph)[MAX_GRAPH])
{
size_t i, j;

for(i = 0; i < nodes; i++){
for(j=0;j < nodes; j++){
printf("[%d][%d].bandw = %ld\t", i, j, graph[i][j].bandw);
printf("[%d][%d].delay = %ld\n", i, j, graph[i][j].delay);
}
}
}

/* END new.c */
--
pete


Ark and Pete,非常感谢你。


你们是最好的。

pete写道:
Ark and Pete, Thank you _very_ much.

You guys are the best.
pete wrote:
es ****** @ gmail.com 写道:


您好,


我在尝试将指针传递给一个结构时遇到了一些麻烦
数组(作为函数参数),执行时我遇到了seg错误。


我可以通过Google搜索获得的最佳效果是:


#define MAX_GRAPH 256

typedef struct edge {

长延迟;

长带;

}边缘;


边缘链接[MAX_GRAPH] [ MAX_GRAPH];


int print_structure(edge * graph [MAX_GRAPH] [MAX_GRAPH]){

int i,j;

for(i = 0; i< nodes; i ++){

for(j = 0; j< nodes; j ++){

printf(" [%d] [%d] .bandw =%d \\ t\t" ;, I,J,图表[i] [j] - GT; bandw); //

赛格故障!

printf(" [%d] [%d] .delay =%d \ n",i,j,graph [i ] [j] - >延迟);

}

}

返回0;

}


int main(int argc,char * argv []){


...

print_structure(link) ;

...

}


有人能帮我发现问题吗?

Hello,

I am having some trouble trying to pass a pointer to an structure
array (as function argument), when executing I get seg faults.

The best I could get out from googling was:

#define MAX_GRAPH 256

typedef struct edge{
long delay;
long bandw;
} edge;

edge link[MAX_GRAPH][MAX_GRAPH];

int print_structure(edge *graph[MAX_GRAPH][MAX_GRAPH]){
int i, j;
for(i=0;i<nodes;i++){
for(j=0;j<nodes;j++){
printf("[%d][%d].bandw = %d\t\t", i, j, graph[i][j]->bandw); //
SEG FAULT!
printf("[%d][%d].delay = %d\n", i, j, graph[i][j]->delay);
}
}
return 0;
}

int main(int argc, char* argv[]){

...
print_structure(link);
...
}

Could anybody help me spot the problem?



/ * BEGIN new.c * /


#include< stdio.h>


#define MAX_GRAPH 3

typedef struct edge {

long delay;

long bandw;

}边缘;


size_t nodes = MAX_GRAPH;


void print_structure(edge(* graph)[MAX_GRAPH]) ;


int main(无效)

{

边连结[MAX_GRAPH] [MAX_GRAPH] = {

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16

};


print_structure(link);

返回0;

}


void print_structure(edge(* graph) )[MAX_GRAPH])

{

size_t i,j;


for(i = 0; i< nodes; i ++){

for(j = 0; j< nodes; j ++){

printf(" [%d] [%d] .bandw =%ld \t",i,j,graph [i] [j] .bandw);

printf(" [%d] [%d] .delay =%ld \ n", i,j,graph [i] [j] .delay); < br $>
}

}

}


/ * END new.c * /


-

pete


/* BEGIN new.c */

#include <stdio.h>

#define MAX_GRAPH 3

typedef struct edge {
long delay;
long bandw;
} edge;

size_t nodes = MAX_GRAPH;

void print_structure(edge (*graph)[MAX_GRAPH]);

int main(void)
{
edge link[MAX_GRAPH][MAX_GRAPH] = {
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16
};

print_structure(link);
return 0;
}

void print_structure(edge (*graph)[MAX_GRAPH])
{
size_t i, j;

for(i = 0; i < nodes; i++){
for(j=0;j < nodes; j++){
printf("[%d][%d].bandw = %ld\t", i, j, graph[i][j].bandw);
printf("[%d][%d].delay = %ld\n", i, j, graph[i][j].delay);
}
}
}

/* END new.c */
--
pete


这篇关于指向结构数组的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆