473,729 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array of structs instead of an array with pointers to structs?

Why make an array of pointers to structs, when it is possible to just make
an array of structs?

I have this struct:

struct test {
int a;
int b;
};

I have then made an array:

struct test testarray[5];

I would then like to shift all the elements one index to the right and
afterwards insert a new pkt struct at index 0.

something like this:

I would like to insert 3 different test structs with "b": 10, 20, and 30.

When I iterate through the testarray and make a printout of testarray[i].b,
I should see something like this:
10, 0, 0, 0, 0
20, 10, 0, 0, 0
30, 20, 10, 0, 0

For this to work I have made this function:

void insertIntoBuffe r(struct test tt)
{
int c;
for(c=3; c>=0; c--)
{
testarray[c+1] = testarray[c];
}

testarray[0] = tt;
int i;
for (i = 0; i < 5; i++ )
{
printf("Added b %d\n", testarray[i].b);
}
printf("\n");

}

It seems to work fine, so I can't seem to see any reason to mess around with
an array of pointers.

Are there anything that I cannot do with this simple solution that would be
possible with an array of pointers to structs?
Nov 15 '05 #1
5 3127

Paminu wrote:
Why make an array of pointers to structs, when it is possible to just make
an array of structs?
void insertIntoBuffe r(struct test tt)
{
int c;
for(c=3; c>=0; c--)
{
testarray[c+1] = testarray[c];
}

testarray[0] = tt;
int i;
for (i = 0; i < 5; i++ )
{
printf("Added b %d\n", testarray[i].b);
}
printf("\n");

}

It seems to work fine, so I can't seem to see any reason to mess around with
an array of pointers.

Are there anything that I cannot do with this simple solution that would be
possible with an array of pointers to structs?

......Observe that everytime you insert a new element into the array you
are using a for loop to shift all elements....in general the complexity
of this function is O(n), which can be achieved in constant time O(1)
if you use pointers (linked list) and add a new element at the head of
the list.

- Singamsetty

Nov 15 '05 #2
Singamsetty wrote:

Paminu wrote:
Why make an array of pointers to structs, when it is possible to just
make an array of structs?
void insertIntoBuffe r(struct test tt)
{
int c;
for(c=3; c>=0; c--)
{
testarray[c+1] = testarray[c];
}

testarray[0] = tt;
int i;
for (i = 0; i < 5; i++ )
{
printf("Added b %d\n", testarray[i].b);
}
printf("\n");

}

It seems to work fine, so I can't seem to see any reason to mess around
with an array of pointers.

Are there anything that I cannot do with this simple solution that would
be possible with an array of pointers to structs?

.....Observe that everytime you insert a new element into the array you
are using a for loop to shift all elements....in general the complexity
of this function is O(n), which can be achieved in constant time O(1)
if you use pointers (linked list) and add a new element at the head of
the list.

- Singamsetty

Ok so the "only" difference between regular arrays and pointers are the
efficency?
Nov 15 '05 #3

"Paminu" <ja******@asd.c om> wrote in message
news:di******** **@news.net.uni-c.dk...
Singamsetty wrote:

Paminu wrote:
Why make an array of pointers to structs, when it is possible to just
make an array of structs?
void insertIntoBuffe r(struct test tt)
{
int c;
for(c=3; c>=0; c--)
{
testarray[c+1] = testarray[c];
}

testarray[0] = tt;
int i;
for (i = 0; i < 5; i++ )
{
printf("Added b %d\n", testarray[i].b);
}
printf("\n");

}

It seems to work fine, so I can't seem to see any reason to mess around
with an array of pointers.

Are there anything that I cannot do with this simple solution that would
be possible with an array of pointers to structs?

.....Observe that everytime you insert a new element into the array you
are using a for loop to shift all elements....in general the complexity
of this function is O(n), which can be achieved in constant time O(1)
if you use pointers (linked list) and add a new element at the head of
the list.

- Singamsetty

Ok so the "only" difference between regular arrays and pointers are the
efficency?


No.

An array is a collection of one or more objects of the same type.
A pointer is a single object. Depending upon what you're doing,
one may be more useful than the other.
-Mike
Nov 15 '05 #4

Paminu wrote:
Singamsetty wrote:

Paminu wrote:
Why make an array of pointers to structs, when it is possible to just
make an array of structs?
void insertIntoBuffe r(struct test tt)
{
int c;
for(c=3; c>=0; c--)
{
testarray[c+1] = testarray[c];
}

testarray[0] = tt;
int i;
for (i = 0; i < 5; i++ )
{
printf("Added b %d\n", testarray[i].b);
}
printf("\n");

}

