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

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    662

    error subscripted value is neither an array nor pointer nor vector

    im trying to get my head around linked lists and its completely mystifying me my test code wont even compile i get the above error on all the parameters of the second printf and a warning about incompatible pointer type when i try to make the next point to the first element (copied straight out of the book)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct
    {
        int door_number;
        char *p_street_name;
        char *p_city;
        struct Address *next;
    }Address;
    
    int main()
    {
        Address *first = NULL;
        Address *new_address;
        Address first_address;
    
    
        first_address.door_number = 4;
        first_address.p_street_name = "some street";
        first_address.p_city = "some town";
        printf("door number = %d street = %s city = %s\n", first_address.door_number,
                                    first_address.p_street_name, first_address.p_city);
    
        new_address = malloc(sizeof(Address));
        if (!new_address)
        {
             return 1;
        }
        new_address->door_number = 2;
        new_address->p_street_name = "some other street";
        new_address->p_city = "some other town";
        new_address->next = first;
        first = new_address;
    
        printf("door number = %d street = %s city = %s\n", first_address[1].door_number,
                                    first_address[1].p_street_name, first_address[1].p_city);
    
        free(first);
        free(new_address);
    
        return 0;
    }
    coop
    Last edited by cooper1200; 05-23-2019 at 01:05 PM.

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    534
    I don't know... maybe because it is neither an array, nor a pointer? Who knows?

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    Quote Originally Posted by flp1969 View Post
    I don't know... maybe because it is neither an array, nor a pointer? Who knows?
    i certainly don't as im knew to this so rather than being sarcastic because you couldn't come up with some convoluted way of doing it either with bitwise operators or double pointers to re allocated memory in some obscurely written function how about not wasting the effort of typing out some meaningless unhelpful drivel and point me in the right direction

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    534
    Quote Originally Posted by cooper1200 View Post
    i certainly don't as im knew to this so rather than being sarcastic because you couldn't come up with some convoluted way of doing it either with bitwise operators or double pointers to re allocated memory in some obscurely written function how about not wasting the effort of typing out some meaningless unhelpful drivel and point me in the right direction
    Ok... there are 3 errors in lines 36-37 and another one in lines 39-40... good luck.

  5. #5
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    619
    Quote Originally Posted by cooper1200 View Post
    Address *first = NULL;
    Address *new_address;
    Address first_address;
    First look at how you're declaring each variable and notice the difference between what they create, another thing is that you're calling free() on the same pointer twice, I'm surprised your app doesn't crash at that point

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,452
    And don't forget about the error on line 33 as well.


    Hint: For your printf() problems, look closely at line 16.

    I'm surprised your app doesn't crash at that point
    Why are you surprised? The code doesn't compile so how can it crash?

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    aswdert it doesn't crash at any point because it doesn't compile

  8. #8
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    619
    Quote Originally Posted by cooper1200 View Post
    aswdert it doesn't crash at any point because it doesn't compile
    Forgot about that, also might wanna stop using auto complete until you understand your error before trying to free the pointer

  9. #9
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    Quote Originally Posted by flp1969 View Post
    Ok... there are 3 errors in lines 36-37 and another one in lines 39-40... good luck.
    yet another comment with no useful information i already know where the errors are as i detailed in my original post

  10. #10
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    Quote Originally Posted by jimblumberg View Post
    And don't forget about the error on line 33 as well.


    Hint: For your printf() problems, look closely at line 16.

    Why are you surprised? The code doesn't compile so how can it crash?
    i appreciate that i only declare one instance of the struct Address and call it first address. however i then create (or at least i intend to) a new instance of Address and call it new address. As i now have two instances of Address surely that is equivalent to writing first_address[2]

    and im not surprised by it not crashing that was in response to someone else's comment saying they were surprised it doesn't crashc.
    coop

  11. #11
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    619
    Quote Originally Posted by cooper1200 View Post
    yet another comment with no useful information i already know where the errors are as i detailed in my original post
    I suggest you start by moving the code that fills each variable into their own functions e.g
    Code:
     Address FillAddress1(void);
    Address FillAddress2(void);
    Address FillAddress3(void);
    You then will have an easier time spotting the problem because each of these functions will fill their own local variable and return that on the stack or wherever the compiler puts it, each variable in main() can then be filled like this depending on how it is declared
    Code:
    one = FillAddress1();
    *two = FillAddress2();
    three[0] = FillAddress3();

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,452
    i appreciate that i only declare one instance of the struct Address and call it first address. however i then create (or at least i intend to) a new instance of Address and call it new address.
    Okay. But do you realize that that "new instance" is a totally different entity?

    As i now have two instances of Address surely that is equivalent to writing first_address[2]
    Surely not. A single instance is not an array.

  13. #13
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    ok i have had another bash at it.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct
    {
        int door_number;
        char *p_street_name;
        char *p_city;
        struct Address *next;
    }Address;
    
    int main()
    {
        Address *first = NULL;
        Address *new_address = NULL;
        Address *t;
        new_address = malloc(sizeof(Address));
    
        if (!new_address)
        {
            return 1;
        }
        new_address->door_number = 3;
        new_address->p_street_name = "some street";
        new_address->p_city = "some town";
        new_address->next = NULL;
    
        first = new_address;
    
        new_address = malloc(sizeof(Address));
    
        if (!new_address)
        {
            return 1;
        }
        new_address->door_number = 3;
        new_address->p_street_name = "some other street";
        new_address->p_city = "some other town";
        new_address->next = NULL;
        first->next = new_address;
    
        t = first;
        while (t)
        {
            printf("door number = %d street = %s city = %s\n", t->door_number, t->p_street_name, t->p_city);
            t = t->next;
        }
    
        free(new_address);
        free(first);
    
        return 0;
    }
    im now even more confused i have 2 warnings about incompatible pointer type lines 40 and 46. which is baffleing me as line 29 works (at least no errors or warnings)

    furthermore dot com i fail to see what this achieves as i now cant add any more "elements" as first points to the first instance and first->next is intended to point to the next instance (with the warning message it clearly doesn't). but after that its useless as i have no way of adding another node unless i declare another pointer that points at the second node so i can add a third and another pointer that points at the third instance so i can add a 4th etc etc. all this is done away with simply creating an array of the stuct in the first place as everything is hard coded.
    Last edited by cooper1200; 05-23-2019 at 04:38 PM.

  14. #14
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    Quote Originally Posted by jimblumberg View Post
    Okay. But do you realize that that "new instance" is a totally different entity?


    Surely not. A single instance is not an array.
    maybe this is where i am getting confused.

    if i wrote Address[2] i have an array of 2 structs called address and could store 2 different sets of information.
    if i understand what your saying correctly that is different to having a single deceleration of Address and dynamically creating another even though the net result is the same ie i can store two different sets of information.
    coop

  15. #15
    Registered User
    Join Date
    Feb 2019
    Posts
    534
    Code:
    //             missing
    //             __tag__
    typedef struct Address
    {
      int door_number;
      char *p_street_name;
      char *p_city;
      struct Address *next;
    } Address;

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