943,969 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 9651
  • C RSS
Nov 1st, 2004
0

filling an array with strings using gets()

Expand Post »
I'm trying to understand how to write a function to fill an array of strings using gets(array_name[i]),i being the row index. Ihave no clue how to even start this I have created a driver that declares a 2-D array that is a list of strings with one string per row. I don't know how to write this function however to fill the array up with strings. If anyone can just get me started I would really appreciate it, this is sadly all I have.


#include "driver.h"
void fill_it(char strings[][LENGTH], int* count) {
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ginger is offline Offline
2 posts
since Sep 2004
Nov 1st, 2004
0

Re: filling an array with strings using gets()

Never use gets. It's impossible to make gets safe. All you need to do is match declarations:
  1. char strings[N][LENGTH];
  2.  
  3. ...
  4.  
  5. void f ( char strings[N][LENGTH], int size );
Then it's just a matter of counting from 0 to size - 1 and using fgets to fill strings[i] with a line. For example:
  1. int i;
  2.  
  3. for ( i = 0; i < size; i++ )
  4. fgets ( strings[i], LENGTH, stdin );
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC