We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

fgets,gets,scanf and array of char pointers

johnm112496
johnm112496 asked
on
Medium Priority
773 Views
Last Modified: 2012-08-14
I am trying to learn array of pointers:
Please show a SIMPLE Example to show me how to input data into
*names[4];
with scanf,gets,fgets.
Thank you, John
Comment
Watch Question

ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
#define len      20
#define slen      "20"
char *names[4];
for(i=0;i<4;i+=1){ names[i] = (char *)malloc(len+1); }
scanf("%" slen "s", names[0]);
gets(names[1]);
fgets(names[2],len,stdin);
printf("%s, %s, %s",names[0],names[1],names[2]);

Author

Commented:
FOR OZO ONLY
can the same be done with:

gets (names[i])??
John

Please send me your EMail @ msd@bendnet.com

Author

Commented:
Adjusted points to 200
Commented:
Sure the same can be done with gets(names[i]) also if i is in the range of 0 to 3 of your example.
 If you work with arraypointers  you must always be sure that you reserve
enough memory. This can be done with malloc or I prefer calloc since this clears
to memory you get from your OS.
 

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
Hey ozo,

Why don't you give answers rather than comments?

You always get there first, then wait for someone else to give your comments as their answers

Wilfred
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
Since I can't tell what the questioner will consider an answer before
they comment back, I prefer not to lock the question until then.
Sometimes questions here seem to degenerate into a race to lock
rather than an attempt to find the best answer, and I don't want to
participate in such a competition.
Sometimes finding answers requires the cooperation of several commentators.
Others may have a different policy, but that's up to them.
The questioner always has the final say about what's an answer in any case.

Commented:
Hey ozo,

I can't argue with that!!

Hats off to you

Cheers,
Wilfred

PS. Sorry John, for diverting from the topic

Author

Commented:
How do you allocate memory while the user inputs the strings with gets(string[i]);  this should allow the computer all the mem allocation for you right? John
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.