It seems to work fine, so I can't seem to see any reason to mess around
with an array of pointers.

Are there anything that I cannot do with this simple solution that would
be possible with an array of pointers to structs?

.....Observe that everytime you insert a new element into the array you
are using a for loop to shift all elements....in general the complexity
of this function is O(n), which can be achieved in constant time O(1)
if you use pointers (linked list) and add a new element at the head of
the list.

- Singamsetty

Ok so the "only" difference between regular arrays and pointers are the
efficency?

.........There are always trade-offs between pointers and arrays. It all
depends on what you are trying to do. Pointers are not as efficient as
arrays when it comes to storing a fixed amount of data, which requires
a non-sequential access.

- Singamsetty

Nov 15 '05 #5


Paminu wrote On 10/11/05 14:30,:
Why make an array of pointers to structs, when it is possible to just make
an array of structs?


More generally, "Why make an array of pointers to X, when it
is possible to just make an array of X?"

The principal reason is flexibility. The size of an array
is fixed when you create it and cannot be changed, but with
pointers you can start using malloc() and friends to let your
data structures automatically adjust to the "problem size."
Also, pointers let you build data structures that just don't
work with plain arrays -- for example, the array of pointers
might contain several pointers to the same struct instance.

In some settings an array of pointers can lead to speed
improvements. For example, suppose you've got ten thousand
very large struct objects and you want to use qsort() to put
them in order. Even if the structs themselves do happen to
reside in an array, you might do better to build an array of
pointers to them and use qsort() to rearrange the pointers:
instead of sloshing all those 792-byte structs back and forth
to reorganize the array, qsort() can just move small 4- or 8-
byte pointers around and may well run faster. You could use
several such "parallel" pointer arrays to sort the big array
of structs in different ways simultaneously: one pointer array
would be sorted by telephone number, another by postal code,
another by credit card balance, and so on. It probably uses
less memory to store one array of big structs and three arrays
of small pointers than to store three separate copies of all
those structs -- and if you're making changes to them, you'd
need to remember to change every copy ...

Pointer arrays are not The Magic Solution to every problem,
and it's silly to use them when you don't need them. But there
is certainly no reason to avoid them, and many circumstances
when they are the tool of choice.

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

Nov 15 '05 #6

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

Similar topics

7
2483
by: Frank M. | last post by:
I'm trying to declare an array of pointers to structures so that I can make the last element a NULL pointer. I figure that it would more easily allow my library routines to know when to stop processing the array. typedef struct screen_disp { int sd_row; int sd_col; char *sd_buff; } SCR_DISP;
204
13043
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 = {0,1,2,4,9};
2
2129
by: Simon Morgan | last post by:
I hope this isn't OT, I looked for a newsgroup dealing purely with algorithms but none were to be found and seeing as I'm trying to implement this in C I thought this would be the best place. I have an array of structs containing data which I'd like to output ordered based on the value of a single member. I was wondering if there is a relatively simple way of doing this without actually modifying the structure of the array? I had a...
15
3837
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> typedef struct _tnode_t { void *content; struct _tnode_t *kids;
5
348
by: sherifffruitfly | last post by:
Hi, I'm just learning cpp, and the exercise I'm working on is basically as follows: 1) Create a struct type with 4 members (char, char, char, int). 2) Create an array of, say 3 instances of the struct, and populate them with data. 3) cin 1, 2, 3, or 4 from the user 4) If the user selected, say, 2, display the contents of the 2nd data
11
2635
by: Cliff Martin | last post by:
Hi, I am reading a fairly large file a line at a time, doing some processing, and filtering out bits of the line. I am storing the interesting information in a struct and then printing it out. This works without any problems. I now would like to filter "duplicate" records. They aren't really duplicate, I can't just qsort as is and eliminate the matching rows. I have number of fields that will be the same, but some of the fields will...
57
3246
by: buuuuuum | last post by:
why array can't be assigned, like structs?
5
2294
by: dev_15 | last post by:
Hi, I'm going through some code and thought that this allocates an array of structs but its supposed according to comments to allocate an array of pointer to structs. What does it actually do ptrLogArray = new structDisplayLogData *; // iCount = 50 structDisplayLogData is a strcuture of data Thanks
2
11942
by: hal | last post by:
Hi, I'm trying to make an array of pointers to 'TwoCounts' structs, where the size of the array is arraySize. Right now I'm just mallocing enough space for all the pointers to the structs, and mallocing space for the pointer 'countPtr' in each struct, but do I need to do anything else? Thanks. typedef struct TwoCounts { int *countPtr;
0
8917
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8761
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9281
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9142
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8148
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2163
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.