really need help!!!

Discussion in 'C Programming' started by ligerdave, Oct 29, 2005.

  1. ligerdave

    ligerdave Guest

    i have the following code:

    /* Created by Anjuta version 1.2.2 */
    /* This file will not be overwritten */

    #include <stdio.h>


    #define LEN sizeof(struct hash_table)

    struct hash_table{
    int value;
    char *key;
    struct hash_table *next;
    }

    struct hash_table* allocate_hash_table(int size){
    struct hash_table* head;
    head = (struct hash_table*) calloc(size, LEN);
    return head;
    }

    int main()
    {
    return (0);
    }

    and i always get error which says: two or more data types in
    declaration of 'allocate_hash_table'


    can anyone tell me whats going wrong?
     
    ligerdave, Oct 29, 2005
    #1
    1. Advertising

  2. ligerdave

    Amphibient Guest

    You might want to consider a semicolon at the end of your struct. Your
    compiler is thinking that you are declaring 'allocate_hash_table' as:

    struct hash_table { /* decl */ } struct hash_table *
    allocate_hash_table(int size) { /*decl */ }

    when it should be:

    struct hash_table { /* decl */ };
    struct hash_table * allocate_hash_table(int size) { /*decl */ }
     
    Amphibient, Oct 29, 2005
    #2
    1. Advertising

  3. ligerdave

    Ben Pfaff Guest

    "ligerdave" <> writes:

    > struct hash_table{
    > int value;
    > char *key;
    > struct hash_table *next;
    > }


    Insert ; here

    > struct hash_table* allocate_hash_table(int size){



    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
     
    Ben Pfaff, Oct 29, 2005
    #3
  4. ligerdave

    ligerdave Guest

    yes! that worked out!!! thanks a lot
     
    ligerdave, Oct 29, 2005
    #4
  5. ligerdave

    Mike Wahler Guest

    "ligerdave" <> wrote in message
    news:...
    >i have the following code:
    >
    > /* Created by Anjuta version 1.2.2 */
    > /* This file will not be overwritten */
    >
    > #include <stdio.h>
    >
    >
    > #define LEN sizeof(struct hash_table)
    >
    > struct hash_table{
    > int value;
    > char *key;
    > struct hash_table *next;
    > }


    You forgot to terminate your struct definition with
    a semicolon:

    };

    >
    > struct hash_table* allocate_hash_table(int size){


    ... which makes the compiler think your function tries
    to declare more than one return type ('struct hash_table'
    and 'struct hash_table *')


    > struct hash_table* head;
    > head = (struct hash_table*) calloc(size, LEN);
    > return head;
    > }
    >
    > int main()
    > {
    > return (0);
    > }
    >
    > and i always get error which says: two or more data types in
    > declaration of 'allocate_hash_table'
    >
    >
    > can anyone tell me whats going wrong?


    See above.

    If this code was produced by a code generator, it has
    a bug.

    -Mike
     
    Mike Wahler, Oct 29, 2005
    #5
  6. "ligerdave" <> writes:
    > i have the following code:
    >
    > /* Created by Anjuta version 1.2.2 */
    > /* This file will not be overwritten */
    >
    > #include <stdio.h>
    >
    >
    > #define LEN sizeof(struct hash_table)
    >
    > struct hash_table{
    > int value;
    > char *key;
    > struct hash_table *next;
    > }
    >
    > struct hash_table* allocate_hash_table(int size){
    > struct hash_table* head;
    > head = (struct hash_table*) calloc(size, LEN);
    > return head;
    > }
    >
    > int main()
    > {
    > return (0);
    > }
    >
    > and i always get error which says: two or more data types in
    > declaration of 'allocate_hash_table'


    Others have pointed out the missing semicolon. I have some other
    comments.

    The macro LEN isn't really useful. You only use it once, and it would
    be clearer to use sizeof explicitly.

    You should never cast the result of calloc() or malloc(). It can mask
    the error of failing to add a "#include <stdlib.h>" -- which you also
    need. (You also don't need the "#include <stdio.h>", since the
    program doesn't do any I/O, but presumably it's not a complete
    program.)

    The difference between malloc() and calloc() (other than a different
    way of specifying the size) is that calloc() initializes the allocated
    space to all-bits-zero. Since the structure contains pointers,
    setting it to all-bits-zero isn't necessarily meaningful; it makes
    more sense to initialize them to null pointers (and a null pointer is
    *not* necessarily all-bits-zero).

    A simple malloc() will allocate the space without initializing it; you
    can then initialize the structures manually if necesary.

    And a couple of minor points: "int main()" is allowed, but
    "int main(void)" is preferred, and you don't need parentheses on the
    return statement.

    Here's a modified version of your program:

    struct hash_table {
    int value;
    char *key;
    struct hash_table *next;
    };

    struct hash_table *allocate_hash_table(int size)
    {
    struct hash_table *head;
    head = malloc(size * sizeof *head);
    return head;
    }

    int main(void)
    {
    return 0;
    }

    You should always check whether malloc() succeeded. In this case, it
    may be acceptable for allocate_hash_table() to return the result of
    malloc() without checking it, and let the caller check whether
    allocate_hash_table() returned a null pointer. But if
    allocate_hash_table() is going to initialize the allocated memory, it
    *must* check the result of malloc() before doing so.

    What to do if malloc() fails is another matter. You may sometimes be
    able to recover somehow, but the simplest thing to do is to abort the
    program with an error message -- which is better than continuing
    blindly with an invalid pointer.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Oct 29, 2005
    #6
  7. In article <>,
    Ben Pfaff <> wrote:

    >> struct hash_table{
    >> int value;
    >> char *key;
    >> struct hash_table *next;
    >> }

    >
    >Insert ; here
    >
    >> struct hash_table* allocate_hash_table(int size){


    This is such a common mistake that compilers really ought to give
    a better error message than the one quoted (which is gcc's, I think).

    -- Richard
     
    Richard Tobin, Oct 30, 2005
    #7
  8. ligerdave

    Doro Guest

    >You should never cast the result of calloc() or malloc(). It can mask
    >the error of failing to add a "#include <stdlib.h>"


    Hi, why this?
    Thanks.
     
    Doro, Oct 30, 2005
    #8
  9. ligerdave

    Greg Comeau Guest

    In article <dk10eb$19nu$>,
    Richard Tobin <> wrote:
    >In article <>,
    >Ben Pfaff <> wrote:
    >
    >>> struct hash_table{
    >>> int value;
    >>> char *key;
    >>> struct hash_table *next;
    >>> }

    >>
    >>Insert ; here
    >>
    >>> struct hash_table* allocate_hash_table(int size){

    >
    >This is such a common mistake that compilers really ought to give
    >a better error message than the one quoted (which is gcc's, I think).


    http://www.comeaucomputing.com/tryitout
    --
    Greg Comeau / Celebrating 20 years of Comeauity!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Oct 30, 2005
    #9
  10. ligerdave

    Greg Comeau Guest

    In article <>,
    Doro <> wrote:
    >>You should never cast the result of calloc() or malloc(). It can mask
    >>the error of failing to add a "#include <stdlib.h>"

    >
    >Hi, why this?


    Because C90 does not require function prototypes, so in their
    absence it will synthesize one, and the assumptions made there
    might be incorrect compared to what it should actually be.
    That means wrong things might begin to get tossed about,
    when say int and void * are internally different on a platform.
    --
    Greg Comeau / Celebrating 20 years of Comeauity!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Oct 30, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Amir
    Replies:
    3
    Views:
    609
  2. nc
    Replies:
    1
    Views:
    511
    nice.guy.nige
    Feb 3, 2005
  3. Replies:
    2
    Views:
    344
  4. =?Utf-8?B?Q2hyaXM=?=

    Help Help. I really need some help with this

    =?Utf-8?B?Q2hyaXM=?=, Jan 31, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    581
    =?Utf-8?B?SmFzb24gVmVybWlsbGlvbg==?=
    Jan 31, 2007
  5. Replies:
    30
    Views:
    1,010
    Arne Vajhøj
    Feb 10, 2008
Loading...

Share This Page