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 12 2270 es******@gmail. com wrote:
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 is called not according to its type (inferred from its
definition). [Remove the * in the definition] es******@gmail. com wrote:
>
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;
} 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, Thank you _very_ much.
You guys are the best.
pete wrote:
es******@gmail. com wrote:
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;
} 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
es******@gmail. com writes:
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]){
Loose the initial *. You end up with:
edge graph[MAX_GRAPH][MAX_GRAPH]
is fine, but the first size is redundant -- because a pointer to the
array will be passed, C only insists that you say exactly what each
element of this array is like, not how many there are. This one can
also (and equivalently) write:
edge graph[][MAX_GRAPH]
edge (*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);
Since the array contains structures, you need to use . to access the
elements. Note, also, that you should use %ld to print 'long int's.
}
}
return 0;
}
int main(int argc, char* argv[]){
...
print_structure (link);
...
}
--
Ben.
On Sun, 19 Aug 2007 22:52:12 -0000, es******@gmail. com wrote:
>Hello,
I am having some trouble trying to pass a pointer to an structure array (as function argument), when executing I get seg faults.
Why do you execute code (the only way to get a seg fault) when the
compiler has already told you that the code is incorrect? If you did
not receive a diagnostic for the constraint violation in your call to
print_structure , you need to up the warning level of your compiler.
> 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];
link is an array of struct.
> int print_structure (edge *graph[MAX_GRAPH][MAX_GRAPH]){
This function is expecting an array of pointer to struct. Due the
fact that an array expression in this context is converted to a
pointer to the first element, the actual type the function is
expecting is
edge (*)(*[MAX_GRAPH])
int i, j;
for(i=0;i<nodes ;i++){
We won't ask about nodes.
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);
Here you pass the array. Since the same conversion is performed, the
actual type passed is
edge (*)[MAX_GRAPH]
Notice that these are not the same type. There is also no implicit
conversion between these two types. Hence the required diagnostic.
If you delete the asterisk from the parameter in the function
definition, your code will be consistent. Then you can start to worry
about correcting the undetected coding errors, such as using %d when
you actually pass printf a long instead of an int.
Remove del for email
>On Sun, 19 Aug 2007 22:52:12 -0000, es******@gmail. com wrote:
[given: a typedef-alias named "edge", a #define constant for
MAX_GRAPH, and]
>>int print_structure (edge *graph[MAX_GRAPH][MAX_GRAPH]){
In article <ak************ *************** *****@4ax.com>
Barry Schwarz <sc******@doezl .netwrote:
>This function is expecting an array of pointer to struct.
Well, the type of the argument named "graph" is -- before the
adjustment you describe in a moment -- "array MAX_GRAPH of array
MAX_GRAPH of pointer to what-edge-is-short-for". So, array of
arrays, each element of the nested array being a pointer to struct
(since edge is short for a struct type).
>Due the fact that an array expression in this context is converted to a pointer to the first element, the actual type the function is expecting is
edge (*)(*[MAX_GRAPH])
Right -- a formal parameter whose type *appears* to be "array N of
T" (for some integer N, or even an omitted integer, and any valid
type T) really gets declared as having type "pointer to T", replacing
the first (and only the first!) "array of" part of the expanded
English version with "pointer to" -- except that the C spelling of
the type "pointer to array MAX_GRAPH of pointer to
whatever-edge-is-short-for" is actually:
edge *(*)[MAX_GRAPH]
The parentheses "look weird" because we lack the identifier that
would go in there. If this were an actual declaration instead of
just a type-name, we would have:
edge *(*ptr)[MAX_GRAPH];
Here, the need for the parentheses is clearer (if not exactly
"clear"!) since we need to bind the inner (second) "*" to "ptr",
then bind the "[MAX_GRAPH]" to that, and then bind the outer (first)
"*" to that, and finally bind the typedef-alias "edge" to that.
Without parentheses, the "[MAX_GRAPH]" would bind to "ptr" first,
then the second "*" would bind to that, and so on.
To turn a C declaration into a type-name, one simply removes the
identifier that was being declared. In this case, it results in
parentheses around the second "*".
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
On 24 Aug 2007 06:43:22 GMT, Chris Torek wrote:
>[given: a typedef-alias named "edge", a #define constant for
MAX_GRAPH, and]
>>>int print_structure (edge *graph[MAX_GRAPH][MAX_GRAPH]){
In article <ak************ *************** *****@4ax.com> Barry Schwarz wrote:
>>This function is expecting an array of pointer to struct.
Well, the type of the argument named "graph" is -- before the adjustment you describe in a moment -- "array MAX_GRAPH of array MAX_GRAPH of pointer to what-edge-is-short-for". So, array of arrays, each element of the nested array being a pointer to struct (since edge is short for a struct type).
>>Due the fact that an array expression in this context is converted to a pointer to the first element, the actual type the function is expecting is edge (*)(*[MAX_GRAPH])
Right -- a formal parameter whose type *appears* to be "array N of T" (for some integer N, or even an omitted integer, and any valid type T) really gets declared as having type "pointer to T", replacing the first (and only the first!) "array of" part of the expanded English version with "pointer to" -- except that the C spelling of the type "pointer to array MAX_GRAPH of pointer to whatever-edge-is-short-for" is actually:
edge *(*)[MAX_GRAPH]
The parentheses "look weird" because we lack the identifier that would go in there. If this were an actual declaration instead of just a type-name, we would have:
edge *(*ptr)[MAX_GRAPH];
Here, the need for the parentheses is clearer (if not exactly "clear"!) since we need to bind the inner (second) "*" to "ptr", then bind the "[MAX_GRAPH]" to that, and then bind the outer (first) "*" to that, and finally bind the typedef-alias "edge" to that. Without parentheses, the "[MAX_GRAPH]" would bind to "ptr" first, then the second "*" would bind to that, and so on.
To turn a C declaration into a type-name, one simply removes the identifier that was being declared. In this case, it results in parentheses around the second "*".
possibly
i'm not enough smart
possibly
all you game with nothing (i think all is easy but there are people
that complicates languages)
with an easy language is possible to write something that in a
difficult language can not to be written
i hope i offend nobody
¬a\/b wrote:
On 24 Aug 2007 06:43:22 GMT, Chris Torek wrote:
<snip>
>
possibly
i'm not enough smart
possibly
all you game with nothing (i think all is easy but there are people
that complicates languages)
with an easy language is possible to write something that in a
difficult language can not to be written
i hope i offend nobody
I'd have to be able to understand what you've written before I could be
offended by it...
¬a\/b wrote:
On 24 Aug 2007 06:43:22 GMT, Chris Torek wrote:
[yet another superb but intricate explanation]
possibly
i'm not enough smart
possibly
all you game with nothing (i think all is easy but there are people
that complicates languages)
with an easy language is possible to write something that in a
difficult language can not to be written
i hope i offend nobody
Check out Lisp or Forth. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Frank Münnich |
last post by:
Hi there..
My name is Frank Münnich. I've got a question about pointers that
refer to an array of a structure.
How do I declare that type?
If I have declared a structure
struct mystruc {
int x,y,z;
char a,b,c;
|
by: j0mbolar |
last post by:
I was reading page 720 of unix network programming,
volume one, second edition. In this udp_write function
he does the following:
void udp_write(char *buf, <everything else omitted)
struct udpiphdr *ui;
struct ip *ip;
ip = (struct ip *) buf;
|
by: x-pander |
last post by:
given the code:
<file: c.c>
typedef int quad_t;
void w0(int *r, const quad_t *p)
{
*r = (*p);
}
|
by: Alexei A. Frounze |
last post by:
Hi all,
I have a question regarding the gcc behavior (gcc version 3.3.4).
On the following test program it emits a warning:
#include <stdio.h>
int aInt2 = {0,1,2,4,9,16};
int aInt3 = {0,1,2,4,9};
|
by: Tobias |
last post by:
Hi!
I have a problem which is quite tricky.
I need to pass a struct from .NET to a native Win32 DLL.
But i just need to pass the pointer to a reference of that struct.
With my first struct this worked pretty well, accepting to write
unsafe code ;-)
But my next struct has an array inside and I don't get it passed over
to the DLL correctly.
Here my struct in c#:
| |
by: Jeff |
last post by:
I am struggling with the following
How do I marshal/access a pointer to an array of strings within a structure
Than
Jef
----------------------------------------------------------------
|
by: Kathy Tran |
last post by:
Hi,
Could you please help me how to declare an araay of pointer in C#.
In my program I declared an structure
public struct SEventQ
{
public uint uiUserData;
public uint uiEvent;
public uint uiParam0;
public uint uiParam1;
|
by: aegis |
last post by:
The following was mentioned by Eric Sosman
from
http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source
"The alignment requirement for any type T must be a
divisor of sizeof(T). (Proof: In `T array;' both
`array' and `array' must be correctly aligned.)
Since `sizeof(unsigned char)' is divisible only by one
and since one is a divisor of every type's size, every
type is suitably aligned for `unsigned char'."
|
by: Sam |
last post by:
I have a situation occuring in my code and I just can't see to figure
out why
I have an structure called employee that will put all of the employee
id's into a char array set to 10
struct Employee
{
char employeeid; /* id of
employee*/
|
by: gcary |
last post by:
I am having trouble figuring out how to declare a pointer to an array
of structures and initializing the pointer with a value. I've looked
at older posts in this group, and tried a solution that looked
sensible, but it didn't work right. Here is a simple example of what
I'm trying to accomplish:
// I have a hardware peripheral that I'm trying to access
// that has two ports. Each port has 10 sequential
// registers. Create a...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |