Thread: pointer and struct

  1. #16
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Quote Originally Posted by misplaced
    lol........i love it...forget the inline keyword.....just go ahead and put your functions inline


    >TRY koo= (struct p *) malloc(sizeof(p));
    Lose the cast, it only serves to hide more subtle errors. And it should be:
    Code:
    koo = malloc ( sizeof ( struct p ) );
    Because p is not a typedef'd struct, you must have the struct keyword unless compiling as C++ by accident. Though the OP had the right of it with this:
    Code:
    koo = malloc ( sizeof *koo );
    My best code is written with the delete key.

  2. #17
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by Prelude
    Code:
     koo = malloc ( sizeof *koo );
    Are you sure about this? GCC gives me a warning about this, even without any -W* parameters used.

    Code:
     typedef struct {
       int a;
     } TTest;
     
     int main() {
       TTest *ptTest;
       ptTest= malloc(sizeof(TTest));
     
       ptTest->a= 1;
       return ptTest->a;
     }
    Code:
     nyda, nyda/ $ LC_ALL="C" gcc test.c -o test
     test.c: In function `main':
     test.c:7: warning: assignment makes pointer from integer without a cast
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > test.c:7: warning: assignment makes pointer from integer without a cast
    Which is precisely the reason for removing the cast in the first place.
    The correct fix is to include stdlib.h to make the message go away.
    The incorrect fix is to cast it to make the message go away.

    Further, if you get messages which complain about void* conversions when using malloc, it means you're compiling your C code with a C++ compiler. The fix in this case is to either use a C compiler, or continue with C++ and replace the malloc call with a 'new' call (and replace free() with delete)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #19
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by Salem
    > test.c:7: warning: assignment makes pointer from integer without a cast
    Which is precisely the reason for removing the cast in the first place.
    The correct fix is to include stdlib.h to make the message go away.
    The incorrect fix is to cast it to make the message go away.
    Thanks Salem. I was already wondering why it was saying "integer without a cast". So I just forgot to include the malloc definition .
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  5. #20
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    >I was already wondering why it was saying "integer without a cast".
    That is a sign that a function is being used before its declaration. Without a declaration, functions are assumed to return int, so you can use such a warning with confidence in what the problem is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM