Wot am I doing rong?? 
Author Message
 Wot am I doing rong??

Hi,
Strictly C question!

Can someone plz tell wot I'm doing wrong?

The code is fine except compiler gives these messages:

Compiling TRANS.CPP:
Error TRANS.CPP 26: Cannot convert 'void *' to 'transactions *' in function
read_transactions(transactions *,const char *)
Error TRANS.CPP 34: Cannot convert 'void *' to 'transactions *' in function
read_transactions(transactions *,const char *)

These are the malloc lines, as highlighted.

can someone plz help,

cheers,

arez.

*********************************************
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#define MAX 80

typedef struct transactions{
      int account_num;
      char name[MAX];
      int amount;
      float balance;
      struct transactions *next;
         } Transnode;

Transnode *read_transactions ( Transnode *head, const char *filename)
{
 Transnode *tmp_ptr, *new_trans;
 FILE *fptr;

 if ( (fptr = fopen(filename, "r")) == NULL)
 {
   perror("file open");
   exit(1);
 }

   new_trans = malloc ( sizeof(Transnode) );

 while ( fscanf( fptr, "%d:%[^:]:%d:%f\n",  &new_trans->account_num,
&new_trans->name,
                &new_trans->amount, &new_trans->balance) ==4)
 {
   tmp_ptr = head;
   head = new_trans;
   new_trans->next = tmp_ptr;
   new_trans = malloc( sizeof(Transnode) );
 }

 free (new_trans);
 fclose(fptr);
 return(head);

Quote:
}

int show_transactions(Transnode *head)
{
 Transnode *tmp_ptr;
 int count=0;

 tmp_ptr = head;
 while( tmp_ptr )
 {
   printf("%d:$s:$d:%f\n", tmp_ptr->account_num,
           tmp_ptr->name,
           tmp_ptr->amount,
           tmp_ptr->balance);
   tmp_ptr = tmp_ptr->next;
   count++;
 }
 return count;

Quote:
}

int main()
{
 char filename[MAX];
 Transnode *head = (Transnode *) NULL;
 int count=0;

 printf("Enter a filename: ");
 scanf("%s", filename);

 head = read_transactions(head, filename);
 count = show_transactions(head);
 printf("\n%d transactions in total\n", count);
   return 0;

Quote:
}

--



Sun, 14 Oct 2001 03:00:00 GMT  
 Wot am I doing rong??

serve.co.uk> writes

Quote:
>Hi,
>Strictly C question!

No it isn't.  You are using a C++ compiler :)  And C++, unlike C, does
not allow implicit conversions from void * to any other type of pointer.
malloc returns a void * which you must cast (or use a C compiler)

Quote:

>Can someone plz tell wot I'm doing wrong?

>The code is fine except compiler gives these messages:

>Compiling TRANS.CPP:
>Error TRANS.CPP 26: Cannot convert 'void *' to 'transactions *' in function
>read_transactions(transactions *,const char *)
>Error TRANS.CPP 34: Cannot convert 'void *' to 'transactions *' in function
>read_transactions(transactions *,const char *)

>These are the malloc lines, as highlighted.

>can someone plz help,

>cheers,

Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Tue, 16 Oct 2001 03:00:00 GMT  
 Wot am I doing rong??
On Wed, 28 Apr 1999 22:48:02 GMT, "arez"

Quote:

>Strictly C question!

Not quite. You're writing C, but compiling C++.

...

Quote:
>Error TRANS.CPP 26: Cannot convert 'void *' to 'transactions *' in function
...
>read_transactions(transactions *,const char *)
...
>   new_trans = malloc( sizeof(Transnode) );

...

Unlike C, C++ does not allow implicit conversions from generic
pointers (void *) to other pointer types. Some regard this as a flaw.

To fix your problem, either add the necessary casts, or put your
compiler into C mode, for example by changing the file extension from
.cpp to .c.

-- Mat.

--



Tue, 16 Oct 2001 03:00:00 GMT  
 Wot am I doing rong??
On Wed, 28 Apr 1999 22:48:02 GMT, "arez"

Quote:
> Hi,
> Strictly C question!

No it's not.  When you name your file with a .cpp extension your
compiler compiles it as C++, and the rules for using malloc() are
different.  Compile the file as C (.c extension) and it will go away.

Quote:

