Thread: error subscripted value is neither an array nor pointer nor vector

  1. #76
    Registered User
    Join Date
    Apr 2019
    Posts
    650
    and there is an error in my code as i changed the variable from head to head tmp and forgot to update the call to malloc

  2. #77
    Registered User
    Join Date
    Apr 2019
    Posts
    650
    and there is an error in my code as i changed the variable from head to head tmp and forgot to update the call to malloc

  3. #78
    Registered User
    Join Date
    Apr 2019
    Posts
    650
    all i want to do is have the function return a pointer to a chunk of memory that is the size of node and assign it to a pointer called head. just like laserlight did in her code. just like every example i have seen dones

  4. #79
    Registered User
    Join Date
    Feb 2019
    Posts
    358
    Quote Originally Posted by cooper1200 View Post
    all i want to do is have the function return a pointer to a chunk of memory that is the size of node and assign it to a pointer called head. just like laserlight did in her code. just like every example i have seen dones
    Ok. here's an interesting way to do it with polymorphism... Say you have a list_T type defined as:
    Code:
    // structure to keep track of the list.
    typedef struct list_s {
      node_T *head;
      node_T *tail;
    } list_T;
    And your note_T type defined as:
    Code:
    // generic node.
    typedef struct node_s {
      struct node_s *next;
    } node_T;
    If all your elements on the list is defined as:
    Code:
    struct element_s {
      node_T *next; // this pointer MUST be here always;
    
      // your data (3D vetor coordinates as example)...
      double x, y, z;  
    };
    You can deal with your list like this:
    Code:
    // Used to initialize your empty list.
    #define EMPTY_LIST { NULL, NULL }
    
    void addhead( list_T *lstp, void *elemp )
    {
      node_T *p = elemp;
    
      p->next = lstp->head;
      lstp->head = p;  // our element will be the new head.
    
      // if the list is previously empty, we have a new tail.
      if ( ! lstp->tail )
        lstp->tail = p;
    }
    Of course you'll need to allocate your new element somewhere. Example of use:
    Code:
    list_T list = EMPTY_LIST;
    struct element_s *elemp;
    
    // allocate element:
    elemp = malloc( sizeof *elemp );
    
    // fill the data
    elemp->x = 1.0f;
    elemp->y = 0.0f;
    elemp->z = sqrt(2.0);
    
    // insert element in the list:
    addhead( &list, elemp );

  5. #80
    Registered User
    Join Date
    Apr 2019
    Posts
    650
    ok this is getting beyond a joke now
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct Node
    {
        int age;
        struct Node *next;
    }Node;
    
    typedef struct LinkedList
    {
        struct Node *head;
        struct Node *tail;
    }LinkedList;
    
    struct LinkedList *create_head(void);
    
    int main()
    {
        //declare my list
        LinkedList list;
        Node data;
    
        //set both head and tail pointers to null
        list.head = NULL;
        list.tail = NULL;
    
        list.head = malloc(sizeof(data));
        if (!list.head)
        {
            printf("error allocating memory\n");
            return -1;
        }
        list.tail = list.head;
    
    
    
        return 0;
    }
    compiles no errors no warnings.

    yet all i have done is the same code (literally i cut and pasted it) in a function
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct Node
    {
        int age;
        struct Node *next;
    }Node;
    
    typedef struct LinkedList
    {
        struct Node *head;
        struct Node *tail;
    }LinkedList;
    
    struct LinkedList *create_head(Node data);
    
    int main()
    {
        //declare my list
        LinkedList list;
        Node data;
    
        //set both head and tail pointers to null
        list.head = NULL;
        list.tail = NULL;
    
        list.head = create_head(data);
    
        list.tail = list.head;
    
    
    
        return 0;
    }
    struct LinkedList *create_head(Node data)
    {
        LinkedList *head_tmp;
    
        head_tmp = malloc(sizeof(data));
        if (!head_tmp)
        {
            printf("error allocating memory\n");
        }
    
        return *head_tmp;
    }
    produces
    Code:
    linked list simple/main.c|28|warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]|
    linked list simple/main.c||In function ‘create_head’:|
    linked list simple/main.c|46|error: incompatible types when returning type ‘LinkedList {aka struct LinkedList}’ but ‘struct LinkedList
    linked list simple/main.c|47|warning: control reaches end of non-void function [-Wreturn-type]|
    so the way i read all that is it doesn't see the return statement even though it says the return statement is an error, the error on line 46 is because i passed it a linked list and it wanted a linked list. and s to the warning on line 28 god alone knows
    ||=== Build failed: 1 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|

  6. #81
    Registered User
    Join Date
    Apr 2019
    Posts
    650
    ok i have tried again and managed to change everything so it compiles with no warnings or errors. so i went ahead and added a print and delete list functions
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct Node
    {
        int age;
        struct Node *next;
    }Node;
    
    typedef struct LinkedList
    {
        struct Node *head;
        struct Node *tail;
    }LinkedList;
    
    struct LinkedList *initalize_list(LinkedList *list);
    struct Node *create_head(void);
    struct Node *get_memory(void);
    int get_age(void);
    void print_list(LinkedList list);
    void delete_list(LinkedList *list);
    
    int main()
    {
        //declare my list
        LinkedList *list = NULL;
    
        list = initalize_list(list);
        print_list(*list);
        delete_list(list);
    
        return 0;
    }
    
    struct LinkedList *initalize_list(LinkedList *list)
    {
        list->head = NULL;
        list->tail = NULL;
    
        list->head = create_head();
        list->head->next = NULL;
        list->tail = list->head;
    
        list->head->age = get_age();
        return list;
    
    }
    
    struct Node *create_head(void)
    {
        Node *head_tmp;
    
        head_tmp = get_memory();
        if (!head_tmp)
        {
            fprintf(stderr, "error allocating memory\n");
            exit(EXIT_FAILURE);// if it cant allocate memory for the head node at startup somthing really wrong so bug out
        }
    
        return head_tmp;
    }
    
    struct Node *get_memory(void)
    {
        return malloc(sizeof(Node));
    }
    
    int get_age(void)
    {
        int age;
    
        printf("PLease enter an age: ");
        scanf(" %d", &age);
    
        return age;
    }
    
    void print_list(LinkedList list)
    {
        int count = 1;
        Node *tmp = list.head;
    
        while (tmp)
        {
            printf("person%d's age is %d\n", count, list.head->age);
            count++;
            tmp = list.head->next;
        }
    }
    
    void delete_list(LinkedList *list)
    {
        int count = 1;
        Node *tmp;
    
    
        printf("freeing memory.....\n");
        while (list->head)
        {
            tmp = list->head->next;
            printf("freeing element %d\n", count);
            free(list->head);
            list->head = tmp;
            count++;
        }
        printf("memory freed\n");
    }
    however when i run it get seg fault on line 37.
    coop

  7. #82
    Registered User
    Join Date
    Apr 2019
    Posts
    650
    ok after hours of searching and re-watching videos i just don't get what i have done wrong. it just will not let me set head to anything be it a null or a pointer derived from malloc so either there is something really wrong or this struct idea holding a pointer to the head and tail just doesn't work.

    the whole thing has been an exercise in futility i just don't understand this at all.

  8. #83
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,810
    Have a closer look at what create_head is returning - Hint: It's not the same as list.head

  9. #84
    Registered User
    Join Date
    Apr 2019
    Posts
    650
    its returning the pointer from malloc

  10. #85
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,810
    list.head is a struct Node *
    create_head returns a struct _ *

  11. #86
    Registered User
    Join Date
    Apr 2019
    Posts
    650
    or more precisely it returns head_tmp which is of type node and set to the pointer returned from malloc

    and as list_head is of type node there is no miss match if i have it return a type of linked list it whines like a moped with its throttle stuck open

  12. #87
    Registered User
    Join Date
    Apr 2019
    Posts
    650
    Quote Originally Posted by Click_here View Post
    list.head is a struct Node *
    create_head returns a struct _ *
    how is it i have defined it as struct node

  13. #88
    Registered User
    Join Date
    May 2010
    Posts
    4,374
    however when i run it get seg fault on line 37.
    Code:
     
    int main()
    {
        //declare my list
        LinkedList *list = NULL;
     
        list = initalize_list(list);
    ...
     
    struct LinkedList *initalize_list(LinkedList *list)
    {
        list->head = NULL;
    ...
    You create a list pointer and assign NULL to that pointer, then pass that NULL pointer into your initialize_list() function, next you try to assign NULL to a field of a NULL pointer object, result CRASH. You need to assign memory to that pointer before you try to use it.

  14. #89
    Registered User
    Join Date
    Apr 2019
    Posts
    650
    it crashes when i try to assign memory as well

  15. #90
    Registered User
    Join Date
    Apr 2019
    Posts
    650
    however i didn't realise that linkedlist *list = null is the same as linked list *list ...... list->head = null list->tail = null

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Subscripted value is neither array nor pointer ?
    By prietito1 in forum C Programming
    Replies: 2
    Last Post: 08-08-2012, 12:42 AM
  2. subscripted value is neither array nor pointer: 2 files
    By pochis40 in forum C Programming
    Replies: 6
    Last Post: 05-27-2011, 03:16 PM
  3. Replies: 1
    Last Post: 05-08-2010, 10:03 PM
  4. Replies: 9
    Last Post: 03-16-2009, 02:18 AM
  5. subscripted value is neither array nor pointer
    By new_to_c in forum C Programming
    Replies: 8
    Last Post: 04-01-2007, 02:43 PM

Tags for this Thread