Please help <pointer to the struct> 
Author Message
 Please help <pointer to the struct>

se help me to solve the following problem:

I have declared pointer to the stucture :

main() {

 struct intmd{

        char str_1[10];

        char operand[6];

        int error_flag[10];

        int process_flag[2];


struct intmd, intermd, *ptr_intermd;


/* now, the pointer to the struct has to be

   passed to the function that is declared

   in another file "file.h"

   fd stands for file descriptor.



Declaration func_1 in the file.h is as follow:

int func_1(int fd,struct intmd *ptr_intermd){

    /* now I attempt to read from the file

       a structure record



    return 0;





Tom Z.

Tue, 28 Apr 1998 03:00:00 GMT  
 Please help <pointer to the struct>

|> I have declared pointer to the stucture :
|> struct intmd, intermd, *ptr_intermd;
               ^ this comma shouldn't be here.
|> ptr_intermd=&intermd;

Let us analyze this line carefully: I _think_ the root of your confusion is
buried here. Specifically, what _exactly_ is the `&' in this statement.

Okay, what are types in C? They tell you the kind of values that are
possible, how they are represented and what can be done with them. Thus, a
`signed char' typically is the type that holds small integers from, say -128
to +127 (this may be different on your system). They can be used in a number
of contexts (e.g. you can add and divide them) and are typically used to
represent the characters that you see in texts (the exact correspondence
between the glyph that you see and the integer depends on your system: many
systems use a `code' called ASCII in short).

structs are also types which can have values. A value of a struct however is
not one integer, but a bunch of values (each of some type) associated with
certain names (called fields). Thus the value of your struct intmd is the
collection of values for the fields str_1, operand, error_flag and

A pointer to a struct is _also_ a type. Its values are `pointers' to structs,
i.e. the value is the `unique name' or, as it is usually called and
implemented, the `address' of some other struct.

In your case, ptr_intermd is an object of type pointer to struct which you
have defined, but which you did not initialize. Naturally, before you set it,
it does not point to any definite struct, so the value is `undefined'. Now,
you want it to point to a particular struct object. So, you name the struct
object, put an & in front of it (which tells the compiler that you do not
want the `value' of that struct, but rather a `pointer' to it), and assign
the result to ptr_intermd.

So far, so good.

|> /* now, the pointer to the struct has to be
|>    passed to the function that is declared
|>    in another file "file.h"

So, what do you want to do? An `object' never goes anywhere, you can only
pass a value. The value that you have assigned to ptr_intermd is recalled
just by naming it (in a context where a value is being looked for of
course ... not, for example, after an & where an object is being looked
for)... so use

func_1 (fd,ptr_intermd);

|>    func_1(fd,&ptr_intermd);

But, see what you have done now! You put an & in front of ptr_intermd ...
which means you do _not_ want the value of ptr_intermd. You want a pointer to
_it_. The result of course is a pointer to whatever type of object
ptr_intermd is: it is a pointer to a pointer to a struct! Not at all what
func_1 expects.

You would have been right if you had written func_1(fd,&intermd); Can you see

|> int func_1(int fd,struct intmd *ptr_intermd){
|>     read(f,&(*ptr),sizeof(*ptr));

You probably meant ptr_intermd instead of ptr. But can you see why you could
have written just ptr_intermd instead of &(*ptr_intermd)?


Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
Others see <gopher://>,
internetwork-mail-guide>. -- <>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]

Wed, 29 Apr 1998 03:00:00 GMT  
 [ 2 post ] 

 Relevant Pages 

1. <<<<<<<Parsing help, please>>>>>>>>

2. File Format conversion, ascii freeform -->.csv <-->.wk1<-->dbf<-->?HELP

3. <<ndbm.h>> - please help

4. memory leak: pointer->pointer->pointer->struct

5. memory Leak: pointer->pointer->pointer->struct

6. <><><>HELP<><><> PCMCIA Motorola Montana 33.6

7. <<<>>>Need C code advice with functions and sorting.<<<>>>

8. >>>Windows Service<<<

9. struct a <---> struct b

10. >>I need help!<<(duh)

11. >>> *** PLEASE HELP WITH OCX *** <<<

12. struct.array works - struct->pointer not


Powered by phpBB® Forum Software