> Can someone plz tell wot I'm doing wrong?

> The code is fine except compiler gives these messages:

> Compiling TRANS.CPP:
> Error TRANS.CPP 26: Cannot convert 'void *' to 'transactions *' in function
> read_transactions(transactions *,const char *)
> Error TRANS.CPP 34: Cannot convert 'void *' to 'transactions *' in function
> read_transactions(transactions *,const char *)

> These are the malloc lines, as highlighted.

> can someone plz help,

> cheers,

> arez.

> *********************************************
> #include <stdio.h>
> #include <stdlib.h>
> #include <errno.h>

> #define MAX 80

> typedef struct transactions{
>       int account_num;
>       char name[MAX];
>       int amount;
>       float balance;
>       struct transactions *next;
>          } Transnode;

> Transnode *read_transactions ( Transnode *head, const char *filename)
> {
>  Transnode *tmp_ptr, *new_trans;
>  FILE *fptr;

>  if ( (fptr = fopen(filename, "r")) == NULL)
>  {
>    perror("file open");
>    exit(1);
>  }

>    new_trans = malloc ( sizeof(Transnode) );

>  while ( fscanf( fptr, "%d:%[^:]:%d:%f\n",  &new_trans->account_num,
> &new_trans->name,
>                 &new_trans->amount, &new_trans->balance) ==4)
>  {
>    tmp_ptr = head;
>    head = new_trans;
>    new_trans->next = tmp_ptr;
>    new_trans = malloc( sizeof(Transnode) );
>  }

>  free (new_trans);
>  fclose(fptr);
>  return(head);
> }

> int show_transactions(Transnode *head)
> {
>  Transnode *tmp_ptr;
>  int count=0;

>  tmp_ptr = head;
>  while( tmp_ptr )
>  {
>    printf("%d:$s:$d:%f\n", tmp_ptr->account_num,
>            tmp_ptr->name,
>            tmp_ptr->amount,
>            tmp_ptr->balance);
>    tmp_ptr = tmp_ptr->next;
>    count++;
>  }
>  return count;
> }

> int main()
> {
>  char filename[MAX];
>  Transnode *head = (Transnode *) NULL;
>  int count=0;

>  printf("Enter a filename: ");
>  scanf("%s", filename);

>  head = read_transactions(head, filename);
>  count = show_transactions(head);
>  printf("\n%d transactions in total\n", count);
>    return 0;
> }

--



Tue, 16 Oct 2001 03:00:00 GMT  
 Wot am I doing rong??

Quote:

> Compiling TRANS.CPP:
> Error TRANS.CPP 26: Cannot convert 'void *' to 'transactions *' in function
> read_transactions(transactions *,const char *)
> Error TRANS.CPP 34: Cannot convert 'void *' to 'transactions *' in function
> read_transactions(transactions *,const char *)

It seems you're compiling the program as C++, not C.

You need to tell the compiler that the program is indeed C.
Renaming the file to trans.c might help; if not, see your
compiler documentation.

By the way, gcc -Wall -W (egcs-2.91.63) gives the following
warnings you should perhaps investigate:

trans.c: In function `read_transactions':
trans.c:30: warning: char format, different type arg (arg 4)
trans.c: In function `show_transactions':
trans.c:54: warning: double format, pointer arg (arg 3)
trans.c:54: warning: too many arguments for format
--



Tue, 16 Oct 2001 03:00:00 GMT  
 Wot am I doing rong??
Wed, 28 Apr 1999 22:48:02 GMT,

Quote:
> Hi,
> Strictly C question!

> Can someone plz tell wot I'm doing wrong?

> The code is fine except compiler gives these messages:

> Compiling TRANS.CPP:
> Error TRANS.CPP 26: Cannot convert 'void *' to 'transactions *' in function
> read_transactions(transactions *,const char *)
> Error TRANS.CPP 34: Cannot convert 'void *' to 'transactions *' in function
> read_transactions(transactions *,const char *)

> These are the malloc lines, as highlighted.

The compiler probably thinks your code is C++ because of the
.CPP extension.  malloc() returns void* and in C, this converts
to any object (or incomplete) pointer type, but not in C++.  
Either name the file .C or look for an option that tells
the compiler to ignore the extension and work in C mode.  

(CCed) david.thompson at but not for trintech.com
--



