Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   dynamic array of struct (http://www.velocityreviews.com/forums/t437440-dynamic-array-of-struct.html)

Excluded_Middle 03-22-2005 10:44 PM

dynamic array of struct
 
what is wrong with this code

#include <stdio.h>

typedef struct s1
{
int a;
int b;
}s1;

int main()
{
s1 *tmp;

tmp = (s1 *)calloc(10,sizeof(s1));
if(tmp == NULL) /* Error checking done */

tmp[5]->a = 90;
tmp[5]->b = 239;

printf("a is %d\n",tmp[5]->a);
printf("b is %d\n",tmp[5]->b);
free(tmp);
return 0;
}

in this program i want to create a dynamic array of s1. and then i
want to access each element using array notation like tmp[5] for the
6th element in the array.

thats what i get when I compile it using cc comand on linux OS
buffer.c: In function `main':
buffer.c:16: invalid type argument of `->'
buffer.c:17: invalid type argument of `->'
buffer.c:19: invalid type argument of `->'
buffer.c:20: invalid type argument of `->'

Eric Sosman 03-22-2005 10:49 PM

Re: dynamic array of struct
 


Excluded_Middle wrote:
> what is wrong with this code
>
> #include <stdio.h>
>
> typedef struct s1
> {
> int a;
> int b;
> }s1;
>
> int main()
> {
> s1 *tmp;
>
> tmp = (s1 *)calloc(10,sizeof(s1));
> if(tmp == NULL) /* Error checking done */
>
> tmp[5]->a = 90;


tmp[5] is a struct, not a pointer to a struct.
Use `.' instead of `->'.

--
Eric.Sosman@sun.com


Artie Gold 03-22-2005 10:51 PM

Re: dynamic array of struct
 
Excluded_Middle wrote:
> what is wrong with this code
>
> #include <stdio.h>
>
> typedef struct s1
> {
> int a;
> int b;
> }s1;
>
> int main()
> {
> s1 *tmp;
>
> tmp = (s1 *)calloc(10,sizeof(s1));
> if(tmp == NULL) /* Error checking done */
>
> tmp[5]->a = 90;

`tmp[5]' is an object of type `struct s1', as opposed to being a pointer
to one, so
tmp[5].a = 90;
is what you want.
> tmp[5]->b = 239;

Similarly.
>
> printf("a is %d\n",tmp[5]->a);
> printf("b is %d\n",tmp[5]->b);
> free(tmp);
> return 0;
> }
>
> in this program i want to create a dynamic array of s1. and then i
> want to access each element using array notation like tmp[5] for the
> 6th element in the array.
>
> thats what i get when I compile it using cc comand on linux OS
> buffer.c: In function `main':
> buffer.c:16: invalid type argument of `->'
> buffer.c:17: invalid type argument of `->'
> buffer.c:19: invalid type argument of `->'
> buffer.c:20: invalid type argument of `->'

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays

Martin Ambuhl 03-22-2005 11:15 PM

Re: dynamic array of struct
 
Excluded_Middle wrote:
> what is wrong with this code


Compare your code

> #include <stdio.h>
> typedef struct s1
> {
> int a;
> int b;
> }s1;


> int main()
> {
> s1 *tmp;
> tmp = (s1 *)calloc(10,sizeof(s1));
> if(tmp == NULL) /* Error checking done */
> tmp[5]->a = 90;
> tmp[5]->b = 239;
> printf("a is %d\n",tmp[5]->a);
> printf("b is %d\n",tmp[5]->b);
> free(tmp);
> return 0;
> }


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

typedef struct s1
{
int a;
int b;
} s1;

int main()
{
s1 *tmp;

if (!(tmp = malloc(10 * sizeof *tmp))) {
fprintf(stderr, "malloc failed.\n" "bailing ...\n");
exit(EXIT_FAILURE);
}
tmp[5].a = 90;
tmp[5].b = 239;
printf("a is %d\n", tmp[5].a);
printf("b is %d\n", tmp[5].b);
free(tmp);
return 0;
}

CBFalconer 03-23-2005 03:05 AM

Re: dynamic array of struct
 
Excluded_Middle wrote:
>
> what is wrong with this code


You put a cast where it doesn't belong and suppressed an error
message, and other things.

>
> #include <stdio.h>
>
> typedef struct s1
> {
> int a;
> int b;
> }s1;
>
> int main()
> {
> s1 *tmp;
>
> tmp = (s1 *)calloc(10,sizeof(s1));

^^^^^^
get rid of this cast and you will get an error message here. A
better statement would be:

tmp = malloc(10 * sizeof *tmp);

and initialize things properly yourself if needed. calloc
guarantees all bytes zero, but that does not properly initialize
such fields as reals and pointer. The error message should lead
you to "#include <stdlib.h>" at some point.

> if(tmp == NULL) /* Error checking done */
>
> tmp[5]->a = 90;


the type of tmp[5] is s1. s1 is not a pointer. It has fields a
and b, which you access with dot notation, i.e. "tmp[5].a = 90;".
You could also use "(tmp + 5)->a". And so forth.

> tmp[5]->b = 239;
>
> printf("a is %d\n",tmp[5]->a);
> printf("b is %d\n",tmp[5]->b);
> free(tmp);
> return 0;
> }
>
> in this program i want to create a dynamic array of s1. and then i
> want to access each element using array notation like tmp[5] for the
> 6th element in the array.
>
> thats what i get when I compile it using cc comand on linux OS
> buffer.c: In function `main':
> buffer.c:16: invalid type argument of `->'
> buffer.c:17: invalid type argument of `->'
> buffer.c:19: invalid type argument of `->'
> buffer.c:20: invalid type argument of `->'



--
"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare




All times are GMT. The time now is 12:15 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.