Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointer to struct ,assignment to members. 2

Status
Not open for further replies.

marsd

IS-IT--Management
Apr 25, 2001
2,218
US
I'm new to C so be patient please.

What are some legal ways to assign values to members with this kind of code?

struct bogus {
char *name;
int number;
float *extra;
};

typedef struct bogus FOO;

int main(void) {
FOO *sptr;

if (sptr = (FOO *)malloc(sizeof(FOO)) > 0) {
sptr->name = what works here?
sptr->number = what works here?
sptr->extra = what works here?
}
return 0;
}

I know this looks like HW , but it is just me trying to learn the language.

Thanks.


 
Although the following conditional statement's ok,

Code:
if (sptr = (FOO *)malloc(sizeof(FOO)) > 0)

but you should replace the greater than operator (>) with not equal to operator (!=):

Code:
if ((sptr = (FOO *)malloc(sizeof(FOO))) != 0)

Unless you'll not want to modify to string pointed to by sptr->name, you should assign the string by using strcpy() function (strpy(sptr->name, "any string")). If you want to assign by using strcpy() function, be sure to allocate new memory for it (sptr->name) before using it.

For the sptr->extra, you MUST allocate new memory for it before using it.
 
Hi,

you can:

char *pName="ANDY", *p;
or char buf[80]; strcpy( buf, "JOE" ) ; p=buf ;
int i=25;
float f=0.5;

sptr->name = pName or buf or p
sptr->number = i
sptr->extra = &f

You can also, maby you know, define a struct without
dynamic allocation.

struct bogus myStruct ;
myStruct.name = p ;
myStruct.number = 25;
myStruct.extra = &f ;


bye
 
denniscpp..
Could you give me a working example of a
memory allocation for the pointer members you
refer to?
Naively, would this work?
sptr->extra = (float *)sizeof(float));

Also can I assign the value to the pointed member string
with a function besides strcpy(). This is really limiting.

How about something like:
char *ret_string(char *msg) {
char *p;
p = (char *)malloc(35 * sizeof(char));
if (p) {
printf("%5s:" msg);
fgets(p, 35, stdin);
}
return p;
}

Thanks for your help!
 
Victorv,

Thanks, That's exactly what I'm looking for.
As far as doing the static structs..I'm trying to
get beyond that. A little work with some more advanced concepts, up to linked lists..
 
hi,

some words about memory allocation

myroutine()
{
int j ;
static int counter =0 ;
counter++;
return counter;
}

int myarea[20] ;

main()
{
int i ;
char *p;
p=malloc(256);
...

if()
i=myroutine();
}


The word dynamic means that the memory area is dinamically
allocated at runtime not at comilation time; the parameter
of malloc can be read from keyboard,file or from an operation.

The vars i,j,p are created at runtime using stack, and they
are destroyed when the routine exit.
(they are called automatic and their value at routine begin
is unpredictable: if you can more than 1 time a rout., each
time the value are dirty, and you have to initialize.)

The var counter has been decled "static" : this means that
at the start program the machine put a value (0 usually),
but if you re-call the routine, the value remain set.
(this means static);

The var myarea (external at routines) goes as static vars.
The use of static modifier in this type of vars, is used
to use the same name in different module ( file1.c file2.c)
with different location.

All above is true for any type ov vars: int,char,float,struct....

Don't abuse malloc if you don't need, and remember to free
allocated areas ( use free() function ) ;

bye
 
Right victorv,
But if you don't learn how to to do this then eventually
you will be badly stumped when there is a situation that
you need to be able to grok these concepts.
An example is the linux ip firewall code, there is a section
that contains complex linked list code. I will never be able to understand it if I can't reasonably approximate it.

Anyway, thanks alot, this helped clear up some problems for me.
 
Victorv,

I just happened to be reading through this thread, and your last reply stated that you shouldn't abuse the use of malloc if you don't need it. As more and more programs are becoming datacentric, it becomes more of a waste to use statically allocated memory. I realize it all depends on your actual application, but I've noticed a growing trend in user needs, and learning the usage of malloc has become a lot more important now than even just a few years ago.

As a side note, I don't feel that it is "abuse" if you know how to use it correctly, you can make full use out of it, and you know how to fully clean up after it when you program finishes or any other time you need to.

Just my 2 cents :)

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top