Re: Pointer refering to an array of structutes?
- From: Robby <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 1 Dec 2007 18:21:01 -0800
Hello, Thanks for all your posts!
I tried both ways that Igor suggested:
1)an array of structures
2) allocating an array of twenty MAB structures on the heap
-ARRAY OF STRUCTURES
Based on our discussion, the folowing generates no errors:
========================Header file
struct MAB{
int LETTER_HEIGHT; //HEIGHT OF THE LETTER
int LETTER_WIDHT; //WIDTH OF THE LETTER
int LETTER_SPACING; //SPACING BETWEEN EACH LETTER
};
====================================.c file
struct MAB aMAB[20];
========================================
-ALLOCATING AN ARRAY OF 20 MAB STRUCTURES ON HEAP
However, the following generates errors:
========================Header file
struct MAB{
int LETTER_HEIGHT; //HEIGHT OF THE LETTER
int LETTER_WIDHT; //WIDTH OF THE LETTER
int LETTER_SPACING; //SPACING BETWEEN EACH LETTER
};
====================================.c file
MAB* iptr = malloc(20*sizeof(MAB));
====================================
I was wondering if it would have to do with a limitation of the compiler?
Or is it me again making mistakes?
--
Best regards
Robert
"Robby" wrote:
Thanks all for your posts! I will start by concentrating myself by the first.
one!
Igor, will your code work on raw C language, I have checked my compiler's
guide and as you must already know, "new" is not a valid command, I only have
calloc and malloc! Sorry my freind, I must only use C language for my
application as its for a microcontroller IC. However I have a question
pertaining to your example below:
========================================
struct S {int x;};
void f(S* p, int size) {
for (int i = 0; i < size; ++i) {
DoSomething(p[i].x);
}
}
S arr[100];
f(arr, 100);
S* p = new S[100];
f(p, 100);
delete[] p;
=========================
Mind you it is a long time I haven't used structures and therefore, If I may
ask:
1) S arr[100]; >>>> you create an array of "S" structures called arr, right?
2) If you may refresh my memory, what does this line do? S* p = new S[100];
---Does it create an array of pointers on the heap?
I tried entering the above code in my compiler and it doesn't work! If it
would be possible to show me how I can do your example but for "C".
With sincere regards
Rob
--
Best regards
Robert
"Igor Tandetnik" wrote:
"Robby" <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:5251BDFA-E64B-464B-84C1-E815D1AC604B@xxxxxxxxxxxxx
I would like to create an array of structures and then be able to
access any item of the structure in any element of the array via a
pointer. This way if I am in a function and I need information of an
item of a particular structure, I can simply just pass the pointer to
the structure and not the whole array of structures!
What do you believe is the difference? How precisely do you think you
can pass "the whole array of structures"?
So I believe I need to create a pointer that can reference an array of
structures.
Right?
Usually, you just pass a pointer to the first element of the array, and
a number of elements:
struct S {int x;};
void f(S* p, int size) {
for (int i = 0; i < size; ++i) {
DoSomething(p[i].x);
}
}
S arr[100];
f(arr, 100);
S* p = new S[100];
f(p, 100);
delete[] p;
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
- Follow-Ups:
- Re: Pointer refering to an array of structutes?
- From: Igor Tandetnik
- Re: Pointer refering to an array of structutes?
- References:
- Re: Pointer refering to an array of structutes?
- From: Igor Tandetnik
- Re: Pointer refering to an array of structutes?
- From: Robby
- Re: Pointer refering to an array of structutes?
- Prev by Date: Re: VC preprocessor bug?
- Next by Date: Re: Pointer refering to an array of structutes?
- Previous by thread: Re: Pointer refering to an array of structutes?
- Next by thread: Re: Pointer refering to an array of structutes?
- Index(es):
Relevant Pages
|