473,396 Members | 2,129 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,396 software developers and data experts.

Dynamic array of ints problem

Greetings all, I'm working on a program that contains a struct:

struct queue {

int front; //the front of the queue
int elements; //the number of elements in the queue
int asize; //the size of the array
int qlist[]; //an array that will be dynamically sized

};

My objective is to read from a file, the length of the queue. Now, I've tried to use malloc, realloc and sorts of things to resize the array qlist[]. All my attempts have failed. My question: how can I resize qlist[] to store a number of ints specified at runtime?

I'd like to use a pointer to the struct, instead of the struct itself, if possible. Example: struct queue * q, instead of struct queue q;

Thanks in Advance.
Jun 26 '07 #1
5 2367
Silent1Mezzo
208 100+
Greetings all, I'm working on a program that contains a struct:

struct queue {

int front; //the front of the queue
int elements; //the number of elements in the queue
int asize; //the size of the array
int qlist[]; //an array that will be dynamically sized

};

My objective is to read from a file, the length of the queue. Now, I've tried to use malloc, realloc and sorts of things to resize the array qlist[]. All my attempts have failed. My question: how can I resize qlist[] to store a number of ints specified at runtime?

I'd like to use a pointer to the struct, instead of the struct itself, if possible. Example: struct queue * q, instead of struct queue q;

Thanks in Advance.
If you know the maximum number of elements that qlist can hold then put that within the brackets. If you don't know then do
Expand|Select|Wrap|Line Numbers
  1. int* qlist
and then you can malloc it.
Jun 26 '07 #2
If you know the maximum number of elements that qlist can hold then put that within the brackets. If you don't know then do
Expand|Select|Wrap|Line Numbers
  1. int* qlist
and then you can malloc it.
Ok, that's not a problem. But, say I do malloc it like this:

struct queue {


int front;
int elements;
int asize;
int * qlist;

};

...

struct queue * resize(int size){
struct queue * q;

q->front = 0;
q->elements = 0;
q->asize = size;
q->qlist = (int *)malloc(sizeof(int) * size);

return q;
}

in the above function, "size" is the max number of elements i'm going to store. If i attempt to what is shown in the code above, I usually get an 'improper use of flexible member' error. Any ideas?
Jun 26 '07 #3
Silent1Mezzo
208 100+
Ok, that's not a problem. But, say I do malloc it like this:

struct queue {


int front;
int elements;
int asize;
int * qlist;

};

...

struct queue * resize(int size){
struct queue * q;

q->front = 0;
q->elements = 0;
q->asize = size;
q->qlist = (int *)malloc(sizeof(int) * size);

return q;
}

in the above function, "size" is the max number of elements i'm going to store. If i attempt to what is shown in the code above, I usually get an 'improper use of flexible member' error. Any ideas?
Because q is a pointer to the structure you need to first malloc that pointer so it has enough room to hold everything inside of it
Expand|Select|Wrap|Line Numbers
  1. q = malloc(sizeof(struct queue));
Jun 26 '07 #4
Brilliant! Problem solved, thanks a bunch!
Jun 28 '07 #5
Silent1Mezzo
208 100+
Brilliant! Problem solved, thanks a bunch!
Glad everything worked out for you.
Jun 28 '07 #6

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

Similar topics

6
by: Vasileios Zografos | last post by:
Hello, I have a function that generates some values (e.g. vertices in 2d space) the number of which I dont know. So, it could generate 20 vertices, 100 vertices, or even 1 vertex. void...
6
by: Irrwahn Grausewitz | last post by:
Hey, y'all. While doing some pointer experiments I encountered a problem. I know that I know the answer already, but trying to remember I just screwed up my mind. I wonder if someone would be...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
9
by: gl | last post by:
How do I take an array or arraylist, and sort it randomly? Like suppose the items in it are (1,2,3,4,5) and I want to get it to be in a random order (2,3,1,4,5). How do you do that? I think it's a...
4
by: Mark Healey | last post by:
I'd like to allocate some memory for a two dimensional array. The problem is that whenever I try to use pointer arithmetic I screw up. I just want to use conventional array type statements to get...
24
by: VijaKhara | last post by:
hi all, i am trying to create a dynamic 2D array with size N x 3 (N will be put in as a parameter) using the following code: int **xyz; int i,N; N=30000; xyz=malloc(3*sizeof(int*));
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...
8
by: sachinv1821 | last post by:
hi all i have simple Problem please tell me the Solution if u know?? main() { int a={1,2,3,4,5,6,7,8,9}; printf("%u %u %u",a,a,a); } when i execute this program i am getting a Fixed address...
31
by: mdh | last post by:
I am still having a problem understanding K&RII on p 112. I have looked at the FAQs --which I am sure answer it in a way that I have missed, so here goes. A 2-dim array, (per K&R) is really a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.