Solved

fgets,gets,scanf and array of char pointers

Posted on 1997-10-31
8
713 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
0
Comment
Question by:johnm112496
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1256040
#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]);
0
 

Author Comment

by:johnm112496
ID: 1256041
FOR OZO ONLY
can the same be done with:

gets (names[i])??
John

Please send me your EMail @ msd@bendnet.com
0
 

Author Comment

by:johnm112496
ID: 1256042
Adjusted points to 200
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Accepted Solution

by:
rbr earned 200 total points
ID: 1256043
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.
 
0
 
LVL 2

Expert Comment

by:wpinto
ID: 1256044
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 1256045
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.
0
 
LVL 2

Expert Comment

by:wpinto
ID: 1256046
Hey ozo,

I can't argue with that!!

Hats off to you

Cheers,
Wilfred

PS. Sorry John, for diverting from the topic
0
 

Author Comment

by:johnm112496
ID: 1256047
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Detect CR LF to each line 12 175
C Programming - If Statement 8 83
nested if statement in excel help 4 41
A  &&  B  || (C||D) how it works in c 7 150
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question