Re: malloc & incompatible types in assignment

Discussion in 'C Programming' started by Dimitris Mandalidis, Aug 31, 2003.

  1. On Sun, 31 Aug 2003 15:44:42 +0300, Nejat AYDIN wrote

    > You cannot assign a pointer to a structure. You'd probably want


    Hmm I knew that the answer was simple :) But if I have to use Message foo and
    not Message *foo, does foo.something makes any sense?

    D.

    --
    PGP key 39A40276 at wwwkeys.eu.pgp.net
    key fingeprint: 9A0B 61C6 B826 4B73 69BB 972B C5E7 A153 39A4 0276
     
    Dimitris Mandalidis, Aug 31, 2003
    #1
    1. Advertising

  2. Dimitris Mandalidis

    Nejat AYDIN Guest

    Dimitris Mandalidis wrote:
    >
    > On Sun, 31 Aug 2003 15:44:42 +0300, Nejat AYDIN wrote
    >
    > > You cannot assign a pointer to a structure. You'd probably want

    >
    > Hmm I knew that the answer was simple :) But if I have to use Message foo and
    > not Message *foo, does foo.something makes any sense?


    I don't understand the question exactly. If you define
    Message foo;
    where Message a structure type, the memory for the foo is already
    supplied by the compiler, so you don't need, and in fact you cannot,
    allocate memory for the foo via malloc. foo.something surely make
    sense if you defined foo as above, but foo = malloc(...) doesn't make
    any sense.
     
    Nejat AYDIN, Aug 31, 2003
    #2
    1. Advertising

  3. Dimitris Mandalidis

    Simon Biber Guest

    "Dimitris Mandalidis" <_this_to_reply> wrote:
    > Hmm I knew that the answer was simple :) But if I have to use
    > Message foo and not Message *foo, does foo.something makes any sense?


    Yes, and if you declare a simple variable of type Message, rather than
    a pointer, it is allocated and deallocated automatically when it goes
    out of scope.

    --
    Simon.
     
    Simon Biber, Aug 31, 2003
    #3
  4. On Sun, 31 Aug 2003 23:04:06 +1000, Simon Biber wrote

    > Yes, and if you declare a simple variable of type Message, rather than
    > a pointer, it is allocated and deallocated automatically when it goes
    > out of scope.


    OK I got it, thanks both of you.

    HAND
    D.

    --
    PGP key 39A40276 at wwwkeys.eu.pgp.net
    key fingeprint: 9A0B 61C6 B826 4B73 69BB 972B C5E7 A153 39A4 0276
     
    Dimitris Mandalidis, Aug 31, 2003
    #4
  5. Dimitris Mandalidis

    Al Bowers Guest

    Dimitris Mandalidis wrote:
    > On Sun, 31 Aug 2003 15:44:42 +0300, Nejat AYDIN wrote
    >
    >
    >>You cannot assign a pointer to a structure. You'd probably want

    >
    >
    > Hmm I knew that the answer was simple :) But if I have to use Message foo and
    > not Message *foo, does foo.something makes any sense?
    >
    > D.
    >


    Function malloc will return a pointer value which points to
    the allocated memory. So you need to declare a pointer.

    Message *foo;

    To access the members of this allocated object, you would use the
    -> operator. So;
    foo->something would make sense.

    If you should declare a struct object.
    Message bar;
    then assign
    bar = *foo;
    the bar.something would make sense.

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    typedef struct BIO
    {
    char name[16];
    unsigned age;
    }BIO;

    BIO *CreateBio(const char *name, unsigned age);

    int main(void)
    {
    BIO exfriend;
    BIO *friend;

    friend = CreateBio("Sarah",16);
    if(friend != NULL)
    printf("My friend's name is %s\n"
    "and my friend's age is %u\n",
    friend->name,friend->age);
    else puts("Failed to create Bio");
    if(friend != NULL)
    {
    exfriend = *friend;
    printf("\nMy former friend's name is %s\n"
    "and my former friend's age is %u\n",
    exfriend.name, exfriend.age);
    }
    free(friend);
    return 0;
    }

    BIO *CreateBio(const char *name, unsigned age)
    {
    BIO *tmp = malloc(sizeof *tmp);

    if(tmp != NULL)
    {
    unsigned i = sizeof(tmp->name);
    tmp->age = age;
    strncpy(tmp->name,name,i);
    tmp->name[i-1] = '\0';
    }
    return tmp;
    }


    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x)
    http://www.geocities.com/abowers822/
     
    Al Bowers, Aug 31, 2003
    #5
    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. Brian Stubblefield

    incompatible types in assignment

    Brian Stubblefield, May 25, 2004, in forum: C Programming
    Replies:
    7
    Views:
    33,799
    Michael Fyles
    May 30, 2004
  2. v4vijayakumar

    incompatible types in assignment

    v4vijayakumar, Jun 5, 2006, in forum: C Programming
    Replies:
    16
    Views:
    923
    Keith Thompson
    Jun 6, 2006
  3. Richard Bos

    error: incompatible types in assignment

    Richard Bos, Jul 25, 2006, in forum: C Programming
    Replies:
    8
    Views:
    1,525
    Mark McIntyre
    Jul 25, 2006
  4. Replies:
    3
    Views:
    888
  5. dannydb368

    HELP please - incompatible types in assignment

    dannydb368, Mar 15, 2009, in forum: C Programming
    Replies:
    0
    Views:
    1,074
    dannydb368
    Mar 15, 2009
Loading...

Share This Page