ABHINAV RANA - 1 year ago 111
C Question

# Why does pointer value is being used as . instead of -> in this

``````    struct AdjListNode
{
int dest;
};

// A structure to represent an adjacency list
{
};

// A structure to represent a graph. A graph is an array of adjacency lists.
// Size of array will be V (number of vertices in graph)
struct Graph
{
int V;
};

struct Graph* createGraph(int V)
{
struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
graph->V = V;

// Create an array of adjacency lists.  Size of array will be V

// Initialize each adjacency list as empty by making head as NULL
int i;
for (i = 0; i < V; ++i)

return graph;
}

// Adds an edge to an undirected graph
void addEdge(struct Graph* graph, int src, int dest)
{
// list of src.  The node is added at the begining

// Since graph is undirected, add an edge from dest to src also
}
``````

Specifically

``````graph->array[i].head = NULL;
``````

• Ok so the graph is a pointer of type Graph

• hence uses -> to access array which is an object of graph of type AdjList
which has a
pointer of type AdjListNode which is also a pointer.

Since *array member in Graph is a pointer to AdjList then why is

``````graph->array[i]->head =  NULL;
``````

not used ?

I know that
`.`
operator is used when we access a member of struct which is not a pointer.

``````somestructpointer -> itsmember
``````

is basically sugar coating

``````(*somestructpointer).itsmember
``````

I don't understand what's happening. HELP.

`graph` is a pointer.
`graph->array`dereferences the above pointer to to get to the array variable.
`graph->array[i]` uses array notation to reach a member at position `i`.
The member we reach is of type `struct AdjList`, which is not a pointer. It's a `struct`. So a dot(`.`) is appropriate.