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

  1. #91
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    it crashes when i try to assign memory as well
    Show what you tried.

    however i didn't realise that linkedlist *list = null is the same as linked list *list ...... list->head = null list->tail = null
    But they are not the same you have three different pointers.

    Let me ask another couple of questions: What is the purpose of your list pointer in main()? Why are you trying to return list from your initialize function?

  2. #92
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    linked list holds the head and the tail points of the list. so it is like the first i had in my previous code (page 2) i need the updated list from initalize so i can pass it onto other functions like add node print list etc etc.

    as to what i tried with the call to malloc i just simply commented out lines 37 and 38

  3. #93
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    as to what i tried with the call to malloc i just simply commented out lines 37 and 38
    Show me exactly what you tried, just removing things is not going to solve the problem.

  4. #94
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Look closer at how things are defined - Go back to basics...


    how is it i have defined it as struct node ...

    Code:
    typedef struct LinkedList
    {
        struct Node *head;
        struct Node *tail;
    }LinkedList;
    
    ...
    
    struct LinkedList *create_head(void);
    
    ...
    
    list.head = create_head(data);
    Okay... now let me rename things to make them clearer..

    I chose shapes because it is easier to visualize.

    Imagine a square (say 10cm x 10cm) with a 2cm circular hole in it
    Code:
    typedef struct Square
    {
    
        struct Circle *circle;  // only a circle can go here
    
    }Square;
    
    ...
    
    // Makes and returns a 10cm x 10cm square
    struct Square *create_square(void);
    
    ...
    
    // Tries to put a 10cm x 10cm square in a 2cm circle
    list.circle = create_square(data);
    Fact - Beethoven wrote his first symphony in C

  5. #95
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    im clearly not understanding this.....
    if i have a function int foo() it returns a type int yes?
    if i have a function char foo it returns a type char
    etc
    if i have a unction int *foo why doesnt it return a pointer to a type int

  6. #96
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    if i change the return type to linked list and the head_temp to type linked list
    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 LinkedList *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 LinkedList *create_head(void)
    {
        LinkedList *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");
    }
    i get the following warnings:
    Code:
    linked list simple/main.c|40|warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]|
    /linked list simple/main.c|53|warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]|

  7. #97
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It does, but what I've noticed is that you have a bad habit of dereferencing a pointer then returning the result, so that's probably what you've been doing: returning an int when you want to return a pointer to int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #98
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    broadly i have done every permitation of it that i can think of and only the one where i return a pointer of type node compiles

  9. #99
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    if i return *head_tmp i get an error

  10. #100
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Okay... Let me be more direct...
    Code:
    struct LinkedList *create_head(void);
    ... should be

    Code:
    struct Node *create_head(void);
    Why? Because the "head" is a Node. What is a "Node"? An individual element on the list - i.e. One person's age

    This means that in create_head you'll need to change the type that you "malloc" to a Node.


    What you are currently doing is creating not a "head", but another struct that contains a "head" and "tail" - aka a "LinkedList"
    Fact - Beethoven wrote his first symphony in C

  11. #101
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    Quote Originally Posted by laserlight View Post
    It does, but what I've noticed is that you have a bad habit of dereferencing a pointer then returning the result, so that's probably what you've been doing: returning an int when you want to return a pointer to int.
    if int * foo() returns a pointer of type int.... why doesn't struct node *foo() return a pointer of type struct node

  12. #102
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cooper1200
    if int * foo() returns a pointer of type int.... why doesn't struct node *foo() return a pointer of type struct node
    It does. I think Click_here identified the problem.

    By the way, you posted an error message about incompatible types. Doesn't the error message mention the types involved? Like, didn't it say that a struct LinkedList* was expected, but a struct Node* was found, or something to that effect?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #103
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    Quote Originally Posted by laserlight View Post
    It does. I think Click_here identified the problem.

    By the way, you posted an error message about incompatible types. Doesn't the error message mention the types involved? Like, didn't it say that a struct LinkedList* was expected, but a struct Node* was found, or something to that effect?
    i thought click here said it returned a stuct _*

    the error message i got was
    Code:
    linked list simple/main.c|46|error: incompatible types when returning type ‘LinkedList {aka struct LinkedList}’ but ‘struct LinkedList

  14. #104
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cooper1200
    i thought click here said it returned a stuct _*
    Click_here wrote that you declared create_head as returning a struct LinkedList* when it should have been declared as returning a struct Node*.

    Quote Originally Posted by cooper1200
    the error message i got was
    Exactly. You should pay attention to the error message in detail. Notice "incompatible types when returning type ‘LinkedList aka struct LinkedList’ but ‘struct LinkedList". You truncated that part of the error message. Perhaps you didn't think it was important? At a glance, in literally a second or two of looking at that error message including that part about the types, I already know what's the problem. If I already knew the context, I could fix the error in say, 10 to 15 seconds. Not kidding. But without that full error message, and because you didn't correctly post in code tags, it took me minutes to identify the problem.

    You need to understand error messages. Don't just look at the line number and "try every permutation". Understand the error message, then you just need to try one permutation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #105
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    Quote Originally Posted by Click_here View Post
    Okay... Let me be more direct...
    Code:
    struct LinkedList *create_head(void);
    ... should be

    Code:
    struct Node *create_head(void);
    Why? Because the "head" is a Node. What is a "Node"? An individual element on the list - i.e. One person's age

    This means that in create_head you'll need to change the type that you "malloc" to a Node.


    What you are currently doing is creating not a "head", but another struct that contains a "head" and "tail" - aka a "LinkedList"
    isnt this exactly what i had in post 81

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