Error in pointer conversion

This is a discussion on Error in pointer conversion within the C Programming forums, part of the General Programming Boards category; I am simply trying to create a vector but as soon as i compile I get this error message "base ...

  1. #1
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    593

    Error in pointer conversion

    I am simply trying to create a vector but as soon as i compile I get this error message

    "base operand of -> has non pointer type VectorT"

    I cant seem to find a way around this, any help is greatly appreciated


    Code:
    typedef char NodeItemT;
    
    typedef struct
    {
    	NodeItemT info;
    } NodeT;
    
    typedef struct
    {
    	NodeT *list;
    	int   capacity;
    	int   size;
    } VectorT;
    
    NodeT *newNode(NodeItemT item);
    VectorT *new1(int cap);
    
    int main()
    {
        
     getch();
     return 0;   
    }
    
    
    NodeT *newNode(NodeItemT item)
    {
    	NodeT* node;
    
    	node = (NodeT*) malloc (sizeof(NodeT));
        node->info=item;
    	return node;
    }
    
    VectorT *new1(int cap)
    {
    VectorT *init;
    init=(VectorT*)malloc (15*sizeof(VectorT));
    
    int i;
    for(i=0;i<15;i++)
    {
       init[i]->list->info='\0'; // Error here
       init[i]->capacity=cap; //Error here
       init[i]->size=0;     //Error here
    }
       
    return init;
            
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Init is a pointer to a vector... if you want to create an array of pointers you need to use...

    Code:
    init=(VectorT*)malloc (15*sizeof(*init);)
    
    // OR
    
    
    init=(VectorT*)malloc (15*sizeof(*VectorT));
    You want the size of the pointer... not the size of the struct.

  3. #3
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    593
    I tried those and I still get the same error

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,462
    Quote Originally Posted by CommonTater View Post
    Code:
     init=(VectorT*)malloc (15*sizeof(*VectorT));
    Methinks this wont work. One can not dereference a type name. I think you just meant sizeof(VectorT) without the *

    Quote Originally Posted by camel-man View Post
    "base operand of -> has non pointer type VectorT"
    The -> is the member access operand for a pointer to a struct. So what it's saying is that whatever is on the left of a -> is not a pointer to a struct. You declare init as a VectorT *, but you treat it as an array, so init[i] gives you back a VectorT, not a pointer to a VectorT. Thus, you need
    Code:
        init[i].list->info = NULL; // You don't want the nul character '\0', you probably want NULL, the null pointer constant
        init[i].capacity = cap;
        init[i].size = 0;

  5. #5
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    593
    If i could kiss you i would

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,462
    Quote Originally Posted by camel-man View Post
    If i could kiss you i would
    Flattered, but given your user name, I think I'll pass. Now, if your user name were really_hot_super_model_chick_with_no_STDs, then I might just take you up on that offer

  7. #7
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    593
    haha ok well the offer stands

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what does this pointer type conversion do?
    By casmac in forum C Programming
    Replies: 4
    Last Post: 06-02-2010, 11:04 AM
  2. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 07:33 AM
  3. Copy constructor error, 'this' pointer conversion
    By bennyandthejets in forum C++ Programming
    Replies: 6
    Last Post: 01-08-2004, 04:18 PM
  4. Suspicious Pointer Conversion
    By mr_spanky202 in forum C Programming
    Replies: 35
    Last Post: 04-11-2003, 12:35 PM
  5. help me in conversion using pointer
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 08-20-2002, 12:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21