Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > really need help!!!

Reply
Thread Tools

really need help!!!

 
 
ligerdave
Guest
Posts: n/a
 
      10-29-2005
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?

 
Reply With Quote
 
 
 
 
Amphibient
Guest
Posts: n/a
 
      10-29-2005
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 */ }

 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      10-29-2005
"ligerdave" <(E-Mail Removed)> 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[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\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[i]\
);}return 0;}
 
Reply With Quote
 
ligerdave
Guest
Posts: n/a
 
      10-29-2005
yes! that worked out!!! thanks a lot

 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      10-29-2005

"ligerdave" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>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


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-29-2005
"ligerdave" <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      10-29-2005
In article <(E-Mail Removed)>,
Ben Pfaff <(E-Mail Removed)> 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
 
Reply With Quote
 
Doro
Guest
Posts: n/a
 
      10-29-2005
>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.

 
Reply With Quote
 
Greg Comeau
Guest
Posts: n/a
 
      10-30-2005
In article <dk10eb$19nu$(E-Mail Removed)>,
Richard Tobin <(E-Mail Removed)> wrote:
>In article <(E-Mail Removed)>,
>Ben Pfaff <(E-Mail Removed)> 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?
 
Reply With Quote
 
Greg Comeau
Guest
Posts: n/a
 
      10-30-2005
In article <(E-Mail Removed). com>,
Doro <(E-Mail Removed)> 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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I really need help with this so if anyone can help me out that wouldbe really great of you. adityagaddam90@gmail.com Java 30 02-10-2008 11:29 PM
do we really-really need Photoshop Elements? Giovanni Azua Digital Photography 6 05-18-2005 04:22 PM
OT : But help really really needed re: Domain Name selling, hosting etc. problem nc HTML 1 02-03-2005 07:24 PM
REALLY REALLY WERID PROBLEM!!!!pls take a look Amir ASP .Net 3 01-23-2004 06:01 PM
really really mysterious IE6 problem--secure site ultraviolet353 Computer Support 7 11-22-2003 07:56 PM



Advertisments