Struct & malloc problem : pointer expected 
Author Message
 Struct & malloc problem : pointer expected

Hi,

Hope someone can help solve this. I do not know what is wrong
with the try program below. The compiler indicated error on the
line with scanf statement -> pointer expected !

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

typedef struct data
 {
  int x
  double y;
 } RECORD;

main()
{
int count;

RECORD *p;
p=(RECORD *) malloc(sizeof(RECORD)*200);

for (count=0; count< 200; count++)
   {
    print("Enter data : ");
    scanf("%f",p[count]->y);
   }

Quote:
}

thx & rgds
adrian


Sun, 07 Dec 2003 13:52:39 GMT  
 Struct & malloc problem : pointer expected

Quote:
> Hi,

> Hope someone can help solve this. I do not know what is wrong
> with the try program below. The compiler indicated error on the
> line with scanf statement -> pointer expected !

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

> typedef struct data
>  {
>   int x
>   double y;
>  } RECORD;

The above typedef appears to be useless to many programmers
since it simply hides that the type is a struct. It is
mostly easier to use the type if you know from it's name
what it is.

Quote:
> main()

it's called

int main (void)

Quote:
> {
> int count;

> RECORD *p;

I'ld write

struct data *p;

Quote:
> p=(RECORD *) malloc(sizeof(RECORD)*200);

p = malloc(200 * sizeof *p);

No need to cast malloc as you have included stdlib.h.

Quote:

> for (count=0; count< 200; count++)
>    {
>     print("Enter data : ");
>     scanf("%f",p[count]->y);

scanf needs to write data into the given variable. In C
that can only be done by passing a reference (a pointer)
to a variable to a function. So you should calculate
the address of p[count]->y so scand knows where in
memory to write data it reads from input.

Also %f specifies that a pointer to a float variable is
passed to scanf rather than a pointer to a double. For
doubles use %lf.

scanf("%lf", &p[count]->y);

Quote:
>    }

> }

> thx & rgds
> adrian

HTH
--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond


Sun, 07 Dec 2003 14:48:29 GMT  
 Struct & malloc problem : pointer expected

Quote:

> Hi,

> Hope someone can help solve this. I do not know what is wrong
> with the try program below. The compiler indicated error on the
> line with scanf statement -> pointer expected !

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

> typedef struct data
>  {
>   int x
>   double y;
>  } RECORD;

> main()
> {
> int count;

> RECORD *p;
> p=(RECORD *) malloc(sizeof(RECORD)*200);
> for (count=0; count< 200; count++)
>    {
>     print("Enter data : ");
>     scanf("%f",p[count]->y);
>    }
> }

Adrian...

Here's a minimal list of things that need fixing. I'll leave it up
to you to find out why (or ask more specific questions if you
can't):

[1] int main(void)
[2] p = malloc(sizeof(RECORD) * 200);
[3] if (p == NULL) /* Do error recovery here */
[4] scanf("%f",&p[count]->y);

In addition, your use of scanf() is fairly risky practice and makes
your program unreliable. Remember that failure of a compiler to
find errors doesn't mean that the code is good.
--
Morris Dovey
West Des Moines, Iowa USA



Sun, 07 Dec 2003 14:54:06 GMT  
 Struct & malloc problem : pointer expected
Quote:

> Hi,

> Hope someone can help solve this. I do not know what is wrong
> with the try program below. The compiler indicated error on the
> line with scanf statement -> pointer expected !

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

> typedef struct data
>  {
>   int x
>   double y;
>  } RECORD;

> main()
> {
> int count;

> RECORD *p;
> p=(RECORD *) malloc(sizeof(RECORD)*200);

Fine so far (though I know there's a number of people out there going to beat You up for casting malloc()'s result - but never mind, the result remains correct anyway).

p now
 - points to a block of memory large enough to hold 200 RECORDs
 - always points to an object of type RECORD and size  sizeof(RECORD)
 - may be used like an array of 200 records

Since p[i] is equivalent to *(p+i) for any <type> *p  (with <type> != void), p[i] will always yield a  RECORD , not a  RECORD * .

Quote:

> for (count=0; count< 200; count++)
>    {
>     print("Enter data : ");
>     scanf("%f",p[count]->y);

Ok, now here we have two errors in one compact term.

1) As stated above, p[count] is an object of type RECORD, which is a structure - not a pointer to a structure. To address its element y, You'll need to use p[count].y.
2) Scanf expects a pointer to the target object as its argument, thus You need to take the address of p[count].y using the prefix &-operator.

So the correct form for this line should be
        scanf("%f",&p[count].y);

Quote:
>    }

> }

Cheers,
        Thomas


Sun, 07 Dec 2003 13:09:04 GMT  
 Struct & malloc problem : pointer expected

Quote:

> Hope someone can help solve this. I do not know what is wrong
> with the try program below. The compiler indicated error on the
> line with scanf statement -> pointer expected !

> typedef struct data
>  {
>   int x
>   double y;
>  } RECORD;

> RECORD *p;
> p=(RECORD *) malloc(sizeof(RECORD)*200);

Miss the cast, change to sizeof *variable, like this:

  p=malloc(200*sizeof *p);

It's more solid that way.

Quote:
>     scanf("%f",p[count]->y);

p is a pointer to RECORD. p[count], therefore, is a RECORD. A RECORD is
not a pointer. Therefore, you can't apply -> to it. In fact, you
probably meant
  scanf("%f",p[count].y);
