dynamic array of struct

E

Excluded_Middle

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 `->'
 
E

Eric Sosman

Excluded_Middle said:
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 `->'.
 
A

Artie Gold

Excluded_Middle said:
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
 
M

Martin Ambuhl

Excluded_Middle said:
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;
}
 
C

CBFalconer

Excluded_Middle said:
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 said:
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 `->'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top