C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 11th, 2013, 09:31 AM
comi comi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 1 comi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 9 sec
Reputation Power: 0
Struct array in c

hello
does anyone knows how can i create an array of structs and how can i get every struct address?
actually i dont know the size of the array and i tried something like
structname *example;
but it doesnt work.
tnx

Reply With Quote
  #2  
Old February 11th, 2013, 11:28 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,836 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 16 h 15 m 5 sec
Reputation Power: 1774
Did you try
Code:
// an array of 10 structname's
structname example[10];


> and how can i get every struct address?
That would be &example[index] for all the valid subscripts of the array.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old February 11th, 2013, 11:50 AM
schadocalex schadocalex is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Location: France
Posts: 2 schadocalex User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 13 m 22 sec
Reputation Power: 0
Hi,

Quote:
actually i dont know the size of the array

So structname example[10] isn't appropriated!

structname *example; may work, but it depends on the way you use it! If you don't know the size, you have to allocate it with malloc(). After that, you use it like salem showed you : &example[index].

Reply With Quote
  #4  
Old February 11th, 2013, 02:08 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,127 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 17 h 44 m
Reputation Power: 1949
Quote:
Originally Posted by comi
hello
does anyone knows how can i create an array of structs and how can i get every struct address?
actually i dont know the size of the array and i tried something like
structname *example;
but it doesnt work.
tnx

structname *example would serve to point to the dynamically created array, but it is useless until you have initialized it with memory to contain that array. Since this is C, you would use malloc thus:
example = malloc(sizeof(structname)*ARRAY_SIZE);
where ARRAY_SIZE would be a macro (AKA "a #define") representing the number of elements in the array.

You don't know what size to make the array, so malloc it first with some arbitrary size. I would use 10 just because it's a not-too-big round number, but you could even use 1. Then when you have filled that array and need to add more, use realloc() to change the size of the array. You could add one more element with each call to realloc, but I would think that that operation would slow your program down a bit, so I would be inclined to add multiple elements each time, like say another 10. Or you could just add one at a time and see how well it works.

You should also be keeping in an integer variable a count of the number of structs you have in that array.

PS

Merci, schadocalex! I didn't see where he said that he doesn't know ahead of time how many structs he'll have to store in the array.

Last edited by dwise1_aol : February 11th, 2013 at 02:11 PM.

Reply With Quote
  #5  
Old February 11th, 2013, 03:28 PM
schadocalex schadocalex is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Location: France
Posts: 2 schadocalex User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 13 m 22 sec
Reputation Power: 0
Sorry, i understood it like that.

comi -> If you use realloc(), be sure to know how to use it! If realloc() fail (for any reason) and you don't operate (is the good verb? lol) it, you can say hello to difficulties, which are different from malloc().

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Struct array in c

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap