Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 01-09-2008, 07:03 PM   #1 (permalink)
Traceur
 
zegulas's Avatar
 
Join Date: Oct 2003
Location: Mumbai
Posts: 276
Default How to store n number of strings in an array with unknown size


How to declare an array for storing strings while we don't know how many will be stored, it has to be specified during runtime by the user. For C language.
__________________
Parkour: A journey started by two & followed by many...

Last edited by zegulas; 06-09-2008 at 06:44 PM.
zegulas is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 01-09-2008, 08:51 PM   #2 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: How to store n number of strings in an with unknown size

Use a linked list data structure.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 01-09-2008, 09:13 PM   #3 (permalink)
Traceur
 
zegulas's Avatar
 
Join Date: Oct 2003
Location: Mumbai
Posts: 276
Default Re: How to store n number of strings in an with unknown size

I don't know how to use it, could you give an example code?
__________________
Parkour: A journey started by two & followed by many...
zegulas is offline  
Old 02-09-2008, 01:20 PM   #4 (permalink)
Legen-wait for it-dary!
 
dheeraj_kumar's Avatar
 
Join Date: Dec 2004
Location: Chennai
Posts: 2,471
Default Re: How to store n number of strings in an with unknown size

Code:
struct node
{
   int id;
   char *string;
   struct node *next;
}*head, *temp;
id = number of the string / index of the array
string = pointer to your string.
next = next item in linked list.
head = head of the list
temp = used to allocate new nodes in the list

you need to use the basic linked list concepts like malloc() to allocate memory, changing the next pointer etc. if you need a small tut on lists,

http://cslibrary.stanford.edu/103/LinkedListBasics.pdf
http://www.ehow.com/how_2056292_crea...ed-list-c.html

make sure you initialize your structure members, you wouldnt want segmentation faults
__________________
If the Start Windows Restart when Windows starts check box is checked Windows Restart will start automatically every time Windows is started. - Actual excerpt from a windows program help file
dheeraj_kumar is offline  
Old 06-09-2008, 06:44 PM   #5 (permalink)
Traceur
 
zegulas's Avatar
 
Join Date: Oct 2003
Location: Mumbai
Posts: 276
Default Re: How to store n number of strings in an with unknown size

Can someone show an example for calloc to do the same?
__________________
Parkour: A journey started by two & followed by many...
zegulas is offline  
Old 06-09-2008, 07:13 PM   #6 (permalink)
Legen-wait for it-dary!
 
dheeraj_kumar's Avatar
 
Join Date: Dec 2004
Location: Chennai
Posts: 2,471
Default Re: How to store n number of strings in an with unknown size

calloc takes in the no. of items to be assigned memory as its argument. Considering your situation, where you dont know the no. of items, its better to use malloc.
__________________
If the Start Windows Restart when Windows starts check box is checked Windows Restart will start automatically every time Windows is started. - Actual excerpt from a windows program help file
dheeraj_kumar is offline  
Old 20-09-2008, 09:42 PM   #7 (permalink)
Right Off the Assembly Line
 
Join Date: Sep 2008
Posts: 3
Default Re: How to store n number of strings in an with unknown size

use teh following code fragment using malloc to take inout of strings using dynamic memory allocation.
include necessary header files.
main()
{
int i=-1;
char *a,ch;
a=(char*)(malloc(a,1*sizeof(char));
printf("Enter the string");
do{
ch=getchar();
i++;
a=(char*)(realloc(a,i+1*sizeof(char));
*(a+i)=ch;
}while (ch!='\n');
*a='\0';
}
If this code has some errors please let me know i can help you out.......
abhijeet.k1810 is offline  
Old 20-09-2008, 10:48 PM   #8 (permalink)
TechFreakiez.com
 
Abhishek Dwivedi's Avatar
 
Join Date: Sep 2006
Location: New Delhi
Posts: 621
Default Re: How to store n number of strings in an with unknown size

use Dynamic allocation...

Code:
  char *p;
  int val;
  cout<<"Enter Array size:";
  cin>>val;
  p=new char[val];
use the array as p[i]
this is dynamic allocation of memory...remembr to delete the memory aftr ur done with ur array using delete keyword...

Code:
delete p;
__________________
Personal Log | Star date 05.04.2009: TDF Meet Kanpur was Awesome :D
www.TechFreakiez.com

Last edited by Abhishek Dwivedi; 20-09-2008 at 10:53 PM.
Abhishek Dwivedi is offline  
Old 21-09-2008, 12:43 AM   #9 (permalink)
In The Zone
 
vandit's Avatar
 
Join Date: May 2005
Location: If I had a webcam u could have seen me, before your eyes.
Posts: 459
Default Re: How to store n number of strings in an with unknown size

@ abhieshek

the new and delete operators are available in c++ and not c.
and the program u have written will not form a table of strings as he wants.

regarding linked lists dude it will be more convinient for u to refer some books like e balagurusamy or let us c.
__________________
Searching for dell studio 15 ??
dell studio 15 review @ my blog

vandit007.blogspot.com

And remember the stylus that comes along with the touchscreens isnt meant for cleaning your ears !!
vandit is offline  
Old 21-09-2008, 11:16 AM   #10 (permalink)
TechFreakiez.com
 
Abhishek Dwivedi's Avatar
 
Join Date: Sep 2006
Location: New Delhi
Posts: 621
Default Re: How to store n number of strings in an with unknown size

oops...ma mistake..i thought its c++ questn...
__________________
Personal Log | Star date 05.04.2009: TDF Meet Kanpur was Awesome :D
www.TechFreakiez.com
Abhishek Dwivedi is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VISTA TUTORIAL: Add Customized Strings in System Properties Dialog Box Vishal Gupta Tutorials 7 11-07-2007 10:16 PM
Software To Store Song With Less Size shakti Mobiles and Tablets 17 04-03-2007 05:58 PM
Large Image files Size to Small Images Size. Charley QnA (read only) 14 03-03-2007 01:19 PM
What is maximum size of .pst file in microsoft outlook And Can I increase .pst's size fazle Software Q&A 1 25-02-2007 11:53 PM
How to identify Local sms number and National sms number? casual_gamer Mobiles and Tablets 2 04-12-2005 02:31 PM

 
Latest Threads
- by Sujeet
- by clmlbx
- by Sujeet
- by icebags

Advertisement




All times are GMT +5.5. The time now is 11:11 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2