Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

pointer error

24 views
Skip to first unread message

raushan kumar

unread,
Nov 28, 2011, 10:02:14 PM11/28/11
to
Here I have written a program to find the center of tree .It will tell
you the vertex number which is the center of tree. Tree has been
represented
here by using adjacency matrix. In line no 109 i am using an array
of pointers to store the addresses of elements of two dimensional
array.

It showing me segmentation fault in line no 119. I want to know why i
am not able to store the addresses of elements of two dimensional
array.
Or is there another way to storing the address of those elements??

Please also explain what it means when compiler is showing
segmentation fault.









//Program to find center of any general tree
1 #include<stdio.h>
2 #include<stdlib.h>
3 int count(int g[][50], int n);
4 void merge(int g[][50], int n, int m);
5 main()
6 {
7 int g[50][50],a[50],i,j,n,m ;
8 printf("Enter no of vertex in graph\n");
9 scanf("%d",&n);
10 m = n;
11 printf("VALUE OF m = %d \n",m);
12 for(i=0;i<n;i++)
13 {
14
15 for(j=0;j<n;j++)
16 {
17 printf("Enter 1 if there is edge between vertex %d and %d else
enter 0\n",i+1,j+1);
18 scanf("%d",&g[i][j]);
19
20
21 }
22 }
23 merge(g,n,m);
24 }
25 int count(int g[][50], int n)
26 {
27 int i, j, c=0,s;
28 for(i=0;i<n;i++)
29 {
30 s = 0;
31 for(j=0;j<n;j++)
32 {
33 printf("%d ",g[i][j]);
34 s = s + g[i][j];
35 //if(g[i][j] == 1)
36 //k = j;
37 }
38 printf("\n");
39 if(s==1)
40 {
41 c++;
42 //p[i] = k;
43 }
44 }
45 printf("VALUE OF C= %d\n",c);
46 return c;
47 }
48 void merge(int g[][50],int n, int m)
49 {
50 int k,i,j,ver[1],s,sum,l,b[20],q=0;
51 int *a[20];
52 static int p=0;
53 k = count(g, n);
54 printf("VALUE OF K = %d\n",k);
55
56 if(k == m-1)
57 {
58 for(i=0;i<n;i++)
59 {
60 s=0;
61 for(j=0;j<n;j++)
62 {
63 s = s + g[i][j];
64 }
65 if(s > 1)
66 {
67 printf("center of tree = %d\n",i);
68 }
69 }
70 exit(0);
71 }
72 else if(k == m)
73 {
74 for(i=0;i<n;i++)
75 {
76 for(j=0;j<n;j++)
77 {
78 printf("%d ",g[i][j]);
79 if(g[i][j]==1)
80 ver[p++] = i;
81 else
82 continue;
83 }
84 printf("\n");
85 }
86 printf("Vertex %d and %d are center of the
tree",ver[0],ver[1]);
87 exit(0);
88 }
89
90 else
91 // exit(0);
92 {
93 for(i=0;i<n;i++)
94 {
95 sum =0;
96 for(j=0;j<n;j++)
97 {
98 printf("%d ",g[i][j]);
99 sum = sum + g[i][j];
100 if(g[i][j] == 1)
101 l = j;
102 }
103 printf("\n");
104 printf("VALUE OF SUM = %d\n",sum);
105 if(sum == 1)
106 {
107
108 g[i][l] = 0;
109 a[i] = &g[l][i];
110
111 p++;
112 m = m -1;
113 printf("VALUE OF M = %d\n",m);
114 }
115
116 }
117 for(i=0;i<=n;i++)
118 {
119 *(a[i]) = 0;
120 printf("%d",*(a[i]));
121 }
122
123 merge(g,n,m);
124 }
125 }
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Barry Schwarz

unread,
Nov 29, 2011, 5:37:56 AM11/29/11
to
On Mon, 28 Nov 2011 21:02:14 -0600 (CST), raushan kumar
<rausha...@gmail.com> wrote:

>Here I have written a program to find the center of tree .It will tell
>you the vertex number which is the center of tree. Tree has been
>represented
>here by using adjacency matrix. In line no 109 i am using an array
>of pointers to store the addresses of elements of two dimensional
>array.
>
>It showing me segmentation fault in line no 119. I want to know why i
>am not able to store the addresses of elements of two dimensional
>array.
>Or is there another way to storing the address of those elements??
>
>Please also explain what it means when compiler is showing
>segmentation fault.
>
>
>
>
>
>
>
>
>
>//Program to find center of any general tree
> 1 #include<stdio.h>
> 2 #include<stdlib.h>
> 3 int count(int g[][50], int n);
> 4 void merge(int g[][50], int n, int m);
> 5 main()

For compilers that accept this, it is the same as
int main(void).

> 6 {
> 7 int g[50][50],a[50],i,j,n,m ;

What is the purpose of a?

> 8 printf("Enter no of vertex in graph\n");
> 9 scanf("%d",&n);
> 10 m = n;
> 11 printf("VALUE OF m = %d \n",m);
> 12 for(i=0;i<n;i++)
> 13 {
> 14
> 15 for(j=0;j<n;j++)
> 16 {
> 17 printf("Enter 1 if there is edge between vertex %d and %d else
>enter 0\n",i+1,j+1);

How do you know that n <= 50?

> 18 scanf("%d",&g[i][j]);
> 19
> 20
> 21 }
> 22 }
> 23 merge(g,n,m);

So where is the value specified that main should return?
If you adopt a consistent style of indenting, it will make your
programming life a lot easier.

> 64 }
> 65 if(s > 1)
> 66 {
> 67 printf("center of tree = %d\n",i);
> 68 }
> 69 }
> 70 exit(0);
> 71 }
> 72 else if(k == m)
> 73 {
> 74 for(i=0;i<n;i++)
> 75 {
> 76 for(j=0;j<n;j++)
> 77 {
> 78 printf("%d ",g[i][j]);
> 79 if(g[i][j]==1)
> 80 ver[p++] = i;

The only element of ver is ver[0]. As soon as p is positive, this
will invoke undefined behavior.

> 81 else
> 82 continue;

Since you are already at the bottom of the "for j" loop, these two
lines serve no purpose
> 83 }
> 84 printf("\n");
> 85 }
> 86 printf("Vertex %d and %d are center of the
>tree",ver[0],ver[1]);

There is no ver[1]. This statement invokes undefined behavior.

> 87 exit(0);
> 88 }
> 89
> 90 else
> 91 // exit(0);
> 92 {
> 93 for(i=0;i<n;i++)
> 94 {
> 95 sum =0;
> 96 for(j=0;j<n;j++)
> 97 {
> 98 printf("%d ",g[i][j]);
> 99 sum = sum + g[i][j];
> 100 if(g[i][j] == 1)
> 101 l = j;
> 102 }
> 103 printf("\n");
> 104 printf("VALUE OF SUM = %d\n",sum);
> 105 if(sum == 1)
> 106 {
> 107
> 108 g[i][l] = 0;
> 109 a[i] = &g[l][i];

can have values up to 49. But a has only 20 elements. For each of
the values from 20 to 49, this invokes undefined behavior.

> 110
> 111 p++;
> 112 m = m -1;
> 113 printf("VALUE OF M = %d\n",m);
> 114 }
> 115
> 116 }
> 117 for(i=0;i<=n;i++)
> 118 {
> 119 *(a[i]) = 0;
> 120 printf("%d",*(a[i]));
> 121 }
> 122
> 123 merge(g,n,m);
> 124 }
> 125 }

--
Remove del for email

Keith Thompson

unread,
Nov 29, 2011, 5:38:11 AM11/29/11
to
raushan kumar <rausha...@gmail.com> writes:
> Here I have written a program to find the center of tree .It will tell
> you the vertex number which is the center of tree. Tree has been
> represented here by using adjacency matrix. In line no 109 i am using
> an array of pointers to store the addresses of elements of two
> dimensional array.
>
> It showing me segmentation fault in line no 119. I want to know why i
> am not able to store the addresses of elements of two dimensional
> array. Or is there another way to storing the address of those
> elements??
>
> Please also explain what it means when compiler is showing
> segmentation fault.

What is the "center" of a tree?

> //Program to find center of any general tree
> 1 #include<stdio.h>
> 2 #include<stdlib.h>
> 3 int count(int g[][50], int n);
> 4 void merge(int g[][50], int n, int m);
> 5 main()
> 6 {
> 7 int g[50][50],a[50],i,j,n,m ;
> 8 printf("Enter no of vertex in graph\n");
[...]
> 117 for(i=0;i<=n;i++)
> 118 {
> 119 *(a[i]) = 0;
> 120 printf("%d",*(a[i]));
> 121 }
> 122
> 123 merge(g,n,m);
> 124 }
> 125 }

