473,327 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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 `->'
Nov 14 '05 #1
4 8538


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

--
Er*********@sun.com

Nov 14 '05 #2
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
Nov 14 '05 #3
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;
}
Nov 14 '05 #4
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
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
13
by: lovecreatesbeauty | last post by:
/* How do free() know how many elements should be freed in a dynamic array? When free a single variable, the amount of byte of memory can be retrieved from the type of variable itself. ...
2
by: lovecreatesbeauty | last post by:
/* How does free() know how many elements should be freed in a dynamic array? When it frees a single variable, the size information about amount of byte of memory can be retrieved from the...
10
by: Russell Mangel | last post by:
I have written the following program using VS2005. The program is a Dynamic Array similar to System.Collections.ArrayList in .NET. The program works okay until I reach 65536, I can't seem to figure...
0
by: skm376 | last post by:
I am a little confused as to how I need to allocate memory for a dynamic array inside a struct. Here is the struct that I am using: typedef struct command_t *commandPtr; typedef struct...
2
by: Opteron64 | last post by:
Hi, I'm trying to create and initialise a dynamic array within a nested structure. The structure is defined as followed: (C++ code) typedef unsigned char uchar; typedef unsigned int uint; ...
5
by: viperdriver87 | last post by:
Greetings all, I had another post on here about a Dynamic array of ints problem, and with your help, I managed to solve that problem. Now, I've another issue. I need to dyanically allocate an array...
9
by: johndale | last post by:
hi, I am pretty new to C#, but have some experience with other lang.(delphi,php,asp...). I wanted to create dynamic array of structs type, but it wont work. So I google it, and found that C#.NET...
1
by: Gurur | last post by:
Hi all, I have a doubt. If I have 2 structures and one is parent of other , ie the child structure is present in the parent one . And if the child structure is declared as dynamic array in the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.