Solved

fgets,gets,scanf and array of char pointers

Posted on 1997-10-31
8
703 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
Comment Utility
#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
Comment Utility
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
Comment Utility
Adjusted points to 200
0
 
LVL 10

Accepted Solution

by:
rbr earned 200 total points
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 2

Expert Comment

by:wpinto
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
FILE closing 11 68
Is string constant address ? 10 189
Problem to ASCII 1 148
Using popen() and gunzip() to open file in HTTPServer 6 10
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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops 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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now