简体   繁体   中英

error accessing array of structure pointers

I am trying to build an adjacency list but get the following error

graph.c:16: error: subscripted value is neither array nor pointer

I read that this error occurs when a non array is trying to be indexed. When I am able to add an element to it directly (line 58: graph[i] = root), can I please know what is the error in assigning a member of the structure array to a NODE?

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10
struct node{
    int data;
    struct node * link;
};

typedef struct node * NODE;
NODE graph[MAX];
void displayGraph (graph, n){
int i;
NODE cur;
for (i=0;i<n;i++){
    cur = graph[i];
    while(cur != NULL){
        printf("%d ", cur->data);
        }
    }
}

NODE insert (NODE root, NODE temp){
NODE mine;
mine = root;
    if (mine == NULL)
        return root;
    while(mine != NULL){
    mine = mine->link;
    }
mine->link = temp;
return root;
}

main ()
{
int n=0;
int i;
int val;
char * choice;
NODE temp, root;
printf("Enter the number of nodes\n");
scanf("&d", n);
for (i=0;i<n;i++){
    root = NULL;
    while(1){
        printf("Is there an adjacent node?Y:N");
        scanf("%s", choice);
        if(!strcmp(choice, "N"));
            break;
        printf("Enter the adjacent node\n");
        scanf("%d", val);
        temp = malloc(sizeof (struct node));
        temp->data = val;
        temp->link = NULL;
        root = insert(root, temp);
         }
graph[i] = root;
    }
displayGraph (graph, n);
}

You haven't specified a type for the variable graph when you declared the displayGraph function.

void displayGraph (graph, n);

Seeing as graph is declared globally, you can technically omit graph as an argument to this function. You will also need to provide a type for the variable n, but if you insist on having displayGraph accept the graph array, then change:

void displayGraph (graph, n){

to

void displayGraph (NODE graph[], int n){

There are a few other problems with your code, but this should fix the error you are asking about.

Besides the formatting rendering this code difficult to read, and understand, the problems in your code include:

In general, your calls to scanf() need syntax corrections:
ie scanf("&d", &n); should be scanf("%d", &n);
( % instead of & for first argument)

Line 12: void displayGraph (graph, n){
function definition should be:
void displayGraph (NODE * graph, int n){
I am assuming int n because int n=0; appears in the code before
and I am assuming NODE * because of declarations:
typedef struct node * NODE;
NODE graph[MAX];

cur; is a single instance of type NODE
graph[MAX]; is an array of instances of type NODE

Line 13: cur = graph[i]; is not a legal assignment.
You can to do something like this to initialize the pointer cur:

NODE graph[MAX], *cur;  
cur = &graph[0];//to initialize cur to the first position of graph  

Line 48, scanf("%s", choice); choice is not initialized when created, do this:
char * choice;
then later, before you use it:
choice = malloc(NumBytes); //where NumBytes is a value of bytes large enough for the string you will scanf into it.

Line 49: the following lines result in unreachable code for line 51:

if(!strcmp(choice, "N"));
    break;              ^

Remove the ; after the if statement

Line 52: val has not been previously initialized, and here should be written &val

In the following code, I have addressed all of the mentioned issues, including formatting, however, I have not debugged your code, so I am not certain of anything beyond it will compile and build :)

#include<stdlib.h>
#include<string.h>
#define MAX 10

struct node{
    int data;
    struct node * link;
};

typedef struct node * NODE;
NODE graph[MAX];

void displayGraph (NODE * graph, int n);
NODE insert(NODE root, NODE temp);

main ()
{
    int n=0;
    int i;
    int val;
    char * choice;
    NODE temp, root;
    printf("Enter the number of nodes\n");
    scanf("%d", &n);
    for (i=0;i<n;i++)
    {
        root = NULL;
        while(1)
        {
            choice = malloc(2);//based on promted answer being only 1 byte long
            printf("Is there an adjacent node?Y:N");
            scanf("%s", choice);
            //if(!strcmp(choice, "N"))//case sensitive 
            if(!stricmp(choice, "N")) //not case sensitive
            {
                break;
            }
            printf("Enter the adjacent node\n");
            scanf("%d", &val);
            temp = malloc(sizeof (struct node));
            temp->data = val;
            temp->link = NULL;
            root = insert(root, temp);
            free(choice);
        }
        graph[i] = root;
    }
    displayGraph (graph, n);
}

void displayGraph (NODE * graph, int n)
{
    int i;
    NODE cur;
    for (i=0;i<n;i++)
    {
        cur = graph[i];
        while(cur != NULL)
        {
            printf("%d ", cur->data);
        }
    }
}

NODE insert(NODE root, NODE temp)
{
    NODE mine;
    mine = root;
    if (mine == NULL) return root;
    while(mine != NULL)
    {
        mine = mine->link;
    }
    mine->link = temp;
    return root;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM