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

  1. #106
    Registered User
    Join Date
    Apr 2019
    Posts
    655
    Quote Originally Posted by laserlight View Post
    Click_here wrote that you declared create_head as returning a struct LinkedList* when it should have been declared as returning a struct Node*.


    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.
    were getting confused as to which verstion of tjhe code were talking about. the comment in post 99 was about an earlier attemp at the code where i had made it compile with everything in main then copied it into afunction and got aload of warnings and an the above error

  2. #107
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,849
    In post #88, jimblumberg correctly identified the reason for the segfault you experienced running your code in post #81.

    The fix is so simple. In main:
    Code:
    LinkedList list;
    initalize_list(&list);
    print_list(list);
    delete_list(&list);
    Actually, print_list should have a const LinkedList* parameter.

    EDIT:
    Quote Originally Posted by cooper1200
    were getting confused as to which verstion of tjhe code were talking about. the comment in post 99 was about an earlier attemp at the code where i had made it compile with everything in main then copied it into afunction and got aload of warnings and an the above error
    That's the version of the code I'm talking about: you tried different things and only managed to fix it when you changed to return struct Node*. If you understood the error message, you would have done that change straight away.
    Last edited by laserlight; 4 Weeks Ago at 07:14 PM.
    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

  3. #108
    Registered User
    Join Date
    Apr 2019
    Posts
    655
    nope that doesn't work either
    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;
    
        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");
    }
    produces
    Code:
    ||=== Build: Debug in linked list simple (compiler: GNU GCC Compiler) ===|
    /home/ben/Documents/coding/linked list simple/main.c||In function ‘main’:|
    /home/ben/Documents/coding/linked list simple/main.c|28|error: incompatible types when assigning to type ‘LinkedList {aka struct LinkedList}’ from type ‘struct LinkedList *’|
    /home/ben/Documents/coding/linked list simple/main.c|29|error: invalid type argument of unary ‘*’ (have ‘LinkedList {aka struct LinkedList}’)|
    ||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

  4. #109
    Registered User
    Join Date
    Apr 2019
    Posts
    655
    opps forgot to take the * off list in the call to print list

  5. #110
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,818
    Quote Originally Posted by cooper1200 View Post
    opps forgot to take the * off list in the call to print list
    Awesome - It sounds like you are back on your way

  6. #111
    Registered User
    Join Date
    Apr 2019
    Posts
    655
    nope it still wont compile i have the error on line 28

  7. #112
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,849
    Sad to hear that. My code works though. Maybe you should read the error message carefully to find out why your version of my correct code doesn't work. Comparing your code with mine might help too, although it might not teach you why.
    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. #113
    Registered User
    Join Date
    Apr 2019
    Posts
    655
    maybe thats because you know what yoir doing and clearly changed somthing else an as im learning i dont

  9. #114
    Registered User
    Join Date
    Apr 2019
    Posts
    655
    the error message says the linked list isn't compatible with linked list which is like say i want a banana but i don't want that banana

  10. #115
    Registered User
    Join Date
    May 2009
    Posts
    3,597
    A pointer to a banana is not a banana!
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  11. #116
    Registered User
    Join Date
    Apr 2019
    Posts
    655
    initalize list wants the address of list and i passed it the address of list with the suggestion of &list it returns a pointer of type linked list to a linked list member that is a pointer
    so what the hell

    so i dont have the foggyiest what is wrong

  12. #117
    Registered User
    Join Date
    Apr 2019
    Posts
    655
    Quote Originally Posted by stahta01 View Post
    A pointer to a banana is not a banana!
    so because its a pointer its changing its type ???? i thought we agreed on int foo() returned a type int and int *foo() returned a pointer to type int

  13. #118
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,849
    Let's break down the error message:

    This tells you that in the file main.c residing at a particular file system path, there is at least one error (or warning) in the function named main:
    Code:
    /home/ben/Documents/coding/linked list simple/main.c||In function ‘main’:|
    This tells you that in the file main.c, an error was detected on line 28:
    Code:
    /home/ben/Documents/coding/linked list simple/main.c|28|error:
    This tells you that the error is concerned with incompatible types in an assignment, i.e., the type of the right hand operand could not be implicitly converted to the type of the left hand operand in the assignment:
    Code:
    incompatible types when assigning
    This tells you that the left hand operand of the assignment has type LinkedList, but the right hand operand has type pointer to LinkedList:
    Code:
    to type ‘LinkedList {aka struct LinkedList}’ from type ‘struct LinkedList *’|
    So, you cannot perform this assignment. Think: do you need this assignment? Of course not: the LinkedList object has already been modified through the pointer argument. Hence, you can simply get rid of the assignment.
    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

  14. #119
    Registered User
    Join Date
    Apr 2019
    Posts
    655
    ok thanks that worked.

    as i have already changed the list (in this case created the head node and allocated memory to it do i need to return anything at all.

  15. #120
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    439
    Gotta set out for work in a minute so can't stay and chat but I notice you seem to not understand how to use types, I'm going to use a baby's toy for this analogy, remember those hexagonal spheres with different shaped holes all over them? Think of the holes as your variables and the shapes as the values given to those variables, now obviously the shapes NEVER change so C does not expect you types to change either (and rightly calls it an error), the values (or objects to fit in the holes in this example) must match for it to fit perfectly, if an object happens to fit in a hole (e.g. a small cube into a larger circular hole) then in C it can be cast to that type but if you don't know what you're doing it would be like throwing mud into drinking water, you make it unfit for purpose, without the cast the compiler stops you from doing such an act because it doesn't know if it was intentional.

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