struct array type problem

why my program will be crashed, although I have allocated the corrected capacity to pointer a already;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include<stdio.h>
#include<stdlib.h>
typedef struct adjacency_table_node{
	int index;
	int weight;
	struct adjacency_table_node* next;
}adjacency_table_node;

typedef struct adjacency_table{
	char data;
	adjacency_table_node* first;
}adjacency_table,Adjacency_Table[100];

int main(){
	Adjacency_Table* a=(Adjacency_Table*)malloc(sizeof(Adjacency_Table));
	int i;
	for(i=0;i<10;i++){
		printf("hi");
		a[i]->data='A'+i;
		a[i]->first=NULL;
	}
}
Last edited on
As I recall, http://www.cplusplus.com/forum/beginner/237516/ you have had a "how much is much?" dilemma.

The reason I visit this C++ forum is to forget C. For example, the typedef struct foo {} bar, gaz[42]; looks scary.

My wild guess would be:
1
2
3
4
5
6
7
8
9
10
11
12
13
typedef struct {
	char data;
} Foo;

int main() {
  int N = 10;
  Foo* a=(Foo*)malloc( N * sizeof(Foo) );
  int i;
  for ( i=0; i<N; i++) {
    printf("hi");
    a[i].data='A'+i;
  }
}
Last edited on
I see that you reserve enough space for one element.

a[i]->data that should have been your first clue, ¿why do you need to use the arrow (->) operator instead of the dot (.)?
¿what's the type of `a'? ¿what's the type of `a[0]'? ¿what's the type of `a[0][0]'?
thanks for replying.
keskiverto your solution is work , thanks a lot,but i would like to find out what wrong with my program.

ne555 i tried to use a[i].data but the compilar show the error messages"19 8 C:\Users\user\Desktop\Untitled1.cpp [Error] request for member 'data' in '*(a + ((sizetype)(((long long unsigned int)i) * 1600ull)))', which is of non-class type 'Adjacency_Table {aka adjacency_table [100]}'"
What you return on line 15 is this:

adjacency_table[100] *

hence to access the array you need to write this:

(*a)[i].data='A'+i;
coder77 thx!
Topic archived. No new replies allowed.