Tue, 16 Oct 2001 03:00:00 GMT  
 Wot am I doing rong??

Quote:
>   new_trans = malloc ( sizeof(Transnode) );
>   new_trans = malloc( sizeof(Transnode) );

You should do some kind of casting. Because malloc() returns a pointer of
void:

    new_trans = (Transnode *)malloc(sizeof(Transnode));

Regard

  G.Pohl

--



Tue, 16 Oct 2001 03:00:00 GMT  
 Wot am I doing rong??

: Strictly C question!

Nope, it's a C++ question ;-)

: Can someone plz tell wot I'm doing wrong?

: The code is fine except compiler gives these messages:

: Compiling TRANS.CPP:
------------------^^^

That is a funny extension for a C program.  You are likely using a C/C++
compiler which concludes that this is a C++ program from the file
extension.

: Error TRANS.CPP 26: Cannot convert 'void *' to 'transactions *' in function
: read_transactions(transactions *,const char *)
: Error TRANS.CPP 34: Cannot convert 'void *' to 'transactions *' in function
: read_transactions(transactions *,const char *)

: These are the malloc lines, as highlighted.

Those are C++ errors.  In C++, an explicit cast is required here.  There
is a standard conversion to void* but not from void*.

I leave it to the C experts to advise you to change the file extension
to get a C compiler or add the cast to get additional C++ checking which
may catch some logic errors which appear as C++ errors.

John

--



Tue, 16 Oct 2001 03:00:00 GMT  
 Wot am I doing rong??

Quote:

> Strictly C question!

No! actually the problem is caused you your use of C++ instead of C.

Quote:
> The code is fine except compiler gives these messages:
> [...]
> These are the malloc lines, as highlighted.

In C, there would be implicit conversion, but you're using C++ on
this .CPP file, and ANSI C++ forbids this implicit conversion. Solution:
use C or convert explicitly by:

  new_trans = (struct transactions *)malloc (sizeof (Transnode));

etc.

Also:

Quote:
>    printf("%d:$s:$d:%f\n", tmp_ptr->account_num,
>            tmp_ptr->name,
>            tmp_ptr->amount,
>            tmp_ptr->balance);

These $s and $d must be %s and %d, otherwise you're putting a float in a string.
Cheers, D.C.
--

--



Tue, 16 Oct 2001 03:00:00 GMT  
 Wot am I doing rong??


Quote:
> Strictly C question!

> Can someone plz tell wot I'm doing wrong?

> The code is fine except compiler gives these messages:

> Compiling TRANS.CPP:
> Error TRANS.CPP 26: Cannot convert 'void *' to 'transactions *' in function
> read_transactions(transactions *,const char *)
> Error TRANS.CPP 34: Cannot convert 'void *' to 'transactions *' in function
> read_transactions(transactions *,const char *)

[...]

The problem here is quite simple: because you're using a .CPP file
extension, your compiler thinks you're writing in C++, not C, and so is
using C++ semantics. C++ forbids the implicit casting of void* to any
pointer type. C++ is subtly different from C; this is the reason why the
compiler is referring to "transactions*" and not "Transnode*" (in C++,
structures and typedefs occupy the same namespace).

Try renaming the file to trans.c and trying again.

Alternatively, you could use an explicit cast:

        new_trans = (Transnode*)malloc(sizeof(Transnode));

This is good practice anyway.

--
+- David Given ---------------McQ-+ This is a test. This is only a test. Had


+- http://wired.st-and.ac.uk/~dg -+

--



Tue, 16 Oct 2001 03:00:00 GMT  
 Wot am I doing rong??

Quote:

> Hi,
> Strictly C question!

> Can someone plz tell wot I'm doing wrong?

> The code is fine except compiler gives these messages:

[snip]

Quote:
>    new_trans = malloc ( sizeof(Transnode) );

malloc has no idea what the type is of the memory you are requesting,
just how much.  You must cast it yourself.

new_trans = ( transaction* )malloc( sizeof( Transnode ) );

--
Bryan Van de Ven
Applied Research Labs
University of Texas, Austin
--



Tue, 16 Oct 2001 03:00:00 GMT  
 Wot am I doing rong??

Quote:
> The code is fine except compiler gives these messages:

