Solved

fgets,gets,scanf and array of char pointers

Posted on 1997-10-31
8
709 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 switch statements in the C programming language.

770 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