Thread: bit abstract data type

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    118

    bit abstract data type

    hi a problem put in elements into my array of contents for my adt
    Code:
    /*  * File:   main.c
     * Author:
     *
     * Created on November 20, 2011, 10:04 AM
     */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    /*
     * 
     */
    
    
    
    
    struct bitset{
       int size;
       unsigned char *contents;
        
    
    
    
    
    };
    
    
    struct bitset * bitset_new(int size){
     //   return -1;
       struct bitset *p;
        p == malloc(sizeof(*p));
        p->contents= malloc((sizeof(char) * size/8) + (size%8)?0:1);
        return p;
        
        
    }
    
    
    // add an item, with number 'item' to the set
    // (returns 0 if item is out of bounds, 1 otherwise)
    // has no effect if the item is already in the set
    int bitset_add(struct bitset * this)
    {
        return 1;
         char *p;
        printf ("Please enter a line of text, max %d characters\n", 
        sizeof(this->contents));
        
      if (fgets(this->contents, sizeof(this->contents), stdin) != NULL)
      {
        printf ("Thank you, you entered >%s<\n", this->contents);
        
      }
        if ((p = strchr(this->contents, '\n')) != NULL)
          *p = '\0';
          
        //return 0;
    }
    
    
    
    
    
    
    int main(int argc, char** argv) {
    
    
        //unsigned char a='a';
       // unsigned char b='b';
       // unsigned int m=1;
       // unsigned int j=0;
       // int c=m |j;
      //  int e= sizeof(char);
        
        struct bitset *p= bitset_new(26);
        bitset_add(&p);
     
        
      
      
        
        return (EXIT_SUCCESS);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    At a glance, this is wrong:
    Code:
    p == malloc(sizeof(*p));
    It should be:
    Code:
    p = malloc(sizeof(*p));
    By the way, bitset_add's description is strange: add an item to the set?
    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. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by laserlight View Post
    At a glance, this is wrong:
    Code:
    p == malloc(sizeof(*p));
    It should be:
    Code:
    p = malloc(sizeof(*p));
    By the way, bitset_add's description is strange: add an item to the set?
    ye changed the bit set add prototype so am taking the elements in as a buffer

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sigur47
    ye changed the bit set add prototype so am taking the elements in as a buffer
    If you mean something like this:
    Code:
    int bitset_add(struct bitset *this, unsigned char *buffer);
    Then yeah, I can imagine that buffer would be a string of 0s and 1s, or maybe a numeric string whose value as a number is to be converted to the elements of a bitset.
    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

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by laserlight View Post
    If you mean something like this:
    Code:
    int bitset_add(struct bitset *this, unsigned char *buffer);
    Then yeah, I can imagine that buffer would be a string of 0s and 1s, or maybe a numeric string whose value as a number is to be converted to the elements of a bitset.
    thank you would go work on it now .

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    hi laserlight quick question i am having two errors that i am trying to figure out.
    1. where am initialising the content with malloc is giving me an error invalid conversion from void to unsigned char
    2 the line where i initiliase my new pointer to a size is also giving me an error invalid conversion from void to bitset

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sigur47 View Post
    hi laserlight quick question i am having two errors that i am trying to figure out.
    1. where am initialising the content with malloc is giving me an error invalid conversion from void to unsigned char
    2 the line where i initiliase my new pointer to a size is also giving me an error invalid conversion from void to bitset
    You are using a C++ compiler on C code.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamically allocated array of an abstract type
    By Zerok in forum C++ Programming
    Replies: 4
    Last Post: 11-24-2008, 06:50 AM
  2. Abstract Data Type(ADT)
    By jack1234 in forum C Programming
    Replies: 5
    Last Post: 12-08-2007, 01:34 PM
  3. What are abstract data types
    By bhagwat_maimt in forum C++ Programming
    Replies: 4
    Last Post: 01-04-2007, 10:43 AM
  4. Need help with ADT (abstract data types)
    By ortegac in forum C Programming
    Replies: 1
    Last Post: 03-30-2006, 02:23 AM
  5. Abstract Data Type
    By Thantos in forum C Programming
    Replies: 11
    Last Post: 11-29-2003, 04:17 PM

Tags for this Thread