[429] on access to ADT's

When looking at the teacher code, found in struct ADT wrote adt.c sometimes inside, sometimes wrote adt.h which, in fact, some confusion, after careful study, found written in adt.h can test the struct .c used directly, but only in the struct in adt.c adt.c may be used, it is necessary to define the corresponding adt.h the pointer can be used.

to sum up:

  • struct written in adt.h in, you can call
  • struct written in adt.c, as long as you can call adt.c

 

☀☀☀ << Examples >> ☀☀☀

adt.c established struct, build pointer adt.h, but inaccessible in test.c

adt.h

#include <stdio.h>
#include <stdlib.h>

typedef float Weight;
typedef int Vertex;

typedef struct edge *Edge;

void showEdge(Edge);
Edge newEdge(Vertex, Vertex, Weight);

adt.c

#include <stdio.h>
#include <stdlib.h>
#include "adt.h"

struct edge { 
  Vertex v; 
  Vertex w; 
  Weight x;
};

Edge newEdge(Vertex v, Vertex w, Weight x) { // create an edge from v to w
    Edge e = malloc(sizeof(struct edge));
    
    e->v = v;
    e->w = w;
    e->x = x;
    
    return e; 
} 

void showEdge(Edge e) { // print an edge and its weight
    printf("%d-%d: %.2f", e->v, e->w, e->x);
    return; 
} 

test.c

#include "adt.h"

int main() {
	Edge e = newEdge(2, 3, 4);
	showEdge(e);
	
	//printf("\n%d, %d, %0.2f\n", e->v, e->w, e->x);
	
	return 0;
}

output:

2-3: 4.00

☀☀☀ << Examples >> ☀☀☀

 adt.h established struct, adt.c and test.c can call, but relative to the weaker

adt.h

#include <stdio.h>
#include <stdlib.h>

typedef float Weight;
typedef int Vertex;

typedef struct { 
  Vertex v; 
  Vertex w; 
  Weight x;
} Edge;

void showEdge(Edge);
Edge newEdge(Vertex, Vertex, Weight);

adt.c

#include <stdio.h>
#include <stdlib.h>
#include "adt.h"

Edge newEdge(Vertex v, Vertex w, Weight x) { // create an edge from v to w
    Edge e = {v, w, x};
    return e; 
} 

void showEdge(Edge e) { // print an edge and its weight
    printf("%d-%d: %.2f", e.v, e.w, e.x);
    return; 
} 

test.c

#include "adt.h"

int main() {
	Edge e = newEdge(2, 3, 4);
	showEdge(e);
	
	printf("\n%d, %d, %0.2f\n", e.v, e.w, e.x);
	
	return 0;
}

output:

2-3: 4.00
2, 3, 4.00

 

Guess you like

Origin www.cnblogs.com/alex-bn-lee/p/11295181.html