instead. Note that scanf() is fraught with pitfalls, but can be useful
if you're careful indeed.

Richard



Sun, 07 Dec 2003 14:58:10 GMT  
 Struct & malloc problem : pointer expected

Quote:

> >     scanf("%f",p[count]->y);

> p is a pointer to RECORD. p[count], therefore, is a RECORD.
> A RECORD is not a pointer. Therefore, you can't apply -> to it.
> In fact, you probably meant
>   scanf("%f",p[count].y);
> instead.

*You* probably meant:

  scanf("%f", &p[count].y);

because scanf does expect a pointer.

You should have a coffee before you start posting ;-)

willem



Sun, 07 Dec 2003 15:41:07 GMT  
 Struct & malloc problem : pointer expected
Once upon a while "Zoran Cutura"
...

Quote:

> scanf("%lf", &p[count]->y);

Sorry, I missed that p is a pointer but p[count] isn't.
So you have to write

scanf("%lf", &p[count].y);

--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond



Sun, 07 Dec 2003 15:48:31 GMT  
 Struct & malloc problem : pointer expected


Quote:

> > typedef struct data
> >  {
> >   int x
> >   double y;
> >  } RECORD;

Just pointing out the missing ';' after attribute x in the declaration
of struct data. To OP, your compiler should have warned about this also,
so I guess this is just a typo in the mail message.

    Des Walker



Sun, 07 Dec 2003 15:24:48 GMT  
 Struct & malloc problem : pointer expected


Wed, 18 Jun 1902 08:00:00 GMT  
 Struct & malloc problem : pointer expected

Quote:
>The above typedef appears to be useless to many programmers
>since it simply hides that the type is a struct. It is
>mostly easier to use the type if you know from it's name
>what it is.

some of us actually learned how to use abstractions thank you

gosh no typedefs
no #defines

i think you should warn people about the evils of functions
you never know what a function might do

crippled coding standards to cope with crippled interfaces

or you could do your interfaces correctly



Sun, 07 Dec 2003 20:21:07 GMT  
 Struct & malloc problem : pointer expected

Quote:
> >The above typedef appears to be useless to many programmers
> >since it simply hides that the type is a struct. It is
> >mostly easier to use the type if you know from it's name
> >what it is.

>  some of us actually learned how to use abstractions thank you

In what way is FOO more abstract than struct foo?

Gergo

--
Give a woman an inch and she'll park a car in it.



Sun, 07 Dec 2003 20:28:22 GMT  
 Struct & malloc problem : pointer expected


Wed, 18 Jun 1902 08:00:00 GMT  
 Struct & malloc problem : pointer expected

Quote:
>> typedef struct data {
>>   int x
>>   double y;
>>  } RECORD;

> The above typedef appears to be useless to many programmers
> since it simply hides that the type is a struct. It is
> mostly easier to use the type if you know from it's name
> what it is.

You did say many, but I still want to point out that typedef'ing can be a
good thing. I write drivers almost exclusively. Many devices have registers
that are read-only/write-only at the same address. Before hardware is built,
I like to simulate my code and structures. Thus, for the simulated, in-memory
version of the device's register map is a struct. When I switch to the real
hardware I need only change the typedef from struct to union at a single
point.

E.g.

#ifdef REAL_HARDWARE
typdef union
#else /* SIMULATED HARDWARE */
typdef struct
#endif
{
    volatile unsigned char command;
    volatile unsigned char status;

Quote:
} RegMap;

RegMap *pMap = (RegMap *) SOME_ADDR;

map->command = WRITE_SECTORS;
while (map->status & BSY);

So, for me, hiding struct-ness can be a good thing.

--
Warmest regards.

Optimize only if it runs too slowly or does not fit, spaces instead of tabs.



Sun, 07 Dec 2003 20:35:53 GMT  
 Struct & malloc problem : pointer expected



Quote:

> > >     scanf("%f",p[count]->y);

> > p is a pointer to RECORD. p[count], therefore, is a RECORD.
> > A RECORD is not a pointer. Therefore, you can't apply -> to it.
> > In fact, you probably meant
> >   scanf("%f",p[count].y);
> > instead.

> *You* probably meant:

>   scanf("%f", &p[count].y);

> because scanf does expect a pointer.

> You should have a coffee before you start posting ;-)

both of **You** probably meant:

   scanf("%lf", &p[count].y);

since y is a double

Roger...



Sun, 07 Dec 2003 21:30:32 GMT  
 Struct & malloc problem : pointer expected

Quote:

> *You* probably meant:

>   scanf("%f", &p[count].y);

So I did.

Quote:
> You should have a coffee before you start posting ;-)

Most definitely not. But I _should_ have my tea.

Richard



Sun, 07 Dec 2003 23:17:13 GMT  
 
 [ 26 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Malloc & structs contain struct*

2. pointer & malloc problem

3. Struct & pointer problem

4. Struct & Malloc

5. Dynamically malloc()-able pointers in a struct?

6. Confusion- malloc/pointers/structs

7. malloc & struct

8. pointers to functions in structs which take a pointer to that struct as an arg

9. cast pointer to struct to pointer to type of first member of struct

10. malloc()'ing struct to struct and comparison

11. SIMPLE malloc & pointer question

12. (void *) & casting the pointer returned by malloc

 

 
Powered by phpBB® Forum Software