Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: malloc & incompatible types in assignment

Reply
Thread Tools

Re: malloc & incompatible types in assignment

 
 
Dimitris Mandalidis
Guest
Posts: n/a
 
      08-31-2003
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
 
Reply With Quote
 
 
 
 
Nejat AYDIN
Guest
Posts: n/a
 
      08-31-2003
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.
 
Reply With Quote
 
 
 
 
Simon Biber
Guest
Posts: n/a
 
      08-31-2003
"Dimitris Mandalidis" <(E-Mail Removed)_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.


 
Reply With Quote
 
Dimitris Mandalidis
Guest
Posts: n/a
 
      08-31-2003
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
 
Reply With Quote
 
Al Bowers
Guest
Posts: n/a
 
      08-31-2003


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: http://www.velocityreviews.com/forums/(E-Mail Removed) (remove the x)
http://www.geocities.com/abowers822/

 
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
HELP please - incompatible types in assignment dannydb368 C Programming 0 03-15-2009 06:51 PM
"Incompatible types" error though types are same generic type wdh3rd@gmail.com Java 3 04-15-2007 02:36 PM
error: incompatible types in assignment Richard Bos C Programming 8 07-25-2006 10:20 PM
incompatible types in assignment v4vijayakumar C Programming 16 06-06-2006 08:11 AM
incompatible types in assignment Brian Stubblefield C Programming 7 05-30-2004 04:16 PM



Advertisments