Please don't include line numbers in code that you post here; they
don't help, and they make it difficult to copy the code if we want
to try it ourselves. If you need to refer to a specific line (line
119 in your case), just add a comment like /* this is line 119 */.

Your indentation also makes the code difficult to read. Please use
spaces, not tabs, for indentation (since tabs aren't necessarily
rendered consistently). Consider using an automatic code layout tool;
if you're on a Unix-like system, "indent -kr" is a good choice.

A segmentation fault (which occurs in your running program, not in the
compiler) generally means that your program tried to access (read or
write) memory that it doesn't have permission to access, or that doesn't
exist. Dereferencing a null pointer is one common cause of this. On
your line 119, perhaps a[i] is a null pointer, or perhaps it's just an
invalid pointer. (I say this without having looked at the entire
program.) Printing the value of the pointer:

printf("a[%d] = %p\n", i, (void*)a[i]);

*might* tell you something useful; running the program under a
debugger could also be helpful.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Jasen Betts

unread,
Dec 5, 2011, 10:14:15 AM12/5/11
to
On 2011-11-29, raushan kumar <rausha...@gmail.com> wrote:
> Here I have written a program to find the center of tree .It will tell
> you the vertex number which is the center of tree. Tree has been
> represented
> here by using adjacency matrix. In line no 109 i am using an array
> of pointers to store the addresses of elements of two dimensional
> array.

> Please also explain what it means when compiler is showing
> segmentation fault.
>

possibly you didn't give it a tree?

it ran without segfaulting when I tested it on (trivial) good input.
i used this input: 3 0 1 0 1 0 1 0 1 0

meaning the tree 1-2-3

(I think it gave the wrong answer)

I suggest you add an input validation stage and provide some sample
input.


--
⚂⚃ 100% natural

--- Posted via news://freenews.netfront.net/ - Complaints to ne...@netfront.net ---
0 new messages