473,378 Members | 1,152 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,378 software developers and data experts.

pointer to an array of structures

hi..
I am trying to pass an array of structures to a function. my code is

mainObject *scene[10];
void open_callback(int control_id)
{
char *temp_filename = (char*)malloc(255*sizeof(char));
printf("\n Enter the filename : ");
fgets(temp_filename,255,stdin);
temp_filename[strlen(temp_filename)-1] = ' \0 ' ;
open_file(temp_filename, scene);
}

void open_file(char *FileName, mainObject *scene[10])
{
for(int file_count=0; file_count<10; file_count++)
{
scene[file_count] = new mainObject[file_size];
for(int j=0; j< file_size; j++)
{
(scene[file_count]+j)->pos.x = X;
(scene[file_count]+j)->pos.y = Y;
(scene[file_count]+j)->pos.z = Z;
(scene[file_count]+j)->colour.r = R;
(scene[file_count]+j)->colour.g = G;
(scene[file_count]+j)->colour.b = B;
(scene[file_count]+j)->colour.a = A;
(scene[file_count]+j)->renderActive = active;
(scene[file_count]+j)->size = 1.0;
}
}
}

where mainObject is

typedef struct
{
float r,g,b,a;
}color_s ;
typedef struct
{
float x,y,z;
}position_s ;
typedef struct
{
position_s pos;
color_s colour;
float size;
unsigned int renderActive;
}mainObject ;


Execution results in an exception. This may work if i pass a pointer to scene[10]. Can anyone help me with the syntax for this or identify the problem with my code in case its different from the one i assume...

Any help would be greatly appreciated. Thanks a lot

regards,
AM
Apr 21 '08 #1
1 1385
weaknessforcats
9,208 Expert Mod 8TB
mainObject *scene[10];
void open_callback(int control_id)
{
char *temp_filename = (char*)malloc(255*sizeof(char));
printf("\n Enter the filename : ");
fgets(temp_filename,255,stdin);
temp_filename[strlen(temp_filename)-1] = ' \0 ' ;
open_file(temp_filename, scene);
}

void open_file(char *FileName, mainObject *scene[10])
There are several problems in this code. One is that you never free temp_filename inside open_callback(). That's a leak.

Then there this:
mainObject *scene[10];
This is an array to 10 scene pointers. I assume each of these pointers points to something. They do, don't they?

If you really meant an array of 10 scene structures, you have to code:
Expand|Select|Wrap|Line Numbers
  1. mainObject scene[10];
  2.  
Then you pass this array to a function:
Expand|Select|Wrap|Line Numbers
  1. void open_file(char *FileName, mainObject *scene) 
  2.  
and probably should have another argument for the nunmber of elements in tha array.

You mioght reas this article. It addresses your problems.
http://bytes.com/forum/thread772412.html.
Apr 26 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Frank Münnich | last post by:
Hi there.. My name is Frank Münnich. I've got a question about pointers that refer to an array of a structure. How do I declare that type? If I have declared a structure struct mystruc {...
11
by: Sontu | last post by:
Consider the following code: int main(void) { char buffer; func(buffer); } void func(char *bufpas) {
2
by: beetle | last post by:
Hello, I'm storing data in several different binary tree's. The root node is located in a struct containing general data about the tree. struct lnode { char *fname; int nentry;
8
by: G Patel | last post by:
Hi, As the FAQ and several clc post warn again, I know it's not legal to use a pointer to step outside the bounds of an array (except one past the last element, and that pointer must never be...
7
by: Kathy Tran | last post by:
Hi, Could you please help me how to declare an araay of pointer in C#. In my program I declared an structure public struct SEventQ { public uint uiUserData; public uint uiEvent; public uint...
8
by: ulyses | last post by:
I'm trying to put pointer to flexible array of structures in other structure. I want to have pointer to array of pixels in screen structure. Here is mine code, but I think it isn't quite all right:...
15
by: Paminu | last post by:
Still having a few problems with malloc and pointers. I have made a struct. Now I would like to make a pointer an array with 4 pointers to this struct. #include <stdlib.h> #include <stdio.h>...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
20
by: jason | last post by:
Hello, I'm a beginning C programmer and I have a question regarding arrays and finding the number of entries present within an array. If I pass an array of structures to a function, then...
7
by: Neil | last post by:
What I am doing wrong This works batPointer = adaptors.adaptor->batData; adaptors.batteries = batPointer->battery; where: batData is a pointer to a struct batPointer is a pointer to a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.