> Compiling TRANS.CPP:
> Error TRANS.CPP 26: Cannot convert 'void *' to 'transactions *' in function
> read_transactions(transactions *,const char *)
> Error TRANS.CPP 34: Cannot convert 'void *' to 'transactions *' in function
> read_transactions(transactions *,const char *)
...
>  Transnode *tmp_ptr, *new_trans;
...
>    new_trans = malloc ( sizeof(Transnode) );
...
>    new_trans = malloc( sizeof(Transnode) );

malloc() returns void * and you are trying to assign the value to
a Transnode * variable. Although many compilers accept the code
and make the cast automatically you should better use an explicit
cast, like
new_trans = (Transnode *) malloc ( sizeof(Transnode) );
--

Faculty of Informatics, Masaryk University  
Brno, Czechia  16 deg 35'E, 49 deg 12' N
--



Tue, 16 Oct 2001 03:00:00 GMT  
 Wot am I doing rong??
Try
   new_trans = (transactions *) malloc ( sizeof(Transnode) );
This should cure it.

Quote:

> Hi,
> Strictly C question!

> Can someone plz tell wot I'm doing wrong?

> The code is fine except compiler gives these messages:

> Compiling TRANS.CPP:
> Error TRANS.CPP 26: Cannot convert 'void *' to 'transactions *' in function
> read_transactions(transactions *,const char *)
> Error TRANS.CPP 34: Cannot convert 'void *' to 'transactions *' in function
> read_transactions(transactions *,const char *)

> These are the malloc lines, as highlighted.

> can someone plz help,

> cheers,

> arez.

> *********************************************
> #include <stdio.h>
> #include <stdlib.h>
> #include <errno.h>

> #define MAX 80

> typedef struct transactions{
>       int account_num;
>       char name[MAX];
>       int amount;
>       float balance;
>       struct transactions *next;
>          } Transnode;

> Transnode *read_transactions ( Transnode *head, const char *filename)
> {
>  Transnode *tmp_ptr, *new_trans;
>  FILE *fptr;

>  if ( (fptr = fopen(filename, "r")) == NULL)
>  {
>    perror("file open");
>    exit(1);
>  }

>    new_trans = malloc ( sizeof(Transnode) );

>  while ( fscanf( fptr, "%d:%[^:]:%d:%f\n",  &new_trans->account_num,
> &new_trans->name,
>                 &new_trans->amount, &new_trans->balance) ==4)
>  {
>    tmp_ptr = head;
>    head = new_trans;
>    new_trans->next = tmp_ptr;
>    new_trans = malloc( sizeof(Transnode) );
>  }

>  free (new_trans);
>  fclose(fptr);
>  return(head);
> }

> int show_transactions(Transnode *head)
> {
>  Transnode *tmp_ptr;
>  int count=0;

>  tmp_ptr = head;
>  while( tmp_ptr )
>  {
>    printf("%d:$s:$d:%f\n", tmp_ptr->account_num,
>            tmp_ptr->name,
>            tmp_ptr->amount,
>            tmp_ptr->balance);
>    tmp_ptr = tmp_ptr->next;
>    count++;
>  }
>  return count;
> }

> int main()
> {
>  char filename[MAX];
>  Transnode *head = (Transnode *) NULL;
>  int count=0;

>  printf("Enter a filename: ");
>  scanf("%s", filename);

>  head = read_transactions(head, filename);
>  count = show_transactions(head);
>  printf("\n%d transactions in total\n", count);
>    return 0;
> }

> --


--------------------------------------------------------------------
|                          Kevin Roy                               |
|                                                                  |
| Address:  Manchester Computing         Telephone:  0161 275 7031 |
|           Oxford Road                        Fax:  0161 275 6040 |
|           Manchester                                             |

--------------------------------------------------------------------

--



Tue, 16 Oct 2001 03:00:00 GMT  
 
 [ 21 post ]  Go to page: [1] [2]

 Relevant Pages 

1. System.Threading.Timer , am I doing this correctly ?

2. Am I doing this right?

3. what am i doing wrong?

4. what am I doing wrong here?

5. What am I doing wrong?

6. What am I doing wrong?

7. WHAT AM I DOING WRONG?

8. what am i doing wrong!

9. What am I doing wrong (part 2)

10. What am i doing wrong?

11. What am i doing wrong in this code?

12. What am I doing wrong here

 

 
Powered by phpBB® Forum Software